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

namespace eagle.Notifications.ARNNotifications {
    public static class BreakAllianceAcceptedNotificationGenerator {
        public static readonly ARNNotificationGenerator Generator = GenerateNotifications;

        private static IEnumerable<Notification> GenerateNotifications(
                Net.Eagle0.Eagle.Common.Notification notification,
                IGameModel currentModel) {
            var playerId = currentModel.PlayerId;
            var llmMessage = notification.LlmMessage;
            var notif = notification.Details.BreakAllianceAcceptedDetails;
            var offeringFactionId = notif.OfferingFactionId;
            var imprisoningFactionId = notif.TargetFactionId;
            var imprisonedHeroId = notif.AmbassadorHeroId;
            var imprisonedHero = currentModel.Heroes[imprisonedHeroId];

            var affectedProvinces = currentModel.ProvincesForFaction(imprisoningFactionId);
            affectedProvinces.AddRange(currentModel.ProvincesForFaction(offeringFactionId));

            var offeringFactionHeadId =
                    currentModel.MaybeDestroyedFaction(offeringFactionId).FactionHeadId;
            var offeringFactionHead = currentModel.Heroes[offeringFactionHeadId];

            var imprisoningFactionHeadId =
                    currentModel.MaybeDestroyedFaction(imprisoningFactionId).FactionHeadId;
            var imprisoningFactionHead = currentModel.Heroes[imprisoningFactionHeadId];

            if (playerId.HasValue && playerId.Value == imprisoningFactionId) {
                yield return Notification.StreamingNotification(
                        title: "Alliance Broken",
                        prefixedText:
                        $"{currentModel.FactionName(offeringFactionId)} broke their alliance with us.\n\n",
                        llmId: notification.LlmId,
                        provinceIds: affectedProvinces,
                        displayedHeroes: new List<HeroView> {  offeringFactionHead, imprisonedHero});
            } else if (playerId.HasValue && playerId.Value == offeringFactionId) {
                yield return Notification.StreamingNotification(
                        title: "Alliance Broken",
                        prefixedText:
                        $"We broke our alliance with {currentModel.FactionName(imprisoningFactionId)}.\n\n",
                        llmId: notification.LlmId,
                        provinceIds: affectedProvinces,
                        displayedHeroes: new List<HeroView> {  imprisoningFactionHead, imprisonedHero});
            } else {
                yield return Notification.StreamingNotification(
                        title: "Alliance Broken",
                        prefixedText:
                        $"{currentModel.FactionName(offeringFactionId)} broke their alliance with {currentModel.FactionName(imprisoningFactionId)}.\n\n",
                        llmId: notification.LlmId,
                        provinceIds: affectedProvinces,
                        displayedHeroes: new List<HeroView> { imprisoningFactionHead, offeringFactionHead, imprisonedHero });
            }
        }
    }
}