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

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

    public static class RansomPaidDetailsNotificationGenerator {
        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.RansomPaidDetails;
            var paidToFactionId = notif.RansomPaidToFactionId;
            var paidByFactionId = notif.RansomPaidByFactionId;
            var acceptingFactionHead =
                    currentModel.Heroes[currentModel.MaybeDestroyedFaction(paidToFactionId)
                                                .FactionHeadId];

            var offeringFactionHead =
                    currentModel.Heroes[currentModel.MaybeDestroyedFaction(paidByFactionId)
                                                .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.HasValue && playerId.Value == paidToFactionId) {
                // no notification
            } else if (playerId.HasValue && playerId.Value == paidByFactionId) {
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ransom Accepted",
                        textTemplate: $"{currentModel.FactionName(paidToFactionId)} has accepted our ransom for {{RansomedHero}}.\n\n",
                        heroPlaceholders: heroPlaceholders,
                        llmId: notification.LlmId,
                        provinceIds: new List<ProvinceId>(),
                        displayedHeroes: new List<HeroView> { acceptingFactionHead, ransomedHero });
            } else {
                yield return DynamicTextNotification.StreamingDynamicNotification(
                        title: "Ransom Paid",
                        textTemplate: $"{currentModel.FactionName(paidByFactionId)} paid a ransom to {currentModel.FactionName(paidToFactionId)} for the return of {{RansomedHero}}.\n\n",
                        heroPlaceholders: heroPlaceholders,
                        llmId: notification.LlmId,
                        provinceIds: new List<ProvinceId>(),
                        displayedHeroes: new List<HeroView> { acceptingFactionHead, offeringFactionHead, ransomedHero });
            }
        }
    }
}