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

namespace eagle.Notifications.ARNNotifications {
    public static class AllianceRejectedDetailsNotificationGenerator {
        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.AllianceRejectedDetails;
            var offeringFactionId = notif.OfferingFactionId;
            var rejectingFactionId = notif.TargetFactionId;
            var llmMessage = notification.LlmMessage;

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

            var ambassador = currentModel.Heroes[notif.AmbassadorHeroId];

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

            var rejectingFactionHeadId =
                    currentModel.MaybeDestroyedFaction(rejectingFactionId).FactionHeadId;
            var rejectingFactionHead = currentModel.Heroes[rejectingFactionHeadId];

            if (playerId.HasValue && playerId.Value == rejectingFactionId) {
                // Player is rejecting a treaty
                yield return Notification.StreamingNotification(
                                    title: "Alliance Rejected",
                                    prefixedText:
                                    $"You have rejected an alliance with {currentModel.FactionName(offeringFactionId)}.\n\n",
                                    llmId: notification.LlmId,
                                    provinceIds: affectedProvinces,
                                    displayedHeroes: new List<HeroView> {  offeringFactionHead, ambassador});
            } else if (playerId.HasValue && playerId.Value == offeringFactionId) {
                // Player's treaty offer was rejected
                yield return Notification.StreamingNotification(
                                    title: "Alliance Rejected",
                                    prefixedText:
                                    $"{currentModel.FactionName(rejectingFactionId)} has rejected your alliance proposal.\n\n",
                                    llmId: notification.LlmId,
                                    provinceIds: affectedProvinces,
                                    displayedHeroes: new List<HeroView> {  rejectingFactionHead, ambassador});
            } else {
                yield return Notification.StreamingNotification(
                                    title: "Alliance Rejected",
                                    prefixedText:
                                    $"{currentModel.FactionName(rejectingFactionId)} refused to form an alliance with {currentModel.FactionName(offeringFactionId)}.\n\n",
                                    llmId: notification.LlmId,
                                    provinceIds: affectedProvinces,
                                    displayedHeroes: new List<HeroView> { rejectingFactionHead, offeringFactionHead, ambassador });
            }
        }
    }
}