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

namespace eagle.Notifications.ARNNotifications {
    public static class CapturedHeroReturnedDetailsNotificationGenerator {
        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 ReturnedHero =
                    new(nameof(ReturnedHero));
            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.CapturedHeroReturnedDetails;
            bool isMyAction = details.ReturnedHeroId == playerId;

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

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

            string noteTitle = "Hero Returned";
            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 peacefully returned our sworn {DisplayNames.SiblingDescription(imprisonedHero.PronounGender)} {DynamicTextTemplate.Placeholder(Placeholders.ReturnedHero)} from {province.Name}!");
                    heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                            Placeholders.ReturnedHero,
                            imprisonedHero,
                            "our leader"));
                } else {
                    textTemplate = DynamicTextTemplate.Parsed(
                            $"{DynamicTextTemplate.Placeholder(Placeholders.ActingFactionLeader)} has peacefully returned our loyal vassal, {DynamicTextTemplate.Placeholder(Placeholders.ReturnedHero)} from {province.Name}!");
                    heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                            Placeholders.ReturnedHero,
                            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 returned {victimDescription} from {province.Name}.");
                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 peacefully returned {victimDescription} from {province.Name}.");
                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 new DynamicTextNotification(
                    title: noteTitle,
                    textTemplate: textTemplate,
                    placeholders: heroPlaceholders,
                    singleProvinceId: details.ProvinceId,
                    displayedHeroes: new List<HeroView> { firstHero, secondHero });
        }
    }
}
