using System.ComponentModel;
using System.Reflection;
using Microsoft.AspNetCore.Components;
using MudBlazor;
using VisionBuilder.UI.Blazor.Services;
using VisionBuilder.UI.Common;
using VisionBuilder.UI.Common.Services;
using VisionBuilder.UI.Common.ViewModel;
using VisionBuilder.UI.Common.ViewModel.Interfaces.UI;
using Ninject;

namespace VisionBuilder.UI.Blazor.Uno.Components.Layout;

public partial class MainLayout : IDisposable
{
    [Microsoft.AspNetCore.Components.Inject] private IDialogService DialogService { get; set; } = null!;
    [Microsoft.AspNetCore.Components.Inject] private IKernel Kernel { get; set; } = null!;

    private bool _testMode;
    private bool _showTestModeButton;
    private string _version = "";

    private readonly MudTheme _inspectronTheme = new()
    {
        PaletteLight = new PaletteLight
        {
            Primary = "#d32f2f",
            AppbarBackground = "#37474f"
        },
        LayoutProperties = new LayoutProperties
        {
            DefaultBorderRadius = "0px"
        }
    };

    private readonly MudTheme _testModeTheme = new()
    {
        PaletteLight = new PaletteLight
        {
            Primary = "#2e7d32",
            AppbarBackground = "#2e7d32"
        },
        LayoutProperties = new LayoutProperties
        {
            DefaultBorderRadius = "0px"
        }
    };

    private MudTheme _currentTheme = null!;
    private bool _disposed;

    protected override void OnInitialized()
    {
        _currentTheme = _inspectronTheme;
        _version = $"v{Assembly.GetEntryAssembly()?.GetName().Version?.ToString(3) ?? "0.0.0"}";

        var uiConfig = Kernel.Get<UIConfiguration>();
        _showTestModeButton = uiConfig.ShowTestModeButton;

        MainWindowVm.PropertyChanged += OnPropertyChanged;

        // Wire up dialog services
        if (Kernel.Get<IRecipeSelectionDialogService>() is BlazorRecipeSelectionDialogService recipeService)
            recipeService.SetDialogService(DialogService);
        if (Kernel.Get<IImagePreviewService>() is BlazorImagePreviewService imageService)
            imageService.SetDialogService(DialogService);
        if (Kernel.Get<IPasswordInputService>() is BlazorPasswordInputService passwordService)
            passwordService.SetDialogService(DialogService);
        if (Kernel.Get<ISettingsService>() is BlazorSettingsService settingsService)
            settingsService.SetDialogService(DialogService);
    }

    private async void OnPropertyChanged(object? sender, PropertyChangedEventArgs e)
    {
        if (_disposed) return;
        if (e.PropertyName == nameof(MainWindowVM.TestMode))
        {
            _testMode = MainWindowVm.TestMode;
            _currentTheme = _testMode ? _testModeTheme : _inspectronTheme;
            await InvokeAsync(StateHasChanged);
        }
    }

    private void ToggleTestMode()
    {
        MainWindowVm.ToggleTestModeCommand.Execute(null);
    }

    private async Task OnSettings()
    {
        var uiConfig = Kernel.Get<UIConfiguration>();
        if (uiConfig.ProtectSettingsWithPassword)
        {
            var password = await PasswordInputService.GetPasswordAsync();
            if (password != uiConfig.AdminPassword)
                return;
        }
        MainWindowVm.SettingsCommand.Execute(null);
    }

    public void Dispose()
    {
        _disposed = true;
        MainWindowVm.PropertyChanged -= OnPropertyChanged;
    }
}
