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 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";
            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 =
                        $"{returningFactionName} has returned {victimDescription} to {toFactionName}!\n\n";
                heroPlaceholders["HeroName"] = (hero.NameTextId, "the hero");
            } else {
                textTemplate =
                        $"{returningFactionName} has returned {{ReturnedHero}} to {toFactionName}!\n\n";
                heroPlaceholders["ReturnedHero"] = (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 });
        }
    }
}
