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

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

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

        private static readonly Random Random = new();

        private static readonly List<string> NotificationTitles = new() {
            "A Vassal Rises",
            "From Servant to Leader",
            "Unexpected Succession",
            "Rise of the Unsung",
            "Fate's Choice",
            "The Loyal Promoted",
            "New Dawn"
        };

        private static string GetNotificationTitle() {
            return NotificationTitles[Random.Next(NotificationTitles.Count)];
        }

        private static ProvinceId? FindProvinceForHero(HeroId heroId, IGameModel model) {
            foreach (var province in model.Provinces.Values) {
                if (province.FullInfo?.RulingFactionHeroIds.Contains(heroId) == true) {
                    return province.Id;
                }
            }
            return null;
        }

        private static IEnumerable<Notification> GenerateNotifications(
                Net.Eagle0.Eagle.Common.Notification notification,
                IGameModel currentModel) {
            var details = notification.Details.VassalRisesDetails;
            var risingHero = currentModel.Heroes[details.RisingHeroId];

            string textTemplate;
            List<ProvinceId> affectedProvinces;

            if (details.FactionId == currentModel.PlayerId) {
                // Player's own faction - a vassal has risen to lead
                if (details.IsOnlyLeader) {
                    textTemplate =
                            "With all your faction's leaders fallen or imprisoned, your loyal vassal {RisingHero} has risen to lead your forces.\n\n";
                } else {
                    textTemplate =
                            "Your vassal {RisingHero} has risen to join the leadership of your faction.\n\n";
                }

                var heroProvinceId = FindProvinceForHero(details.RisingHeroId, currentModel);
                affectedProvinces = heroProvinceId.HasValue
                                            ? new List<ProvinceId> { heroProvinceId.Value }
                                            : currentModel.ProvincesForFaction(details.FactionId);
            } else {
                // Another faction
                var factionName = currentModel.FactionName(details.FactionId);
                if (details.IsOnlyLeader) {
                    textTemplate =
                            $"With all of {factionName}'s leaders fallen or imprisoned, {{RisingHero}} has risen from obscurity to lead their forces.\n\n";
                } else {
                    textTemplate =
                            $"{{RisingHero}} has risen to join the leadership of {factionName}.\n\n";
                }
                affectedProvinces = currentModel.ProvincesForFaction(details.FactionId);
            }

            var heroPlaceholders = new Dictionary<string, (string nameTextId, string fallback)> {
                { "RisingHero", (risingHero.NameTextId, "A vassal") }
            };

            var displayedHeroes = new List<HeroView> { risingHero };

            yield return DynamicTextNotification.StreamingDynamicNotification(
                    title: GetNotificationTitle(),
                    textTemplate: textTemplate,
                    heroPlaceholders: heroPlaceholders,
                    llmId: notification.LlmId,
                    provinceIds: affectedProvinces,
                    displayedHeroes: displayedHeroes);
        }
    }
}
