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

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

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

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

        private static IEnumerable<Notification> GenerateNotifications(
                Net.Eagle0.Eagle.Common.Notification notification,
                IGameModel currentModel) {
            var playerId = currentModel.PlayerId;
            var notif = notification.Details.RansomRejectedDetails;
            var offeringFactionId = notif.RansomOfferingFactionId;
            var offeringFactionHead =
                    currentModel
                            .Heroes[currentModel.ActiveFactions[offeringFactionId].FactionHeadId];
            var refusingFactionId = notif.RansomRefusedByFactionId;
            var refusingFactionHead =
                    currentModel
                            .Heroes[currentModel.ActiveFactions[refusingFactionId].FactionHeadId];

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

            var ransomedHeroPlaceholder = DynamicPlaceholder.HeroName(
                    Placeholders.RansomedHero,
                    ransomedHero,
                    "the hero");
            var offeringFactionPlaceholder = DynamicPlaceholder.FactionDisplayNameOrLeaderName(
                    Placeholders.OfferingFaction,
                    offeringFactionId,
                    currentModel,
                    "Leader");
            var refusingFactionPlaceholder = DynamicPlaceholder.FactionDisplayNameOrLeaderName(
                    Placeholders.RefusingFaction,
                    refusingFactionId,
                    currentModel,
                    "Leader");

            if (playerId is PlayerId pid && pid == refusingFactionId) {
                var textTemplate = DynamicTextTemplate.Parsed(
                        $"We have refused to accept the ransom from {DynamicTextTemplate.Placeholder(Placeholders.OfferingFaction)} for {DynamicTextTemplate.Placeholder(Placeholders.RansomedHero)}.\n\n");
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ransom Rejected",
                        textTemplate: textTemplate,
                        placeholders: new List<
                                DynamicPlaceholder> { ransomedHeroPlaceholder, offeringFactionPlaceholder },
                        llmId: notification.LlmId,
                        provinceIds: new List<ProvinceId>(),
                        displayedHeroes: new List<HeroView> { ransomedHero, offeringFactionHead });
            } else if (playerId.HasValue && playerId.Value == offeringFactionId) {
                var textTemplate = DynamicTextTemplate.Parsed(
                        $"{DynamicTextTemplate.Placeholder(Placeholders.RefusingFaction)} has refused to accept our ransom for {DynamicTextTemplate.Placeholder(Placeholders.RansomedHero)}.\n\n");
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ransom Rejected",
                        textTemplate: textTemplate,
                        placeholders: new List<
                                DynamicPlaceholder> { refusingFactionPlaceholder, ransomedHeroPlaceholder },
                        llmId: notification.LlmId,
                        provinceIds: new List<ProvinceId>(),
                        displayedHeroes: new List<HeroView> { refusingFactionHead, ransomedHero });
            } else {
                var textTemplate = DynamicTextTemplate.Parsed(
                        $"{DynamicTextTemplate.Placeholder(Placeholders.RefusingFaction)} refused to accept a ransom to {DynamicTextTemplate.Placeholder(Placeholders.OfferingFaction)} for the return of {DynamicTextTemplate.Placeholder(Placeholders.RansomedHero)}.\n\n");
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ransom Rejected",
                        textTemplate: textTemplate,
                        placeholders: new List<
                                DynamicPlaceholder> { refusingFactionPlaceholder, offeringFactionPlaceholder, ransomedHeroPlaceholder },
                        llmId: notification.LlmId,
                        provinceIds: new List<ProvinceId>(),
                        displayedHeroes: new List<HeroView> {
                            refusingFactionHead,
                            offeringFactionHead,
                            ransomedHero
                        });
            }
        }
    }
}
