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

namespace eagle.Notifications {
    public static class ProvinceConqueredNotificationGenerator {
        public static readonly NotificationGenerator Generator = GenerateNotification;

        private static IEnumerable<Notification> GenerateNotification(
                ActionResultView actionResultView,
                IGameModel currentModel) {
            var playerId = currentModel.PlayerId;
            var provinceBefore = currentModel.Provinces[actionResultView.Province.Value];
            if (provinceBefore.RulingFactionId.HasValue) {
                var winner = currentModel.MaybeDestroyedFaction(actionResultView.Player.Value);
                var loser =
                        currentModel.MaybeDestroyedFaction(provinceBefore.RulingFactionId.Value);

                var winnerHead = currentModel.Heroes[winner.FactionHeadId];
                var loserHead = currentModel.Heroes[loser.FactionHeadId];

                if (!playerId.HasValue ||
                    (playerId.Value != winner.Id && playerId.Value != loser.Id)) {
                    yield return new Notification(
                            title: "Province Conquered",
                            text: $"{winner.Name} has conquered {provinceBefore.Name} from {loser.Name}.",
                            singleProvinceId: actionResultView.Province.Value,displayedHeroes: new List<HeroView> { winnerHead,loserHead });
                }
            }
        }
    }
}