using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using OpenCvSharp;
using VisionBuilder.UI.Common.Commands;
using VisionBuilder.UI.Common.Commands.Interfaces;
using VisionBuilder.UI.Common.Services;
using VisionBuilder.UI.Common.ViewModel.Classes;
using VisionBuilder.UI.Common.ViewModel.Interfaces.UI;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace VisionBuilder.UI.Common.ViewModel;

public partial class ErrorPreviewVM:ObservableObject
{
    
    private ErrorsVM _errorsVm;
    private ErrorData _errorData;

    private readonly ILearningTool _learningTool;
    private readonly IPasswordInputService _passwordInputService;


    [ObservableProperty]
    private string _recipeName;

    private readonly UIConfiguration _uiConfiguration;

    [ObservableProperty]
    private bool _learnButtonVisible;

    [ObservableProperty]
    private bool _isNextImageEnabled;

    [ObservableProperty]
    private bool _isPreviousImageEnabled;

    [ObservableProperty]
    private string _imageName;

    [ObservableProperty]
    private Mat _imageAnalysis;

    [ObservableProperty]
    private bool _originalView = false;

    public ErrorPreviewVM(ErrorsVM errorsVm, ErrorData errorData, string recipeName, UIConfiguration uiConfiguration, ILearningTool learningTool, IPasswordInputService passwordInputService)
    {
        _errorsVm = errorsVm;
        _errorData = errorData;
        _recipeName = recipeName;
        _uiConfiguration = uiConfiguration;
        _learningTool = learningTool;
        _passwordInputService = passwordInputService;

        UpdateData();
    }




    [RelayCommand(CanExecute = nameof(IsNextImageEnabled))]
    public void NextImage()
    {
        var imageIndex = _errorsVm.Errors.IndexOf(_errorData);
        if (imageIndex >= _errorsVm.Errors.Count) return;
        imageIndex++;
        _errorData = _errorsVm.Errors[imageIndex];
        UpdateData();
    }

    [RelayCommand(CanExecute = nameof(IsPreviousImageEnabled))]
    public void PreviousImage()
    {
        var imageIndex = _errorsVm.Errors.IndexOf(_errorData);
        if (imageIndex <= 0) return;
        imageIndex--;
        _errorData = _errorsVm.Errors[imageIndex];
        UpdateData();


    }

    [RelayCommand]
    public void SwitchView()
    {
        if (OriginalView)
        {
            ImageAnalysis = _errorData.ImageAnalysis;
            OriginalView = false;
        }
        else
        {
            ImageAnalysis = _errorData.ImageOriginal;
            OriginalView = true;
        }
    }


    private void UpdateData()
    {
        var imageIndex = _errorsVm.Errors.IndexOf(_errorData);

        if (imageIndex < 0)
        {
            IsNextImageEnabled = false;
            IsPreviousImageEnabled = false;
            ImageName = string.Empty;
            ImageAnalysis = null;
            return;
        }
        OriginalView= false;
        IsNextImageEnabled = imageIndex < _errorsVm.Errors.Count - 1;
        IsPreviousImageEnabled = imageIndex > 0;
        ImageName = _errorData.Title;
        ImageAnalysis = _errorData.ImageAnalysis;
        LearnButtonVisible = _learningTool.IsLearningEnabled(_errorData.RecipeName);
    }

    [RelayCommand(CanExecute = nameof(LearnButtonVisible))]
    public async Task Learn()
    {
        if (!string.IsNullOrEmpty(_uiConfiguration.AdminPassword))
        {
            var passwordOk = await _passwordInputService.GetPasswordAsync() == _uiConfiguration.AdminPassword;
            if (!passwordOk) return;
        }
        _learningTool.Learn(_errorData.RecipeName, _errorData.ImageOriginal);
    }

 
}