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

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

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

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

            var hero = currentModel.Heroes[note.ExecutedHeroId];
            var executingFactionName = currentModel.FactionName(note.ExecutingFactionId);

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

            string noteTitle = "Prisoner Executed";
            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(
                        $"{executingFactionName} has executed {victimDescription}!\n\n");
                heroPlaceholders.Add(
                        DynamicPlaceholder.HeroName(Placeholders.HeroName, hero, "the hero"));
            } else {
                textTemplate = DynamicTextTemplate.Parsed(
                        $"{executingFactionName} has executed {DynamicTextTemplate.Placeholder(Placeholders.ExecutedHero)}!\n\n");
                heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                        Placeholders.ExecutedHero,
                        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 });
        }
    }
}
