using System.Drawing.Drawing2D;
using Hawkeye.VisionBuilder.Workflow.Datatypes;
using OpenCvSharp;
using OpenCvSharp.Extensions;
using VisionBuilder.UI.Common.Processing;
using Size = System.Drawing.Size;

namespace Hawkeye.VisionBuilder.Features.ImagesStrip
{
    public partial class ImageStripPanel : Form, IImageSource
    {
        

        public ImagesViewModel ImagesViewModel { get; set; }

        public ImageStripPanel()
        {

            InitializeComponent();
            
            ImagesViewModel = new ImagesViewModel(20);
            //ImagesViewModel.CurrentImageChanged += ImagesViewModel_CurrentImageChanged;
            //ImagesViewModel.ImagesAdded += ImagesViewModel_ImageAdded;
            //ImagesViewModel.ImagesRemoved += ImagesViewModel_ImageRemoved;
            virtualizedImageList1.ViewModel=ImagesViewModel;
            virtualizedImageList1.ImageSelected += VirtualizedImageList1_ImageSelected;
        }

        private void VirtualizedImageList1_ImageSelected(object? sender, string e)
        {
            lblSelectedImage.Text = $"{this.ImagesViewModel.CurrentIndex + 1}/{ImagesViewModel.TotalCount}";
            ImageSelected(new HawkeyeImage() { Filename = ImagesViewModel.CurrentImagePath, ImageData = new Mat(ImagesViewModel.CurrentImagePath) });

        }

    


        //private void ImagesViewModel_CurrentImageChanged(object? sender, CurrentImageChangedEventArgs e)
        //{
        //    var path = e.NewImagePath;
        //    for (int i = 0; i < listView1.Items.Count; i++)
        //    {
        //        var item = listView1.Items[i];
        //        var itemPath = (string)item.ImageKey;
        //        if (itemPath == path)
        //        {
        //            SetSelectedImage(i);
        //            ImageSelected(new HawkeyeImage(){Filename = ImagesViewModel.CurrentImagePath, ImageData = new Mat(ImagesViewModel.CurrentImagePath)});
        //            break;
        //        }
        //    }

        //}


        public bool IsExecutingWorkflow
        {
            get => ckbExecuteWorkflow.Checked;
            set => ckbExecuteWorkflow.Checked = value;
        }

        public event Action<Workflow.Datatypes.HawkeyeImage> ImageSelected = delegate { };
   


        

        public void LoadImages(string directory)
        {
            
            //listView1.BeginUpdate();
            ImagesViewModel.LoadFromDirectory(directory);
            //listView1.EndUpdate();
        }

        private void btnToStart_Click(object sender, EventArgs e)
        {
            ImagesViewModel.First();
        }

        public void Prev()
        {
            ImagesViewModel.Previous();

        }
        private void btnPrev_Click(object sender, EventArgs e)
        {
            Prev();
        }

        public void Next()
        {
            ImagesViewModel.Next();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            
            Next();
        }

        private void btnToEnd_Click(object sender, EventArgs e)
        {
            ImagesViewModel.Last();
        }




        private void btnPlay_Click(object sender, EventArgs e)
        {
            Task.Run(Playback);
        }

        private async Task Playback()
        {
            while (btnPlay.Checked)
            {
                Action a = () =>
                {
                    if (ImagesViewModel.TotalCount == 0)
                    {
                        btnPlay.Checked = false;
                        return;
                    }
                    
                    if (ImagesViewModel.CurrentIndex > ImagesViewModel.TotalCount - 1)
                    {
                        if (cbRepeat.Checked)
                        {
                            ImagesViewModel.First();
                        }
                        else
                        {
                            btnPlay.Checked = false;
                            return;
                        }

                    }
                    Next();
                };
                virtualizedImageList1.Invoke(a);
                virtualizedImageList1.Invalidate();
                await Task.Delay(int.Parse((string)ncInterval.Text));
            }
        }


        public Task<Mat> GetImage(CancellationToken token)
        {
            return Task.FromResult(GetImage());
        }

        public Mat GetImage()
        {
            
            return new Mat(ImagesViewModel.CurrentImagePath);
        }

        public string GetImagePath()
        {
            return ImagesViewModel.CurrentImagePath;
        }

        public bool StopOnErrors
        {
            get => ckbStopOnError.Checked;
            set => ckbStopOnError.Checked = value;
        }

        public bool StopOnNoErrors
        {
            get => ckbStopOnNoError.Checked;
            set => ckbStopOnNoError.Checked = value;
        }


        public void InformImageProcessed(bool result)
        {
            if (!result)
            {
                if (ckbStopOnError.Checked)
                {
                    btnPlay.Checked = false;
                }
            }
            else
            {
                if (ckbStopOnNoError.Checked)
                {
                    btnPlay.Checked = false;
                }
            }
        }



        public void SelectImage(string nextFile)
        {
            ImagesViewModel.SelectByPath(nextFile);

        }
    }
}
