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 StarvationDetailsNotificationGenerator {
        public static readonly ARNNotificationGenerator Generator = GenerateNotifications;

        private static class Placeholders {
            public static readonly DynamicTextPlaceholderName RulingHero = new(nameof(RulingHero));
        }

        private static readonly Random Random = new();

        private static readonly List<string> NotificationTitles = new() {
            "Famine Strikes",
            "Troops Starving",
            "Mass Starvation",
            "Hunger Claims Lives",
            "Empty Bellies, Empty Ranks",
            "Famine's Toll",
            "Starvation Crisis"
        };

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

        private static IEnumerable<Notification> GenerateNotifications(
                Net.Eagle0.Eagle.Common.Notification notification,
                IGameModel currentModel) {
            var details = notification.Details.StarvationDetails;
            var province = currentModel.Provinces[details.ProvinceId];

            var textTemplate = DynamicTextTemplate.Parsed(
                    $"Catastrophe in {province.Name}! The province has run out of food and troops are starving. {DynamicTextTemplate.Placeholder(Placeholders.RulingHero)} addresses the realm:\n\n");

            var heroPlaceholders = new List<DynamicPlaceholder>();
            var displayedHeroes = new List<HeroView>();

            if (currentModel.Heroes.TryGetValue(details.RulingHeroId, out var rulingHero)) {
                heroPlaceholders.Add(DynamicPlaceholder.HeroName(
                        Placeholders.RulingHero,
                        rulingHero,
                        "The governor"));
                displayedHeroes.Add(rulingHero);
            } else {
                heroPlaceholders.Add(
                        DynamicPlaceholder.StaticText(Placeholders.RulingHero, "The governor"));
            }

            yield return DynamicTextNotification.StreamingDynamicNotification(
                    title: GetNotificationTitle(),
                    textTemplate: textTemplate,
                    placeholders: heroPlaceholders,
                    llmId: notification.LlmId,
                    provinceIds: new List<ProvinceId> { details.ProvinceId },
                    displayedHeroes: displayedHeroes);
        }
    }
}
