using System.Collections.Generic;
using Net.Eagle0.Eagle.Views;

namespace eagle.Notifications.ARNNotifications {
    public static class PrisonerReturnedDetailsNotificationGenerator {
        public static readonly ARNNotificationGenerator Generator = GenerateNotifications;

        private static class Placeholders {
            public static readonly DynamicTextPlaceholderName HeroName = new(nameof(HeroName));
            public static readonly DynamicTextPlaceholderName ReturnedHero =
                    new(nameof(ReturnedHero));
        }

        private static IEnumerable<Notification> GenerateNotifications(
                Net.Eagle0.Eagle.Common.Notification notification,
                IGameModel currentModel) {
            var note = notification.Details.PrisonerReturnedDetails;

            var hero = currentModel.Heroes[note.ReturnedHeroId];
            var returningFactionName = currentModel.FactionName(note.ReturningFactionId);
            var toFactionName = currentModel.FactionName(note.ToFactionId);

            var factionLeader =
                    currentModel.Heroes[currentModel.ActiveFactions[note.ReturningFactionId]
                                                .FactionHeadId];

            string noteTitle = "Prisoner Returned";
            DynamicTextTemplate textTemplate;

            var heroPlaceholders = new List<DynamicPlaceholder>();

            if (note.LastFactionId is {} lastFactionId) {
                var lastFaction = currentModel.MaybeDestroyedFaction(lastFactionId);

                var victimDescription = VictimHeroDescription.WithResolvedFactionName(
                        lastFaction,
                        hero,
                        currentModel,
                        Placeholders.HeroName);
                textTemplate = DynamicTextTemplate.Parsed(
                        $"{returningFactionName} has returned {victimDescription} to {toFactionName}!\n\n");
                heroPlaceholders.Add(
                        DynamicPlaceholder.HeroName(Placeholders.HeroName, hero, "the hero"));
            } else {
                textTemplate = DynamicTextTemplate.Parsed(
                        $"{returningFactionName} has returned {DynamicTextTemplate.Placeholder(Placeholders.ReturnedHero)} to {toFactionName}!\n\n");
                heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                        Placeholders.ReturnedHero,
                        hero,
                        "the prisoner"));
            }

            yield return DynamicTextNotification.StreamingDynamicNotification(
                    title: noteTitle,
                    textTemplate: textTemplate,
                    placeholders: heroPlaceholders,
                    llmId: notification.LlmId,
                    provinceIds: new List<int> { note.ProvinceId },
                    displayedHeroes: new List<HeroView> { factionLeader, hero });
        }
    }
}
