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 class Placeholders {
            public static readonly DynamicTextPlaceholderName DepartedHero =
                    new(nameof(DepartedHero));
            public static readonly DynamicTextPlaceholderName FactionLeader =
                    new(nameof(FactionLeader));
        }

        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 List<DynamicPlaceholder> {
                DynamicPlaceholder.HeroName(Placeholders.DepartedHero, departedHero, "A hero")
            };

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