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 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";
            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 = $"{executingFactionName} has executed {victimDescription}!";
                heroPlaceholders["HeroName"] = (hero.NameTextId, "the hero");
            } else {
                textTemplate = $"{executingFactionName} has executed {{ExecutedHero}}!";
                heroPlaceholders["ExecutedHero"] = (hero.NameTextId, "the prisoner");
            }

            yield return new DynamicTextNotification(
                    title: noteTitle,
                    textTemplate: textTemplate,
                    heroPlaceholders: heroPlaceholders,
                    singleProvinceId: note.ProvinceId,
                    displayedHeroes: new List<HeroView> { factionLeader, hero });
        }
    }
}