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

namespace eagle.Notifications.ARNNotifications {
    public static class ProvinceHeldDetailsNotificationGenerator {
        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.ProvinceHeldDetails;
            var provinceBefore = currentModel.Provinces[notif.ProvinceId];
            var winner = currentModel.MaybeDestroyedFaction(notif.DefendingFactionId);

            string losersString = DisplayNames.ListString(
                    notif.AttackingFactionIds.Select(fid => currentModel.FactionName(fid))
                            .ToList());

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

            // Only show the first losing attacker
            var loserFaction =
                    currentModel.MaybeDestroyedFaction(notif.AttackingFactionIds.First());
            var loserFactionHead = currentModel.Heroes[loserFaction.FactionHeadId];

            if (!playerId.HasValue || (playerId.Value != winner.Id)) {
                yield return new Notification(
                        title: "Province Held",
                        text: $"{winner.Name} has successfully defended province {provinceBefore.Name} from {losersString}.",
                        singleProvinceId: notif.ProvinceId,
                        displayedHeroes: new List<HeroView> { winnerHead,loserFactionHead });
            }
        }
    }
}
