// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using Radzen;
using NotificationMessage = SelmoStudio.Hmi.Server.SelmoHmi.Notification.Model.NotificationMessage;
using NotificationSeverity = SelmoStudio.Hmi.Server.SelmoHmi.Notification.Model.NotificationSeverity;

namespace SelmoStudio.Hmi.Server.SelmoHmi.Notification.Service
{
    public class RadzenNotificationImplementation : INotificationImplementation
    {
        private readonly NotificationService _notificationService;
        private const double DefaultDuration = 0;

        public RadzenNotificationImplementation(NotificationService notificationService)
        {
            _notificationService = notificationService;
        }

        public void Notify(NotificationMessage message)
        {
            var radzenMessage = ConvertToRadzenMessage(message);
            _notificationService.Notify(radzenMessage);
        }

        public IList<NotificationMessage> Messages =>
            _notificationService.Messages.Select(ConvertFromRadzenMessage).ToList();

        public void Remove(NotificationMessage message)
        {
            var radzenMessage = _notificationService.Messages.FirstOrDefault(m => m.Payload?.Equals(message.Payload) == true);
            if (radzenMessage != null)
            {
                _notificationService.Messages.Remove(radzenMessage);
            }
        }

        private Radzen.NotificationMessage ConvertToRadzenMessage(NotificationMessage message)
        {
            return new Radzen.NotificationMessage
            {
                Severity = ConvertSeverity(message.Severity),
                Style = message.Style,
                Duration = message.Duration,
                CloseOnClick = message.CloseOnClick,
                Payload = message.Payload,
                DetailContent = new RenderFragment<NotificationService>(ns => builder =>
                    message.DetailContent?.Invoke(this)(builder))
            };
        }

        private NotificationMessage ConvertFromRadzenMessage(Radzen.NotificationMessage radzenMessage)
        {
            return new NotificationMessage
            {
                Severity = ConvertSeverity(radzenMessage.Severity),
                Style = radzenMessage.Style,
                Duration = GetDuration(radzenMessage.Duration),
                CloseOnClick = radzenMessage.CloseOnClick,
                Payload = radzenMessage.Payload,
                DetailContent = new NotificationMessage.NotificationRenderFragment(_ => builder =>
                    radzenMessage.DetailContent?.Invoke(_notificationService)(builder))
            };
        }

        private static double GetDuration(double? duration)
        {
            return duration ?? DefaultDuration;
        }

        private static Radzen.NotificationSeverity ConvertSeverity(NotificationSeverity severity)
        {
            return severity switch
            {
                NotificationSeverity.Info => Radzen.NotificationSeverity.Info,
                NotificationSeverity.Success => Radzen.NotificationSeverity.Success,
                NotificationSeverity.Warning => Radzen.NotificationSeverity.Warning,
                NotificationSeverity.Error => Radzen.NotificationSeverity.Error,
                _ => Radzen.NotificationSeverity.Info
            };
        }

        private static NotificationSeverity ConvertSeverity(Radzen.NotificationSeverity severity)
        {
            return severity switch
            {
                Radzen.NotificationSeverity.Info => NotificationSeverity.Info,
                Radzen.NotificationSeverity.Success => NotificationSeverity.Success,
                Radzen.NotificationSeverity.Warning => NotificationSeverity.Warning,
                Radzen.NotificationSeverity.Error => NotificationSeverity.Error,
                _ => NotificationSeverity.Info
            };
        }
    }
}