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

namespace eagle.Notifications.ARNNotifications {
    using ProvinceId = Int32;

    public static class CapturedHeroExecutedDetailsNotificationGenerator {
        public static readonly ARNNotificationGenerator Generator = GenerateNotifications;

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

            var details = notification.Details.CapturedHeroExecutedDetails;
            bool isMyAction = details.ExecutingFactionId == playerId;

            var province = currentModel.Provinces[details.ProvinceId];
            var actingFaction = currentModel.MaybeDestroyedFaction(details.ExecutingFactionId);
            var victimFaction = currentModel.MaybeDestroyedFaction(details.CapturedFromFactionId);
            var executedHero = currentModel.Heroes[details.ExecutedHeroId];
            var isMyHero = executedHero.FactionId is {}
            fid && fid == currentModel.PlayerId;

            string noteTitle = "Hero Executed";
            string textTemplate;

            HeroView firstHero = currentModel.Heroes[actingFaction.FactionHeadId];
            HeroView secondHero = executedHero;

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

            if (isMyHero) {
                if (executedHero.IsFactionLeader) {
                    textTemplate =
                            $"{DisplayNames.ResolvedFactionString(actingFaction, currentModel)} has executed our sworn {DisplayNames.SiblingDescription(executedHero.PronounGender)} {{ExecutedHero}} in {province.Name}! They must pay for this atrocity!\n\n";
                    heroPlaceholders["ExecutedHero"] = (executedHero.NameTextId, "our leader");
                } else {
                    textTemplate =
                            $"{DisplayNames.ResolvedFactionString(actingFaction, currentModel)} has executed our loyal vassal, {{ExecutedHero}} in {province.Name}!\n\n";
                    heroPlaceholders["ExecutedHero"] = (executedHero.NameTextId, "our vassal");
                }
            } else if (isMyAction) {
                string victimDescription;
                if (victimFaction.FactionHeadId == executedHero.Id) {
                    victimDescription = "faction leader {HeroName}";
                } else if (victimFaction.Leaders.Contains(executedHero.Id)) {
                    victimDescription =
                            $"{DisplayNames.ResolvedFactionString(victimFaction, currentModel)}'s sworn {DisplayNames.SiblingDescription(executedHero.PronounGender)} {{HeroName}}";
                } else {
                    victimDescription =
                            $"{DisplayNames.ResolvedFactionString(victimFaction, currentModel)}'s hero {{HeroName}}";
                }
                textTemplate = $"We executed {victimDescription} in {province.Name}.\n\n";
                heroPlaceholders["HeroName"] = (executedHero.NameTextId, "the hero");
            } else {
                string victimDescription;
                if (victimFaction.FactionHeadId == executedHero.Id) {
                    victimDescription = "faction leader {HeroName}";
                } else if (victimFaction.Leaders.Contains(executedHero.Id)) {
                    victimDescription =
                            $"{DisplayNames.ResolvedFactionString(victimFaction, currentModel)}'s sworn {DisplayNames.SiblingDescription(executedHero.PronounGender)} {{HeroName}}";
                } else {
                    victimDescription =
                            $"{DisplayNames.ResolvedFactionString(victimFaction, currentModel)}'s hero {{HeroName}}";
                }
                textTemplate =
                        $"{DisplayNames.ResolvedFactionString(actingFaction, currentModel)} executed {victimDescription} in {province.Name}.\n\n";
                heroPlaceholders["HeroName"] = (executedHero.NameTextId, "the hero");
            }

            yield return DynamicTextNotification.StreamingDynamicNotification(
                    title: noteTitle,
                    textTemplate: textTemplate,
                    heroPlaceholders: heroPlaceholders,
                    llmId: notification.LlmId,
                    provinceIds: new List<ProvinceId> { details.ProvinceId },
                    displayedHeroes: new List<HeroView> { firstHero, secondHero });
        }
    }
}
