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

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

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

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

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

            var acceptingFactionHeadId =
                    currentModel.MaybeDestroyedFaction(acceptingFactionId).FactionHeadId;
            var acceptingFactionHead = currentModel.Heroes[acceptingFactionHeadId];

            if (playerId.HasValue && playerId.Value == acceptingFactionId) {
                yield return Notification.StreamingNotification(
                                    title: "Truce Accepted",
                                    prefixedText:
                                    $"You have accepted a treaty with {currentModel.FactionName(offeringFactionId)}.\n\n",
                                    llmId: notification.LlmId,
                                    provinceIds: affectedProvinces,
                                    displayedHeroes: new List<HeroView> {  offeringFactionHead, ambassador});
            } else if (playerId.HasValue && playerId.Value == offeringFactionId) {
                yield return Notification.StreamingNotification(
                                    title: "Truce Accepted",
                                    prefixedText:
                                    $"{currentModel.FactionName(acceptingFactionId)} has accepted your treaty offer.\n\n",
                                    llmId: notification.LlmId,
                                    provinceIds: affectedProvinces,
                                    displayedHeroes: new List<HeroView> {  acceptingFactionHead, ambassador});
            } else {
                yield return Notification.StreamingNotification(
                                    title: "Truce Signed",
                                    prefixedText:
                                    $"{currentModel.FactionName(acceptingFactionId)} has signed a truce with {currentModel.FactionName(offeringFactionId)}.\n\n",
                                    llmId: notification.LlmId,
                                    provinceIds: affectedProvinces,
                                    displayedHeroes: new List<HeroView> { acceptingFactionHead, offeringFactionHead, ambassador });
            }
        }
    }
}