// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using Radzen;
using SelmoStudio.Hmi.Server.SelmoHmi.Dialog.Model;

namespace SelmoStudio.Hmi.Server.SelmoHmi.Dialog.Service
{
    public class RadzenDialogServiceImplementation : IDialogService
    {
        private readonly DialogService _radzenDialogService;

        public RadzenDialogServiceImplementation(DialogService radzenDialogService)
        {
            _radzenDialogService = radzenDialogService;
            _radzenDialogService.OnClose += (result) => OnClose?.Invoke();
        }

        public event Action OnClose;

        public Task OpenAsync<T>(string title, Dictionary<string, object> parameters, IDialogOptions options) where T : ComponentBase
        {
            var radzenOptions = new Radzen.DialogOptions
            {
                Width = options.Width,
                Height = options.Height,
                Resizable = options.Resizable,
                Draggable = options.Draggable,
                AutoFocusFirstElement = options.AutoFocusFirstElement
            };

            return _radzenDialogService.OpenAsync<T>(title, parameters, radzenOptions);
        }

        public void Close()
        {
            _radzenDialogService.Close();
        }
    }
}