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

namespace eagle.Notifications.ARNNotifications {
    public static class TruceAmbassadorImprisonedDetailsNotificationGenerator {
        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.TruceAmbassadorImprisonedDetails;
            var offeringFactionId = notif.OfferingFactionId;
            var imprisoningFactionId = notif.TargetFactionId;
            var imprisonedHeroId = notif.AmbassadorHeroId;
            var imprisonedHero = currentModel.Heroes[imprisonedHeroId];

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

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

            var imprisoningFactionHeadId =
                    currentModel.MaybeDestroyedFaction(imprisoningFactionId).FactionHeadId;
            var imprisoningFactionHead = currentModel.Heroes[imprisoningFactionHeadId];

            var heroPlaceholders = new Dictionary<string, (string nameTextId, string fallback)> {
                { "ImprisonedAmbassador", (imprisonedHero.NameTextId, "the ambassador") }
            };

            if (playerId.HasValue && playerId.Value == imprisoningFactionId) {
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ambassador Imprisoned",
                        textTemplate:
                        $"You have rejected a treaty with {currentModel.FactionName(offeringFactionId)} and imprisoned their ambassador {{ImprisonedAmbassador}}.\n\n",
                        heroPlaceholders: heroPlaceholders,
                        llmId: notification.LlmId,
                        provinceIds: affectedProvinces,
                        displayedHeroes: new List<HeroView> {  offeringFactionHead, imprisonedHero});
            } else if (playerId.HasValue && playerId.Value == offeringFactionId) {
                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> {  imprisoningFactionHead, imprisonedHero});
            } else {
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ambassador Imprisoned",
                        textTemplate:
                        $"{currentModel.FactionName(imprisoningFactionId)} has rejected a truce offer from {currentModel.FactionName(offeringFactionId)} and imprisoned their ambassador {{ImprisonedAmbassador}}.\n\n",
                        heroPlaceholders: heroPlaceholders,
                        llmId: notification.LlmId,
                        provinceIds: affectedProvinces,
                        displayedHeroes: new List<HeroView> { imprisoningFactionHead, offeringFactionHead, imprisonedHero });
            }
        }
    }
}