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

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

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

        private static class Placeholders {
            public static readonly DynamicTextPlaceholderName RansomedHero =
                    new(nameof(RansomedHero));
            public static readonly DynamicTextPlaceholderName PaidToFaction =
                    new(nameof(PaidToFaction));
            public static readonly DynamicTextPlaceholderName PaidByFaction =
                    new(nameof(PaidByFaction));
        }

        private static IEnumerable<Notification> GenerateNotifications(
                Net.Eagle0.Eagle.Common.Notification notification,
                IGameModel currentModel) {
            var playerId = currentModel.PlayerId;
            var notif = notification.Details.RansomPaidDetails;
            var paidToFactionId = notif.RansomPaidToFactionId;
            var paidByFactionId = notif.RansomPaidByFactionId;
            var affectedProvinces = currentModel.ProvincesForFaction(paidToFactionId);
            affectedProvinces.AddRange(currentModel.ProvincesForFaction(paidByFactionId));

            var acceptingFactionHead =
                    currentModel.Heroes[currentModel.MaybeDestroyedFaction(paidToFactionId)
                                                .FactionHeadId];

            var offeringFactionHead =
                    currentModel.Heroes[currentModel.MaybeDestroyedFaction(paidByFactionId)
                                                .FactionHeadId];

            var ransomedHeroId = notif.RansomedHeroId;
            var ransomedHero = currentModel.Heroes[ransomedHeroId];

            var ransomedHeroPlaceholder = DynamicPlaceholder.HeroName(
                    Placeholders.RansomedHero,
                    ransomedHero,
                    "the hero");
            var paidToFactionPlaceholder = DynamicPlaceholder.FactionDisplayNameOrLeaderName(
                    Placeholders.PaidToFaction,
                    paidToFactionId,
                    currentModel,
                    "Leader");
            var paidByFactionPlaceholder = DynamicPlaceholder.FactionDisplayNameOrLeaderName(
                    Placeholders.PaidByFaction,
                    paidByFactionId,
                    currentModel,
                    "Leader");

            if (playerId.HasValue && playerId.Value == paidToFactionId) {
                var textTemplate = DynamicTextTemplate.Parsed(
                        $"We have accepted the ransom from {DynamicTextTemplate.Placeholder(Placeholders.PaidByFaction)} for {DynamicTextTemplate.Placeholder(Placeholders.RansomedHero)}.\n\n");
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ransom Accepted",
                        textTemplate: textTemplate,
                        placeholders: new List<
                                DynamicPlaceholder> { paidByFactionPlaceholder, ransomedHeroPlaceholder },
                        llmId: notification.LlmId,
                        provinceIds: affectedProvinces,
                        displayedHeroes: new List<HeroView> { ransomedHero, offeringFactionHead });
            } else if (playerId.HasValue && playerId.Value == paidByFactionId) {
                var textTemplate = DynamicTextTemplate.Parsed(
                        $"{DynamicTextTemplate.Placeholder(Placeholders.PaidToFaction)} has accepted our ransom for {DynamicTextTemplate.Placeholder(Placeholders.RansomedHero)}.\n\n");
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ransom Accepted",
                        textTemplate: textTemplate,
                        placeholders: new List<
                                DynamicPlaceholder> { paidToFactionPlaceholder, ransomedHeroPlaceholder },
                        llmId: notification.LlmId,
                        provinceIds: affectedProvinces,
                        displayedHeroes: new List<HeroView> { acceptingFactionHead, ransomedHero });
            } else {
                var textTemplate = DynamicTextTemplate.Parsed(
                        $"{DynamicTextTemplate.Placeholder(Placeholders.PaidByFaction)} paid a ransom to {DynamicTextTemplate.Placeholder(Placeholders.PaidToFaction)} for the return of {DynamicTextTemplate.Placeholder(Placeholders.RansomedHero)}.\n\n");
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ransom Paid",
                        textTemplate: textTemplate,
                        placeholders: new List<
                                DynamicPlaceholder> { paidByFactionPlaceholder, paidToFactionPlaceholder, ransomedHeroPlaceholder },
                        llmId: notification.LlmId,
                        provinceIds: affectedProvinces,
                        displayedHeroes: new List<HeroView> {
                            acceptingFactionHead,
                            offeringFactionHead,
                            ransomedHero
                        });
            }
        }
    }
}
