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 class Placeholders {
            public static readonly DynamicTextPlaceholderName ActingFactionLeader =
                    new(nameof(ActingFactionLeader));
            public static readonly DynamicTextPlaceholderName ReturnedHero =
                    new(nameof(ReturnedHero));
        }

        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";
                var textTemplate = DynamicTextTemplate.Parsed(
                        $"{DynamicTextTemplate.Placeholder(Placeholders.ActingFactionLeader)} has returned {DynamicTextTemplate.Placeholder(Placeholders.ReturnedHero)} to us!");

                var heroPlaceholders = new List<DynamicPlaceholder> {
                    DynamicPlaceholder.HeroName(Placeholders.ReturnedHero, hero, "our hero"),
                    DynamicPlaceholder.FactionLeaderName(
                            Placeholders.ActingFactionLeader,
                            actingFaction,
                            currentModel,
                            actingFaction.Name)
                };

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