// Copyright © Selmo Technology GmbH. All Rights Reserved

using Novus.Shared.Types.IssueHandling;
using SelmoStudio.Hmi.Server.SelmoHmi.Helpers;
using SelmoStudio.Hmi.Server.SelmoHmi.Notification.Model;
using SelmoStudio.Hmi.Server.Shared.Resources;
using NotificationMessage = SelmoStudio.Hmi.Server.SelmoHmi.Notification.Model.NotificationMessage;

namespace SelmoStudio.Hmi.Server.SelmoHmi.Notification.Service
{
    public class HmiNotificationService : INotificationService
    {
        private INotificationImplementation _notificationImplementation;
        private const bool NotificationMessageCloseOnClick = false;

        public HmiNotificationService(INotificationImplementation notificationImplementation)
        {
            _notificationImplementation = notificationImplementation;
        }

        public void Notify(NotificationConfiguration notificationConfiguration)
        {
            var message = CreateNotificationMessage(notificationConfiguration);

            _notificationImplementation.Notify(message);
        }

        public void RemoveNotificationIfExists(string notificationId)
        {
            var message = _notificationImplementation.Messages.FirstOrDefault(m => m.Payload != null &&
                m.Payload.ToString().Equals(notificationId));

            if (message != null)
            {
                _notificationImplementation.Remove(message);
            }
        }

        private NotificationMessage CreateNotificationMessage(NotificationConfiguration notificationConfiguration)
        {
            return new NotificationMessage
            {
                Severity = GetNotificationMessageSeverity(notificationConfiguration.Severity),
                Style = HtmlStyleHelper.GetNotificationMessageStyle(),
                Duration = notificationConfiguration.MessageDuration,
                CloseOnClick = NotificationMessageCloseOnClick,
                Payload = notificationConfiguration.Id,
                DetailContent = GetNotificationMessageContent(notificationConfiguration.Content,
                    notificationConfiguration.Id.ToString(), GetMessage(notificationConfiguration.Severity))
            };
        }

        private string GetMessage(Severity severity)
        {
            if(severity == Severity.Error)
            {
                return HmiResource.ErrorDuringOperation;
            }

            return HmiResource.OperationInProgress;

        }

        private static NotificationSeverity GetNotificationMessageSeverity(Severity severity)
        {
            return severity switch
            {
                Severity.Info => NotificationSeverity.Info,
                Severity.Warning => NotificationSeverity.Warning,
                Severity.Error or Severity.Fatal => NotificationSeverity.Error,
                _ => NotificationSeverity.Info,
            };
        }

        private static NotificationMessage.NotificationRenderFragment GetNotificationMessageContent(Type componentType, string id, string message)
        {
            return new NotificationMessage.NotificationRenderFragment(_ => builder =>
            {
                builder.OpenComponent(0, componentType);
                builder.AddAttribute(1, "Id", id);
                builder.AddAttribute(1, "Message", message);
                builder.CloseComponent();
            });
        }
    }
}