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

namespace eagle.Notifications.ARNNotifications {
    public static class InvitationAmbassadorImprisonedDetailsNotificationGenerator {
        public static readonly ARNNotificationGenerator Generator = GenerateNotifications;

        private static class Placeholders {
            public static readonly DynamicTextPlaceholderName ImprisonedAmbassador =
                    new(nameof(ImprisonedAmbassador));
        }

        private static IEnumerable<Notification> GenerateNotifications(
                Net.Eagle0.Eagle.Common.Notification notification,
                IGameModel currentModel) {
            var playerId = currentModel.PlayerId;
            var notif = notification.Details.InvitationAmbassadorImprisonedDetails;
            var offeringFactionId = notif.OfferingFactionId;
            var imprisoningFactionId = notif.InvitedFactionId;
            var imprisonedHeroId = notif.AmbassadorHeroId;
            var imprisonedHero = currentModel.Heroes[imprisonedHeroId];

            var affectedProvinces = currentModel.ProvincesForFaction(imprisoningFactionId);
            affectedProvinces.AddRange(currentModel.ProvincesForFaction(offeringFactionId));

            var heroPlaceholders = new List<DynamicPlaceholder> { DynamicPlaceholder.HeroName(
                    Placeholders.ImprisonedAmbassador,
                    imprisonedHero,
                    "the ambassador") };

            if (playerId.HasValue && playerId.Value == imprisoningFactionId) {
                var factionHeadId =
                        currentModel.MaybeDestroyedFaction(offeringFactionId).FactionHeadId;
                var factionHead = currentModel.Heroes[factionHeadId];
                var textTemplate = DynamicTextTemplate.Parsed(
                        $"You have rejected an invitation from {currentModel.FactionName(offeringFactionId)} and imprisoned their ambassador {DynamicTextTemplate.Placeholder(Placeholders.ImprisonedAmbassador)}.\n\n");
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ambassador Imprisoned",
                        textTemplate: textTemplate,
                        placeholders: heroPlaceholders,
                        llmId: notification.LlmId,
                        provinceIds: affectedProvinces,
                        displayedHeroes: new List<HeroView> { factionHead });
            } else if (playerId.HasValue && playerId.Value == offeringFactionId) {
                var factionHeadId =
                        currentModel.MaybeDestroyedFaction(imprisoningFactionId).FactionHeadId;
                var factionHead = currentModel.Heroes[factionHeadId];
                var textTemplate = DynamicTextTemplate.Parsed(
                        $"{currentModel.FactionName(imprisoningFactionId)} has imprisoned our ambassador {DynamicTextTemplate.Placeholder(Placeholders.ImprisonedAmbassador)}!\n\n");
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ambassador Imprisoned",
                        textTemplate: textTemplate,
                        placeholders: heroPlaceholders,
                        llmId: notification.LlmId,
                        provinceIds: affectedProvinces,
                        displayedHeroes: new List<HeroView> { factionHead });
            } else {
                var factionHeadId1 =
                        currentModel.MaybeDestroyedFaction(imprisoningFactionId).FactionHeadId;
                var factionHead1 = currentModel.Heroes[factionHeadId1];
                var factionHeadId2 =
                        currentModel.MaybeDestroyedFaction(offeringFactionId).FactionHeadId;
                var factionHead2 = currentModel.Heroes[factionHeadId2];

                var textTemplate = DynamicTextTemplate.Parsed(
                        $"{currentModel.FactionName(imprisoningFactionId)} has rejected an invitation from {currentModel.FactionName(offeringFactionId)} and imprisoned their ambassador {DynamicTextTemplate.Placeholder(Placeholders.ImprisonedAmbassador)}.\n\n");
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ambassador Imprisoned",
                        textTemplate: textTemplate,
                        placeholders: heroPlaceholders,
                        llmId: notification.LlmId,
                        provinceIds: affectedProvinces,
                        displayedHeroes: new List<HeroView> { factionHead1, factionHead2 });
            }
        }
    }
}
