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

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

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

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

            var hero = currentModel.Heroes[note.ReleasedHeroId];
            var releasingFactionName = currentModel.FactionName(note.ReleasingFactionId);

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

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