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

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

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

        private static IEnumerable<Notification> GenerateNotifications(
                Net.Eagle0.Eagle.Common.Notification notification,
                IGameModel currentModel) {
            var notif = notification.Details.HeroDepartureDetails;
            var departedHero = currentModel.Heroes[notif.DepartingHeroId];
            var province = currentModel.Provinces[notif.ProvinceId];
            var faction = currentModel.MaybeDestroyedFaction(notif.FromFactionId);

            var provinceIds = new List<ProvinceId> { notif.ProvinceId };

            var heroPlaceholders = new Dictionary<string, (string nameTextId, string fallback)> {
                { "DepartedHero", (departedHero.NameTextId, "A hero") }
            };

            if (notif.FromFactionId == currentModel.PlayerId) {
                string textTemplate =
                        $"{{DepartedHero}} has left your service in {province.Name}.\n\n{notification.LlmMessage}";
                yield return new DynamicTextNotification(
                        title: "Hero Departed",
                        textTemplate: textTemplate,
                        heroPlaceholders: heroPlaceholders,
                        singleProvinceId: province.Id,
                        displayedHeroes: new List<HeroView> { departedHero });
            } else {
                string textTemplate =
                        $"{{DepartedHero}} has left {{FactionLeader}}'s service in {province.Name}.\n\n";
                heroPlaceholders["FactionLeader"] =
                        (currentModel.Heroes[faction.FactionHeadId].NameTextId, faction.Name);
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Hero Departed",
                        textTemplate: textTemplate,
                        heroPlaceholders: heroPlaceholders,
                        llmId: notification.LlmId,
                        provinceIds: provinceIds,
                        displayedHeroes: new List<HeroView> {
                            departedHero,
                            currentModel.Heroes[faction.FactionHeadId]
                        });
            }
        }
    }
}