using ImageToolKits;
using OpenCvSharp;
using ProductionControl.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProductionControl.UI
{
    public partial class FrmGetPosByPic : Form
    {
        List<ImageBox.BaseDrawParam> pickARoi = new List<ImageBox.BaseDrawParam>();
        Mat Img;

        private TextBox[] tbArray = new TextBox[14];
        string _Path;
        double[] _Points;
        public FrmGetPosByPic(string  map, double[] pointlist)
        {
            _Path = map;
            _Points = pointlist;
            pickARoi.Clear();
            InitializeComponent();
            //lbPoslist.Items.Clear();
            this.imageBox1.SetLowLevelMode(14);
            imageBox1.DrawOverAllEventHandler += imgBox_DrawOverAllEventHandler;

            tbArray[0] = this.textBox1;
            tbArray[1] = this.textBox2;
            tbArray[2] = this.textBox3;
            tbArray[3] = this.textBox4;
            tbArray[4] = this.textBox5;

            tbArray[5] = this.textBox6;
            tbArray[6] = this.textBox7;
            tbArray[7] = this.textBox8;
            tbArray[8] = this.textBox9;
            tbArray[9] = this.textBox10;
            tbArray[10] = this.textBox11;
            tbArray[11] = this.textBox12;
            tbArray[12] = this.textBox13;
            tbArray[13] = this.textBox14;
        }

        public string GetMapPath()
        { return _Path; }
        public double[] GetPoints()
        { 
            return _Points; }
        
        void imgBox_DrawOverAllEventHandler(object sender, List<ImageBox.BaseDrawParam> e)
        {
            if (e.Count > 0)
            {
                imageBox1.RefreshWindow(imageBox1.Image, e, ImageBox.ImageModeEnum.Part);
                this.pickARoi = imageBox1.GetLowPoints();
                for (int i = 0; i < 14; i++)
                {
                    if (i < e.Count)
                        tbArray[i].Text = "X:" + (e[i] as ImageBox.Point).Column.ToString("0.000") + ", Y:" + (e[i] as ImageBox.Point).Row.ToString("0.000");
                    else
                        tbArray[i].Text = "";
                }
            }
        }
        private void FrmGetPosByPic_Load(object sender, EventArgs e)
        {
            List<ImageBox.BaseDrawParam> roi = new List<ImageBox.BaseDrawParam>();

            if (File.Exists(_Path))
            {
                Img = new Mat(_Path);
                if ((_Points[0] != 0) && (_Points[2] != 0) && (_Points[4] != 0) && (_Points[6] != 0) && (_Points[8] != 0))
                {
                    //this.imageBox1.RefreshWindow(Img);
                    if (_Points.Length == (14 * 2))
                    {
                        for (int i = 0; i < 14; i++)
                        {
                            //string pos = sizeLibProp.GetPointList[5 + 2 * i].ToString() + "," + sizeLibProp.GetPointList[5 + 2 * i +1].ToString();
                            //lbPoslist.Items.Add(pos);
                            ImageBox.BaseDrawParam point = new ImageBox.Point((float)_Points[2 * i + 1], (float)_Points[2 * i]);

                            tbArray[i].Text = "X:" + _Points[2 * i + 0].ToString("0.000") + ", Y:" + _Points[2 * i + 1].ToString("0.000");
                            roi.Add(point);
                            pickARoi.Add(point);
                        }
                        imageBox1.SetDrawPrms(pickARoi);
                        //imageBox1.RefreshWindow(Img, roi);
                        //imageBox1.Refresh();
                    }
                    else if (_Points.Length > 0)
                        MessageBox.Show("点位数据错误！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                //else
                //{
                    //imageBox1.RefreshWindow(Img, roi);
                    //imageBox1.Refresh();
                //}
            }
            else
            {
                string maop_path = FileUtil.selectFile();
                if (File.Exists(maop_path))
                {
                    _Path = maop_path;
                    Img = new Mat(maop_path);
                    this.imageBox1.RefreshWindow(Img);
                }
                else
                {
                    MessageBox.Show("无图纸图片加载！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            imageBox1.RefreshWindow(Img, roi);
            imageBox1.Refresh();
            imageBox1.DisplayROIs(roi);
        }



        private void tsbtnClear_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否清空所有点位?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                pickARoi.Clear();
                this.imageBox1.ClearLowPoints();
                this.imageBox1.RefreshWindow(Img);
                for (int i = 0; i < 14; i++)
                {
                    tbArray[i].Text = "";
                }
            }
        }

        private void tsbtnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsbtnSave_Click(object sender, EventArgs e)
        {
            pickARoi = this.imageBox1.GetLowPoints();
            if (pickARoi.Count == 14)
            {
                _Points = new double[14 * 2];

                for (int i = 0; i < 14; i++)
                {
                    _Points[2 * i] = Math.Round((pickARoi[i] as ImageBox.Point).Column,3);
                    _Points[2 * i + 1] = Math.Round((pickARoi[i] as ImageBox.Point).Row,3);
                }
              
                this.DialogResult = DialogResult.OK;
            }          
            else if (pickARoi.Count >0)
                MessageBox.Show("点位设置错误，点数不对！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                _Points = new double[14 * 2];

                for (int i = 0; i < 14; i++)
                {
                    _Points[2 * i] = 0;
                    _Points[2 * i + 1] = 0;
                }
            }
        }

        private void tsbtnDelectPos_Click(object sender, EventArgs e)
        {
            //this.imageBox1.DelectOntPoint();
            //this.pickARoi.RemoveAt(pickARoi.Count - 1);
        }
    }
}
