using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VIZ.Framework.Common;
using VIZ.Framework.Core;

namespace VIZ.Framework.WpfTest
{
    /// <summary>
    /// OpenCVVideoTest.xaml 的交互逻辑
    /// </summary>
    public partial class OpenCVVideoTest : UserControl
    {
        public OpenCVVideoTest()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            // 附加插件
            TrackingBoxPlugin trackingBoxPlugin = new TrackingBoxPlugin(this.video);
            trackingBoxPlugin.TrackingBoxClick += TrackingBoxPlugin_TrackingBoxClick;

            SelectionBoxPlugin selectionBoxPlugin = new SelectionBoxPlugin(this.video);
            selectionBoxPlugin.Selected += SelectionBoxPlugin_Selected;

            ClipBoxPlugin clipBoxPlugin = new ClipBoxPlugin(this.video);

            SideCheckPolygonPlugin sideCheckPolygonPlugin = new SideCheckPolygonPlugin(this.video);
            sideCheckPolygonPlugin.Click += SideCheckPolygonPlugin_Click;

            this.video.AttachPlugin(trackingBoxPlugin);
            this.video.AttachPlugin(selectionBoxPlugin);
            this.video.AttachPlugin(clipBoxPlugin);
            this.video.AttachPlugin(sideCheckPolygonPlugin);

            // 接入OpenCV插件
            OpenCVStreamOption option = new OpenCVStreamOption();
            OpenCVStream stream = new OpenCVStream(option);
            stream.ExecuteVideoFrame += Stream_ExecuteVideoFrame;
            stream.ChangeUri(@"E:\视频\1.mp4");
            stream.Play();

            // 更新矩形框信息
            //List<TrackingBoxInfo> list = new List<TrackingBoxInfo>();
            //list.Add(new TrackingBoxInfo
            //{
            //    DrawingBorderColor = SharpDxColorHelper.FromString("#FFFF0000"),
            //    ID = "1",
            //    DrawingBorderWidth = 4,
            //    SrcRect = new SharpDX.Mathematics.Interop.RawRectangleF(0, 0, 200, 120)
            //});
            //list.Add(new TrackingBoxInfo
            //{
            //    DrawingBorderColor = SharpDxColorHelper.FromString("#FFFF0000"),
            //    ID = "1",
            //    DrawingBorderWidth = 4,
            //    SrcRect = new SharpDX.Mathematics.Interop.RawRectangleF(400, 300, 600, 420)
            //});
            //this.video.UpdateTrackingBox(list);

            // 裁剪
            //ClipBoxInfo clipBoxInfo = new ClipBoxInfo();
            //clipBoxInfo.DrawingBorderWidth = 8;
            //clipBoxInfo.DrawingBorderColor = SharpDxColorHelper.FromString("#AA0000FF");
            //clipBoxInfo.MaskColor = SharpDxColorHelper.FromString("#66000000");
            //clipBoxInfo.SrcRect = new SharpDX.Mathematics.Interop.RawRectangleF(200, 0, 800, 1080);

            //this.video.UpdateClipBox(clipBoxInfo);

            // 边线检测
            List<SideCheckPolygonInfo> sideCheckPolygonInfos = new List<SideCheckPolygonInfo>();
            sideCheckPolygonInfos.Add(new SideCheckPolygonInfo
            {
                FillColor = SharpDxColorHelper.FromString("#66FF000000"),
                SrcPoints = new List<SharpDX.Mathematics.Interop.RawVector2>()
                {
                    new SharpDX.Mathematics.Interop.RawVector2(100, 100),
                    new SharpDX.Mathematics.Interop.RawVector2(300, 100),
                    new SharpDX.Mathematics.Interop.RawVector2(500, 300),
                    new SharpDX.Mathematics.Interop.RawVector2(600, 500),
                    new SharpDX.Mathematics.Interop.RawVector2(100, 100)
                }
            });

            this.video.UpdateSideCheckPolygon(sideCheckPolygonInfos);
        }

        private void SideCheckPolygonPlugin_Click(object sender, SideCheckPolygonClickEventArgs e)
        {
            Debug.WriteLine($"SideCheckPolygonPlugin_Click: [{e.MousePoint.X}, {e.MousePoint.Y}] [{e.SrcPoint.X}, {e.SrcPoint.Y}]");
        }

        private void Stream_ExecuteVideoFrame(object sender, VideoFrameEventArgs e)
        {
            this.video.UpdateVideoFrame(e.Frame);
        }

        private void SelectionBoxPlugin_Selected(object sender, SelectionBoxSelectedEventArgs e)
        {
            Rect rect = e.DrawingRect.ToRect();
            Debug.WriteLine($"Slect: [{rect.X}, {rect.Y}, {rect.Width}, {rect.Height}]");
        }

        private void TrackingBoxPlugin_TrackingBoxClick(object sender, TrackingBoxClickEventArgs e)
        {
            if (e.HitTrackingBoxInfo == null)
                return;

            Rect rect = e.HitTrackingBoxInfo.SrcRect.ToRect();
            Debug.WriteLine($"Click: [{rect.X}, {rect.Y}, {rect.Width}, {rect.Height}]");
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            OpenCvSharp.Mat mat = this.video.RenderVideoToMat(new List<string>
            {
                VideoControlPluginNames.TrackingBox,
                VideoControlPluginNames.SelectionBox,
                VideoControlPluginNames.ClipBox
            });

            mat.SaveImage(@"e:\1.jpg");

            mat.Dispose();
        }

        private void Button_Click2(object sender, RoutedEventArgs e)
        {
            this.video.ClearClipBox();
        }
    }
}
