using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using SmartSEMControl;
using MeasureData;
using System.Threading;

namespace HOZProject.UserControls
{
    public partial class FormExportPoints : Form
    {
        //全局只有一个fatorySEM
        static FactoryHardware factorySEM = FactoryHardware.Instance;
        ISEMControl iSEM = factorySEM.ISEM;

        public List<CutHole> ListCutHole = new List<CutHole>();

        float[] pts = new float[6];
        float ptbsx = 0;
        float ptbsy = 0;

        public FormExportPoints()
        {
            InitializeComponent();
            pts[0] = 0.001f;
            pts[1] = 0.001f;
            pts[2] = 0.001f;
            pts[3] = 0.001f;
            pts[4] = 0.001f;
            pts[5] = 0.001f;
            ptbsx = 0.002f;
            ptbsy = 0.003f;
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            //pts = iSEM.GetStagePosition();
            lblX.Text = pts[0].ToString("0.000");
            lblY.Text = pts[1].ToString("0.000");
            lblZ.Text = pts[2].ToString("0.000");
            lblT.Text = pts[3].ToString("0.000");
            lblR.Text = pts[4].ToString("0.000");
            lblM.Text = pts[5].ToString("0.000");
            //Thread.Sleep(200);
            //ptbsx = iSEM.GetBeamOffsetX();
            lblBSX.Text = ptbsx.ToString("0.000");
            //ptbsy = iSEM.GetBeamOffsetX();
            lblBSY.Text = ptbsx.ToString("0.000");
        }

        private void btnAddPoint_Click(object sender, EventArgs e)
        {
            SemPosition sp = new SemPosition();
            if (txtPointName.Text == "" || lblX.Text == "" || lblY.Text == "" || lblZ.Text == "" || lblT.Text == "" || lblR.Text == "" || lblM.Text == "" || lblBSX.Text == "" || lblBSY.Text == "")
            {
                return;
            }
            for (int i = 0; i < ListCutHole.Count; i++)
            {
                if (ListCutHole[i].HoleName == txtPointName.Text)
                {
                    MessageBox.Show("点名称已经存在！");
                    return;
                }
            }
            sp.X = pts[0];
            sp.Y = pts[1];
            sp.Z = pts[2];
            sp.T = pts[3];
            sp.R = pts[4];
            sp.M = pts[5];
            sp.BeamShiftX = ptbsx;
            sp.BeamShiftY = ptbsy;
            CutHole cutHole = new CutHole();
            cutHole.HoleName = txtPointName.Text;
            cutHole.Position = sp;
            ListCutHole.Add(cutHole);

            ltPointName.Items.Add(txtPointName.Text);
        }

        private void btnDelPoint_Click(object sender, EventArgs e)
        {
            if (ltPointName.SelectedIndex > -1)
            {
                ListCutHole.RemoveAt(ltPointName.SelectedIndex);
                ltPointName.Items.RemoveAt(ltPointName.SelectedIndex);
            }
        }

        private void ltPointName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(ltPointName.SelectedIndex>-1)
            {
                lblX.Text = ListCutHole[ltPointName.SelectedIndex].Position.X.ToString("0.000");
                lblY.Text = ListCutHole[ltPointName.SelectedIndex].Position.Y.ToString("0.000");
                lblZ.Text = ListCutHole[ltPointName.SelectedIndex].Position.Z.ToString("0.000");
                lblT.Text = ListCutHole[ltPointName.SelectedIndex].Position.T.ToString("0.000");
                lblR.Text = ListCutHole[ltPointName.SelectedIndex].Position.R.ToString("0.000");
                lblM.Text = ListCutHole[ltPointName.SelectedIndex].Position.M.ToString("0.000");
                lblBSX.Text = ListCutHole[ltPointName.SelectedIndex].Position.BeamShiftX.ToString("0.000");
                lblBSY.Text = ListCutHole[ltPointName.SelectedIndex].Position.BeamShiftY.ToString("0.000");
            }
        }

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