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 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 heroPlaceholders = new Dictionary<string, (string nameTextId, string fallback)> {
                { "RansomedHero", (ransomedHero.NameTextId, "the hero") }
            };

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