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

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

    public static class PrisonerReturnedNotificationGenerator {
        public static readonly NotificationGenerator Generator = GenerateNotification;

        private static IEnumerable<Notification> GenerateNotification(
                ActionResultView actionResultView,
                IGameModel currentModel) {
            var ch = actionResultView.GameStateDiff.ChangedHeroes[0];
            if (ch.NewFactionId is FactionId fid && fid == currentModel.PlayerId) {
                var actingFaction = currentModel.ActiveFactions[actionResultView.Player.Value];
                var actingFactionLeader = currentModel.Heroes[actingFaction.FactionHeadId];
                var hero = currentModel.Heroes[ch.Id];

                const string noteTitle = "Prisoner Returned";
                string textTemplate =
                        $"{{ActingFactionLeader}} has returned {{ReturnedHero}} to us!";

                var heroPlaceholders =
                        new Dictionary<string, (string nameTextId, string fallback)> {
                            { "ReturnedHero", (hero.NameTextId, "our hero") },
                            { "ActingFactionLeader",
                              (currentModel.Heroes[actingFaction.FactionHeadId].NameTextId,
                               actingFaction.Name) }
                        };

                yield return new DynamicTextNotification(
                        title: noteTitle,
                        textTemplate: textTemplate,
                        heroPlaceholders: heroPlaceholders,
                        provinceIds: new List<ProvinceId>(),
                        displayedHeroes: new List<HeroView> { actingFactionLeader, hero });
            }
        }
    }
}