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

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

        private static class Placeholders {
            public static readonly DynamicTextPlaceholderName ExiledHero = new(nameof(ExiledHero));
            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.PrisonerExiledDetails;

            var hero = currentModel.Heroes[note.ExiledHeroId];
            var exilingFactionName = currentModel.FactionName(note.ExilingFactionId);

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

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