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

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

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

            if (isMyHero) {
                if (exiledHero.IsFactionLeader) {
                    textTemplate =
                            $"{{ActingFactionLeader}} has exiled our sworn {DisplayNames.SiblingDescription(exiledHero.PronounGender)} {{ExiledHero}} from {province.Name}!\n\n";
                    heroPlaceholders["ExiledHero"] = (exiledHero.NameTextId, "our leader");
                } else {
                    textTemplate =
                            $"{{ActingFactionLeader}} has exiled our loyal vassal, {{ExiledHero}} from {province.Name}!\n\n";
                    heroPlaceholders["ExiledHero"] = (exiledHero.NameTextId, "our vassal");
                }
                heroPlaceholders["ActingFactionLeader"] =
                        (currentModel.Heroes[actingFaction.FactionHeadId].NameTextId,
                         actingFaction.Name);
            } else if (isMyAction) {
                textTemplate = $"We exiled {{ExiledHero}} from {province.Name}.\n\n";
                heroPlaceholders["ExiledHero"] = (exiledHero.NameTextId, "the hero");
            } else {
                string victimDescription;
                if (victimFaction.FactionHeadId == exiledHero.Id) {
                    victimDescription = "faction leader {HeroName}";
                } else if (victimFaction.Leaders.Contains(exiledHero.Id)) {
                    victimDescription =
                            $"{{VictimFactionLeader}}'s sworn {DisplayNames.SiblingDescription(exiledHero.PronounGender)} {{HeroName}}";
                } else {
                    victimDescription = $"{{VictimFactionLeader}}'s hero {{HeroName}}";
                }
                textTemplate =
                        $"{{ActingFactionLeader}} has exiled {victimDescription} from {province.Name}.\n\n";
                heroPlaceholders["HeroName"] = (exiledHero.NameTextId, "the hero");
                heroPlaceholders["ActingFactionLeader"] =
                        (currentModel.Heroes[actingFaction.FactionHeadId].NameTextId,
                         actingFaction.Name);
                if (!victimFaction.FactionHeadId.Equals(exiledHero.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 });
        }
    }
}
