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 IEnumerable<Notification> GenerateNotifications(
                Net.Eagle0.Eagle.Common.Notification notification,
                IGameModel currentModel) {
            var playerId = currentModel.PlayerId;
            var llmMessage = notification.LlmMessage;
            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 Dictionary<string, (string nameTextId, string fallback)> {
                { "ImprisonedAmbassador", (imprisonedHero.NameTextId, "the ambassador") }
            };

            if (playerId.HasValue && playerId.Value == imprisoningFactionId) {
                var factionHeadId =
                        currentModel.MaybeDestroyedFaction(offeringFactionId).FactionHeadId;
                var factionHead = currentModel.Heroes[factionHeadId];
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ambassador Imprisoned",
                        textTemplate:
                        $"You have rejected an invitation from {currentModel.FactionName(offeringFactionId)} and imprisoned their ambassador {{ImprisonedAmbassador}}.\n\n",
                        heroPlaceholders: 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];
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ambassador Imprisoned",
                        textTemplate:
                        $"{currentModel.FactionName(imprisoningFactionId)} has imprisoned our ambassador {{ImprisonedAmbassador}}!\n\n",
                        heroPlaceholders: 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];

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