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

namespace eagle.Notifications {
    using FactionId = Int32;

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

        private static IEnumerable<Notification> GenerateNotification(
                ActionResultView actionResultView,
                IGameModel currentModel) {
            var cp = actionResultView.GameStateDiff.ChangedProvinces.First(
                    pvd => pvd.NewFullInfo != null &&
                           pvd.NewFullInfo.AddedUnaffiliatedHeroes.Any());
            if (currentModel.Provinces[cp.Id].RulingFactionId is FactionId fid &&
                fid == currentModel.PlayerId) {
                var ch = cp.NewFullInfo.AddedUnaffiliatedHeroes.First();
                var hero = currentModel.Heroes[ch.HeroId];
                var province = currentModel.Provinces[cp.Id];

                string noteTitle = "Prisoner Escaped";
                string textTemplate = $"{{EscapedHero}} has escaped from {province.Name}!";

                var heroPlaceholders =
                        new Dictionary<string, (string nameTextId, string fallback)> {
                            { "EscapedHero", (hero.NameTextId, "A prisoner") }
                        };

                yield return new DynamicTextNotification(
                        title: noteTitle,
                        textTemplate: textTemplate,
                        heroPlaceholders: heroPlaceholders,
                        singleProvinceId: cp.Id,
                        displayedHeroes: new List<HeroView> { hero });
            }
        }
    }
}