
using System.ComponentModel;
using MaterialSkin.Controls;
using MaterialSkin.Core.Controls;
using OpenCvSharp.Extensions;
using VisionBuilder.UI.Common;
using VisionBuilder.UI.Common.ViewModel;
using VisionBuilder.UI.Common.ViewModel.Interfaces.UI;

namespace Inspectron.HawkEye.View
{
    public partial class ImagePreview : MaterialForm
    {
        private ErrorPreviewVM _previewVm;

        public void SetViewModel(ErrorPreviewVM errorPreviewVm)
        {
            _previewVm = errorPreviewVm;

            _previewVm.PropertyChanged += PreviewVmOnPropertyChanged;
            btnLearnThis.Visible = _previewVm.LearnButtonVisible;
            btnPrev.Enabled = _previewVm.IsPreviousImageEnabled;
            btnNext.Enabled = _previewVm.IsNextImageEnabled;
            pictureBox1.Image = _previewVm.ImageAnalysis?.ToBitmap();
            lblErrName.Text = _previewVm.ImageName;
        }

        private void PreviewVmOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case nameof(ErrorPreviewVM.ImageAnalysis):
                    pictureBox1.Image = _previewVm.ImageAnalysis?.ToBitmap();
                    break;
                case nameof(ErrorPreviewVM.LearnButtonVisible):
                    btnLearnThis.Visible = _previewVm.LearnButtonVisible;
                    break;
                case nameof(ErrorPreviewVM.IsPreviousImageEnabled):
                    btnPrev.Enabled = _previewVm.IsPreviousImageEnabled;
                    break;
                case nameof(ErrorPreviewVM.IsNextImageEnabled):
                    btnNext.Enabled = _previewVm.IsNextImageEnabled;
                    break;
                case nameof(ErrorPreviewVM.ImageName):
                    this.lblErrName.Text = _previewVm.ImageName;
                    break;
                case nameof(ErrorPreviewVM.OriginalView):
                    btnSwitchView.Text = _previewVm.OriginalView ? "Show Analysis" : "Show Original";
                    break;
            }
        }

        public ImagePreview()
        {
            InitializeComponent();
        }

        private void materialRaisedButton1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnLearnThis_Click(object sender, EventArgs e)
        {
            _previewVm.Learn();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            _previewVm.PreviousImage();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            _previewVm.NextImage();
        }

        private void btnSwitchView_Click(object sender, EventArgs e)
        {
            _previewVm.SwitchView();
        }
    }
}
