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 class Placeholders {
            public static readonly DynamicTextPlaceholderName ActingFactionLeader =
                    new(nameof(ActingFactionLeader));
            public static readonly DynamicTextPlaceholderName HeroName = new(nameof(HeroName));
            public static readonly DynamicTextPlaceholderName ImprisonedHero =
                    new(nameof(ImprisonedHero));
            public static readonly DynamicTextPlaceholderName VictimFactionLeader =
                    new(nameof(VictimFactionLeader));
        }

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

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

            var heroPlaceholders = new List<DynamicPlaceholder>();

            if (isMyHero) {
                if (imprisonedHero.IsFactionLeader) {
                    textTemplate = DynamicTextTemplate.Parsed(
                            $"{DynamicTextTemplate.Placeholder(Placeholders.ActingFactionLeader)} has imprisoned our sworn {DisplayNames.SiblingDescription(imprisonedHero.PronounGender)} {DynamicTextTemplate.Placeholder(Placeholders.ImprisonedHero)} in {province.Name}!\n\n");
                    heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                            Placeholders.ImprisonedHero,
                            imprisonedHero,
                            "our leader"));
                } else {
                    textTemplate = DynamicTextTemplate.Parsed(
                            $"{DynamicTextTemplate.Placeholder(Placeholders.ActingFactionLeader)} has imprisoned our loyal vassal, {DynamicTextTemplate.Placeholder(Placeholders.ImprisonedHero)} in {province.Name}!\n\n");
                    heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                            Placeholders.ImprisonedHero,
                            imprisonedHero,
                            "our vassal"));
                }
                heroPlaceholders.Add(DynamicPlaceholder.FactionLeaderName(
                        Placeholders.ActingFactionLeader,
                        actingFaction,
                        currentModel,
                        actingFaction.Name));
            } else if (isMyAction) {
                var victimDescription = VictimHeroDescription.WithFactionLeaderPlaceholder(
                        victimFaction,
                        imprisonedHero,
                        Placeholders.HeroName,
                        Placeholders.VictimFactionLeader);
                textTemplate = DynamicTextTemplate.Parsed(
                        $"We imprisoned {victimDescription} in {province.Name}.\n\n");
                heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                        Placeholders.HeroName,
                        imprisonedHero,
                        "the hero"));
                if (VictimHeroDescription.UsesFactionLeaderPlaceholder(
                            victimFaction,
                            imprisonedHero)) {
                    heroPlaceholders.Add(DynamicPlaceholder.FactionLeaderName(
                            Placeholders.VictimFactionLeader,
                            victimFaction,
                            currentModel,
                            victimFaction.Name));
                }
            } else {
                var victimDescription = VictimHeroDescription.WithFactionLeaderPlaceholder(
                        victimFaction,
                        imprisonedHero,
                        Placeholders.HeroName,
                        Placeholders.VictimFactionLeader);
                textTemplate = DynamicTextTemplate.Parsed(
                        $"{DynamicTextTemplate.Placeholder(Placeholders.ActingFactionLeader)} has imprisoned {victimDescription} in {province.Name}.\n\n");
                heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                        Placeholders.HeroName,
                        imprisonedHero,
                        "the hero"));
                heroPlaceholders.Add(DynamicPlaceholder.FactionLeaderName(
                        Placeholders.ActingFactionLeader,
                        actingFaction,
                        currentModel,
                        actingFaction.Name));
                if (VictimHeroDescription.UsesFactionLeaderPlaceholder(
                            victimFaction,
                            imprisonedHero)) {
                    heroPlaceholders.Add(DynamicPlaceholder.FactionLeaderName(
                            Placeholders.VictimFactionLeader,
                            victimFaction,
                            currentModel,
                            victimFaction.Name));
                }
            }

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