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 LowFoodWarningDetailsNotificationGenerator {
        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() {
            "Granaries Running Low",
            "Food Shortage Warning",
            "Dwindling Supplies",
            "Hungry Times Ahead",
            "Provisions Depleting",
            "Crisis Looming",
            "Empty Granaries"
        };

        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.LowFoodWarningDetails;
            var province = currentModel.Provinces[details.ProvinceId];
            var rulingHeroId = province.FullInfo?.RulingHeroId;

            DynamicTextTemplate textTemplate;

            if (details.MonthsOfFoodRemaining <= 1) {
                textTemplate = DynamicTextTemplate.Parsed(
                        $"Dire news from {DynamicTextTemplate.Placeholder(Placeholders.RulingHero)} in {province.Name}. With only {details.CurrentFood} food remaining and monthly consumption of {details.MonthlyConsumption}, starvation looms within the month.\n\n");
            } else {
                textTemplate = DynamicTextTemplate.Parsed(
                        $"{DynamicTextTemplate.Placeholder(Placeholders.RulingHero)} reports that {province.Name} has only {details.MonthsOfFoodRemaining} months of food remaining. Current stores: {details.CurrentFood}. Monthly consumption: {details.MonthlyConsumption}.\n\n");
            }

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

            if (rulingHeroId.HasValue &&
                currentModel.Heroes.TryGetValue(rulingHeroId.Value, 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);
        }
    }
}
