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

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

    public static class CapturedHeroImprisonedDetailsNotificationGenerator {
        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.CapturedHeroImprisonedDetails;
            bool isMyAction = details.ImprisoningFactionId == playerId;

            var province = currentModel.Provinces[details.ProvinceId];
            var actingFaction = currentModel.MaybeDestroyedFaction(details.ImprisoningFactionId);
            var victimFaction = currentModel.MaybeDestroyedFaction(details.CapturedFromFactionId);

            var imprisonedHero = currentModel.Heroes[details.ImprisonedHeroId];
            var isMyHero = imprisonedHero.FactionId is {}
            fid && fid == currentModel.PlayerId;

            string noteTitle = "Hero Imprisoned";
            string textTemplate;

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

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

            if (isMyHero) {
                if (imprisonedHero.IsFactionLeader) {
                    textTemplate =
                            $"{{ActingFactionLeader}} has imprisoned our sworn {DisplayNames.SiblingDescription(imprisonedHero.PronounGender)} {{ImprisonedHero}} in {province.Name}!\n\n";
                    heroPlaceholders["ImprisonedHero"] = (imprisonedHero.NameTextId, "our leader");
                } else {
                    textTemplate =
                            $"{{ActingFactionLeader}} has imprisoned our loyal vassal, {{ImprisonedHero}} in {province.Name}!\n\n";
                    heroPlaceholders["ImprisonedHero"] = (imprisonedHero.NameTextId, "our vassal");
                }
                heroPlaceholders["ActingFactionLeader"] =
                        (currentModel.Heroes[actingFaction.FactionHeadId].NameTextId,
                         actingFaction.Name);
            } else if (isMyAction) {
                string victimDescription;
                if (victimFaction.FactionHeadId == imprisonedHero.Id) {
                    victimDescription = "faction leader {HeroName}";
                } else if (victimFaction.Leaders.Contains(imprisonedHero.Id)) {
                    victimDescription =
                            $"{{VictimFactionLeader}}'s sworn {DisplayNames.SiblingDescription(imprisonedHero.PronounGender)} {{HeroName}}";
                } else {
                    victimDescription = $"{{VictimFactionLeader}}'s hero {{HeroName}}";
                }
                textTemplate = $"We imprisoned {victimDescription} in {province.Name}.\n\n";
                heroPlaceholders["HeroName"] = (imprisonedHero.NameTextId, "the hero");
                if (!victimFaction.FactionHeadId.Equals(imprisonedHero.Id)) {
                    heroPlaceholders["VictimFactionLeader"] =
                            (currentModel.Heroes[victimFaction.FactionHeadId].NameTextId,
                             victimFaction.Name);
                }
            } else {
                string victimDescription;
                if (victimFaction.FactionHeadId == imprisonedHero.Id) {
                    victimDescription = "faction leader {HeroName}";
                } else if (victimFaction.Leaders.Contains(imprisonedHero.Id)) {
                    victimDescription =
                            $"{{VictimFactionLeader}}'s sworn {DisplayNames.SiblingDescription(imprisonedHero.PronounGender)} {{HeroName}}";
                } else {
                    victimDescription = $"{{VictimFactionLeader}}'s hero {{HeroName}}";
                }
                textTemplate =
                        $"{{ActingFactionLeader}} has imprisoned {victimDescription} in {province.Name}.\n\n";
                heroPlaceholders["HeroName"] = (imprisonedHero.NameTextId, "the hero");
                heroPlaceholders["ActingFactionLeader"] =
                        (currentModel.Heroes[actingFaction.FactionHeadId].NameTextId,
                         actingFaction.Name);
                if (!victimFaction.FactionHeadId.Equals(imprisonedHero.Id)) {
                    heroPlaceholders["VictimFactionLeader"] =
                            (currentModel.Heroes[victimFaction.FactionHeadId].NameTextId,
                             victimFaction.Name);
                }
            }

            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 });
        }
    }
}
