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

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

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

            var heroPlaceholders = new List<DynamicPlaceholder>();

            if (isMyHero) {
                if (executedHero.IsFactionLeader) {
                    textTemplate = DynamicTextTemplate.Parsed(
                            $"{DisplayNames.ResolvedFactionString(actingFaction, currentModel)} has executed our sworn {DisplayNames.SiblingDescription(executedHero.PronounGender)} {DynamicTextTemplate.Placeholder(Placeholders.ExecutedHero)} in {province.Name}! They must pay for this atrocity!\n\n");
                    heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                            Placeholders.ExecutedHero,
                            executedHero,
                            "our leader"));
                } else {
                    textTemplate = DynamicTextTemplate.Parsed(
                            $"{DisplayNames.ResolvedFactionString(actingFaction, currentModel)} has executed our loyal vassal, {DynamicTextTemplate.Placeholder(Placeholders.ExecutedHero)} in {province.Name}!\n\n");
                    heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                            Placeholders.ExecutedHero,
                            executedHero,
                            "our vassal"));
                }
            } else if (isMyAction) {
                var victimDescription = VictimHeroDescription.WithResolvedFactionName(
                        victimFaction,
                        executedHero,
                        currentModel,
                        Placeholders.HeroName);
                textTemplate = DynamicTextTemplate.Parsed(
                        $"We executed {victimDescription} in {province.Name}.\n\n");
                heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                        Placeholders.HeroName,
                        executedHero,
                        "the hero"));
            } else {
                var victimDescription = VictimHeroDescription.WithResolvedFactionName(
                        victimFaction,
                        executedHero,
                        currentModel,
                        Placeholders.HeroName);
                textTemplate = DynamicTextTemplate.Parsed(
                        $"{DisplayNames.ResolvedFactionString(actingFaction, currentModel)} executed {victimDescription} in {province.Name}.\n\n");
                heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                        Placeholders.HeroName,
                        executedHero,
                        "the hero"));
            }

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