using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DH.Commons.Enums;
using DH.UI.Model.Winform;

namespace XKRS.UI.Device.Winform
{
    public partial class CtrlVisionDisplay : UserControl
    {
        public Canvas CvImage { get; set; }
        public bool IsShowPreTreatedImage { get; set; } = true;
        public bool IsShowDefectImage { get; set; } = true;

        public Bitmap CurrBitmap { get; set; }
        public List<IShapeElement> CurrDetectionResults { get; set; } = new List<IShapeElement>();

        public event Action<bool> OnShowOpConfigMenuStateChanged;

        public CtrlVisionDisplay()
        {
            InitializeComponent();

            CvImage = new Canvas();
            CvImage.IsShowElementList = false;
            CvImage.IsShowROITool = false;
            CvImage.Dock = DockStyle.Fill;

            CvImage.ContextMenuStrip = ctmsVisibleControl;

            plMain.Controls.Add(CvImage);

            InitialToolBarVisible();

            this.Load += ((s, e) =>
              {
                  gBox.Text = DetectionName;
              });
        }

        public string DetectionId { get; set; }

        public string DetectionName { get; set; }

        public int RowIndex { get; set; }

        public int ColIndex { get; set; }

        public void RefreshDetectionResult(Bitmap image, List<IShapeElement> detectionResults)
        {
            try
            {
                if (image == null)
                    return;

                CurrBitmap = image;
                if (detectionResults != null)
                {
                    CurrDetectionResults = new List<IShapeElement>(detectionResults);
                }

                if (CvImage.IsHandleCreated)
                {
                    CvImage.Elements.Clear();
                    if (IsShowPreTreatedImage)
                    {
                        CvImage.LoadImage(image);
                    }
                    else
                    {
                        //加载一张同样大小的 空白图
                        Bitmap newImage = new Bitmap(image.Width, image.Height);
                        using (Graphics grp = Graphics.FromImage(newImage))
                        {
                            grp.FillRectangle(Brushes.White, 0, 0, newImage.Width, newImage.Height);
                        }

                        CvImage.LoadImage(newImage);
                    }

                    if (IsShowDefectImage && detectionResults != null)
                    {
                        detectionResults.ForEach(e => CvImage.Elements.Add(e));
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        #region 图像显示开关

        private void chkShowDefect_CheckedChanged(object sender, EventArgs e)
        {
            IsShowDefectImage = chkShowDefect.Checked;
            RefreshDetectionResult(CurrBitmap, CurrDetectionResults);
        }

        private void chkShowOrigin_CheckedChanged(object sender, EventArgs e)
        {
            IsShowPreTreatedImage = chkShowOrigin.Checked;
            RefreshDetectionResult(CurrBitmap, CurrDetectionResults);
        }
        #endregion

        #region 显示控制
        public bool IsShowOperationBar
        {
            get => tscImageSwitch.Visible;
            set => tsmiShowImageSwitch.Checked = tscImageSwitch.Visible = value;
        }

        public bool IsShowToolBar
        {
            get => CvImage.IsShowToolBar;
            set => tsmiShowToolBar.Checked = CvImage.IsShowToolBar = value;
        }

        public bool IsShowStatusBar
        {
            get => CvImage.IsShowStatusBar;
            set => tsmiShowStatusBar.Checked = CvImage.IsShowStatusBar = value;
        }

        private bool isShowOpConfig = false;
        public bool IsShowOpConfig
        {
            get => tsmiShowOpConfig.Checked;
            set
            {
                if (tsmiShowOpConfig.Checked != value)
                {
                    tsmiShowOpConfig.Checked = value;
                }

                OnShowOpConfigMenuStateChanged?.Invoke(value);
            }
        }

        private void tsmiShowOpConfig_CheckedChanged(object sender, EventArgs e)
        {
            IsShowOpConfig = tsmiShowOpConfig.Checked;
        }

        private void InitialToolBarVisible()
        {
            IsShowOperationBar = IsShowToolBar = IsShowStatusBar = false;
        }

        private void tsmiShowToolBar_CheckedChanged(object sender, EventArgs e)
        {
            IsShowToolBar = tsmiShowToolBar.Checked;
        }

        private void tsmiShowOpBar_CheckedChanged(object sender, EventArgs e)
        {
            IsShowOperationBar = tsmiShowImageSwitch.Checked;
        }

        private void tsmiShowStatusBar_CheckedChanged(object sender, EventArgs e)
        {
            IsShowStatusBar = tsmiShowStatusBar.Checked;
        }
        #endregion


    }
}