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

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

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

            if (isMyHero) {
                if (imprisonedHero.IsFactionLeader) {
                    textTemplate =
                            $"{{ActingFactionLeader}} has peacefully returned our sworn {DisplayNames.SiblingDescription(imprisonedHero.PronounGender)} {{ReturnedHero}} from {province.Name}!";
                    heroPlaceholders["ReturnedHero"] = (imprisonedHero.NameTextId, "our leader");
                } else {
                    textTemplate =
                            $"{{ActingFactionLeader}} has peacefully returned our loyal vassal, {{ReturnedHero}} from {province.Name}!";
                    heroPlaceholders["ReturnedHero"] = (imprisonedHero.NameTextId, "our vassal");
                }
                heroPlaceholders["ActingFactionLeader"] =
                        (currentModel.Heroes[actingFaction.FactionHeadId].NameTextId,
                         actingFaction.Name);
            } else if (isMyAction) {
                string victimDescription;
                if (victimFaction.FactionHeadId == imprisonedHero.Id) {
                    victimDescription = "faction leader {HeroName}";
                } else if (victimFaction.Leaders.Contains(imprisonedHero.Id)) {
                    victimDescription =
                            $"{{VictimFactionLeader}}'s sworn {DisplayNames.SiblingDescription(imprisonedHero.PronounGender)} {{HeroName}}";
                } else {
                    victimDescription = $"{{VictimFactionLeader}}'s hero {{HeroName}}";
                }
                textTemplate = $"We returned {victimDescription} from {province.Name}.";
                heroPlaceholders["HeroName"] = (imprisonedHero.NameTextId, "the hero");
                if (!victimFaction.FactionHeadId.Equals(imprisonedHero.Id)) {
                    heroPlaceholders["VictimFactionLeader"] =
                            (currentModel.Heroes[victimFaction.FactionHeadId].NameTextId,
                             victimFaction.Name);
                }
            } else {
                string victimDescription;
                if (victimFaction.FactionHeadId == imprisonedHero.Id) {
                    victimDescription = "faction leader {HeroName}";
                } else if (victimFaction.Leaders.Contains(imprisonedHero.Id)) {
                    victimDescription =
                            $"{{VictimFactionLeader}}'s sworn {DisplayNames.SiblingDescription(imprisonedHero.PronounGender)} {{HeroName}}";
                } else {
                    victimDescription = $"{{VictimFactionLeader}}'s hero {{HeroName}}";
                }
                textTemplate =
                        $"{{ActingFactionLeader}} has peacefully returned {victimDescription} from {province.Name}.";
                heroPlaceholders["HeroName"] = (imprisonedHero.NameTextId, "the hero");
                heroPlaceholders["ActingFactionLeader"] =
                        (currentModel.Heroes[actingFaction.FactionHeadId].NameTextId,
                         actingFaction.Name);
                if (!victimFaction.FactionHeadId.Equals(imprisonedHero.Id)) {
                    heroPlaceholders["VictimFactionLeader"] =
                            (currentModel.Heroes[victimFaction.FactionHeadId].NameTextId,
                             victimFaction.Name);
                }
            }

            yield return new DynamicTextNotification(
                    title: noteTitle,
                    textTemplate: textTemplate,
                    heroPlaceholders: heroPlaceholders,
                    singleProvinceId: details.ProvinceId,
                    displayedHeroes: new List<HeroView> { firstHero, secondHero });
        }
    }
}
