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

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

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

        private static class Placeholders {
            public static readonly DynamicTextPlaceholderName ActingFactionLeader =
                    new(nameof(ActingFactionLeader));
            public static readonly DynamicTextPlaceholderName ExiledHero = new(nameof(ExiledHero));
            public static readonly DynamicTextPlaceholderName HeroName = new(nameof(HeroName));
            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.CapturedHeroExiledDetails;
            bool isMyAction = details.ExilingFactionId == playerId;

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

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

            string noteTitle = "Hero Exiled";
            DynamicTextTemplate textTemplate;

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

            var heroPlaceholders = new List<DynamicPlaceholder>();

            if (isMyHero) {
                if (exiledHero.IsFactionLeader) {
                    textTemplate = DynamicTextTemplate.Parsed(
                            $"{DynamicTextTemplate.Placeholder(Placeholders.ActingFactionLeader)} has exiled our sworn {DisplayNames.SiblingDescription(exiledHero.PronounGender)} {DynamicTextTemplate.Placeholder(Placeholders.ExiledHero)} from {province.Name}!\n\n");
                    heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                            Placeholders.ExiledHero,
                            exiledHero,
                            "our leader"));
                } else {
                    textTemplate = DynamicTextTemplate.Parsed(
                            $"{DynamicTextTemplate.Placeholder(Placeholders.ActingFactionLeader)} has exiled our loyal vassal, {DynamicTextTemplate.Placeholder(Placeholders.ExiledHero)} from {province.Name}!\n\n");
                    heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                            Placeholders.ExiledHero,
                            exiledHero,
                            "our vassal"));
                }
                heroPlaceholders.Add(DynamicPlaceholder.FactionLeaderName(
                        Placeholders.ActingFactionLeader,
                        actingFaction,
                        currentModel,
                        actingFaction.Name));
            } else if (isMyAction) {
                textTemplate = DynamicTextTemplate.Parsed(
                        $"We exiled {DynamicTextTemplate.Placeholder(Placeholders.ExiledHero)} from {province.Name}.\n\n");
                heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                        Placeholders.ExiledHero,
                        exiledHero,
                        "the hero"));
            } else {
                var victimDescription = VictimHeroDescription.WithFactionLeaderPlaceholder(
                        victimFaction,
                        exiledHero,
                        Placeholders.HeroName,
                        Placeholders.VictimFactionLeader);
                textTemplate = DynamicTextTemplate.Parsed(
                        $"{DynamicTextTemplate.Placeholder(Placeholders.ActingFactionLeader)} has exiled {victimDescription} from {province.Name}.\n\n");
                heroPlaceholders.Add(
                        DynamicPlaceholder.HeroName(Placeholders.HeroName, exiledHero, "the hero"));
                heroPlaceholders.Add(DynamicPlaceholder.FactionLeaderName(
                        Placeholders.ActingFactionLeader,
                        actingFaction,
                        currentModel,
                        actingFaction.Name));
                if (VictimHeroDescription.UsesFactionLeaderPlaceholder(victimFaction, exiledHero)) {
                    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 });
        }
    }
}
