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 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";
            string textTemplate;

            var heroPlaceholders = new Dictionary<string, (string nameTextId, string fallback)>();

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

                string victimDescription;
                if (lastFaction.FactionHeadId == hero.Id) {
                    victimDescription = "faction leader {HeroName}";
                } else if (lastFaction.Leaders.Contains(hero.Id)) {
                    victimDescription =
                            $"{DisplayNames.ResolvedFactionString(lastFaction, currentModel)}'s sworn {DisplayNames.SiblingDescription(hero.PronounGender)} {{HeroName}}";
                } else {
                    victimDescription =
                            $"{DisplayNames.ResolvedFactionString(lastFaction, currentModel)}'s hero {{HeroName}}";
                }

                textTemplate = $"{releasingFactionName} has released {victimDescription}!\n\n";
                heroPlaceholders["HeroName"] = (hero.NameTextId, "the hero");
            } else {
                textTemplate = $"{releasingFactionName} has released {{ReleasedHero}}!\n\n";
                heroPlaceholders["ReleasedHero"] = (hero.NameTextId, "the prisoner");
            }

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