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 MeasureData;
using MeasureThread;
using FileManager;

namespace WindowsFormsApp1
{
    public partial class MainForm : Form
    {
        #region 成员变量
        private BackgroundWorker m_BackgroundWorker;// 申明后台对象
        /// <summary>
        /// 测量文件
        /// </summary>
        public MeasureFile m_MeasureFile;
        
        /// 测量线程  
        public Measure m_Ms;

        //图像文件夹
        public string m_strImgPath;

        #endregion
        #region 构造函数
        public MainForm()
        {
            InitializeComponent();

            m_BackgroundWorker = new BackgroundWorker(); // 实例化后台对象 
            m_BackgroundWorker.WorkerReportsProgress = true; // 设置可以通告进度 
            m_BackgroundWorker.WorkerSupportsCancellation = true; // 设置可以取消 
            m_BackgroundWorker.DoWork += new DoWorkEventHandler(DoWork);
            m_BackgroundWorker.ProgressChanged += new ProgressChangedEventHandler(UpdateProgress);
            m_BackgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedWork);

            LogManager.InitManulLog();
           

        }
        #endregion
        #region 测量线程
        void DoWork(object sender, DoWorkEventArgs e)
        {
            m_Ms = new Measure();
            m_Ms.InitMeas(m_MeasureFile);
            m_Ms.SendThreadStatus += new ThreadStatusHandler(displayMessage); //注册事件

            //自动测量的全过程
            m_Ms.DoMeasure();
            //定位
            //切割
            //分析位置
            //截面

        }

        public void displayMessage(object sender, ThreadStatusEventArgs e)
        {
            //主界面显示内容
            this.BeginInvoke((Action)delegate
            {
                this.listmsg.Items.Add(e.Time.ToString() + e.State);
            });
        }

        void UpdateProgress(object sender, ProgressChangedEventArgs e)
        {
        }
        void CompletedWork(object sender, RunWorkerCompletedEventArgs e)
        {
        }
        #endregion
        #region 按钮操作
        /// <summary>
        /// 新建文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNewFile_Click(object sender, EventArgs e)
        {
            m_MeasureFile = new MeasureFile();
            if (!m_MeasureFile.New())
            {
                return;
            }
            else
            {
                MessageBox.Show("新建测量文件成功。");
            }
        }

        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveFile_Click(object sender, EventArgs e)
        {
            if (m_MeasureFile == null)
            {
                MessageBox.Show("请新建一个测量文件");
            }
            else
            {
                m_MeasureFile.Save();                
            }
        }

        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenFile_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (m_BackgroundWorker.IsBusy)
            {
                m_BackgroundWorker.CancelAsync();
            }

        }

        /// <summary>
        /// 加载切割孔
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLoadCutHoles_Click(object sender, EventArgs e)
        {
            if (m_MeasureFile == null)
            {
                MessageBox.Show("请新建一个测量文件");
            }
            else
            {
                if (!m_MeasureFile.GetCutHolesFromFile(""))
                {
                    MessageBox.Show("导入切孔失败");
                }

                this.CutHoleGridView.Rows.Clear();
                List<CutHole> listHoles = m_MeasureFile.ListCutHole;
                foreach (CutHole hole in listHoles)
                {
                    //在CutHoleGridView中，添加切孔信息
                    int index = this.CutHoleGridView.Rows.Add();
                    this.CutHoleGridView.Rows[index].Cells[0].Value = hole.HoleName;

                    SemPosition pos = hole.Position;
                    this.CutHoleGridView.Rows[index].Cells[1].Value = pos.X;
                    this.CutHoleGridView.Rows[index].Cells[2].Value = pos.Y;
                    this.CutHoleGridView.Rows[index].Cells[3].Value = pos.Z;
                    this.CutHoleGridView.Rows[index].Cells[4].Value = pos.M;
                    this.CutHoleGridView.Rows[index].Cells[5].Value = pos.R;
                    this.CutHoleGridView.Rows[index].Cells[6].Value = pos.T;
                }
            }
        }

        /// <summary>
        /// 启动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (m_MeasureFile == null)
            {
                MessageBox.Show("请新建一个测量文件");
                listmsg.Items.Clear();
            }
            else
            {
                if (m_BackgroundWorker.IsBusy)
                {
                    MessageBox.Show("线程已经运行");
                    return;
                }
                m_BackgroundWorker.RunWorkerAsync(this);
            }
        }

        /// <summary>
        /// 调区FIB模板
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btFIB_Click(object sender, EventArgs e)
        {
            string FilePathName;
            string fileNameWithoutExtension;
            //新建一个文件对话框
            OpenFileDialog pOpenFileDialog = new OpenFileDialog();
            //设置对话框标题
            pOpenFileDialog.Title = "选择模板文件";
            //设置打开文件类型
            pOpenFileDialog.Filter = "ely文件（*.ely）|*.ely";
            //监测文件是否存在
            pOpenFileDialog.CheckFileExists = true;
            //文件打开后执行以下程序
            if (pOpenFileDialog.ShowDialog() == DialogResult.OK)
            {
                FilePathName = System.IO.Path.GetFullPath(pOpenFileDialog.FileName);                             //绝对路径
                fileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(FilePathName);
                this.tBFIBTemp.Text = fileNameWithoutExtension;
            }
        }

        private void btParamOK_Click(object sender, EventArgs e)
        {
            if (m_MeasureFile == null)
            {
                MessageBox.Show("请新建一个测量文件");
                this.listmsg.Items.Add("请新建一个测量文件");
            }
            else
            {
                m_MeasureFile.MParam.PT = this.cBIsPT.Checked;
                m_MeasureFile.MParam.SampleName = this.tBSampleName.Text;
                m_MeasureFile.MParam.FIBTemp = this.tBFIBTemp.Text;
                m_MeasureFile.MParam.FocusMode = this.cBIsManul.Checked;

                MessageBox.Show("参数设置成功");
            }
        }

        #endregion

        //定位操作
        private void button5_Click(object sender, EventArgs e)
        {

        }

        //切割操作
        private void button6_Click(object sender, EventArgs e)
        {

        }

        //分析位置操作
        private void button7_Click(object sender, EventArgs e)
        {

        }

        //观测截面
        private void button1_Click(object sender, EventArgs e)
        {

        }

        private void btImgPath_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.Description = "选择拍图保存的文件夹";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (string.IsNullOrEmpty(dialog.SelectedPath))
                {
                    MessageBox.Show("图像文件夹不能为空");
                    return;
                }

                m_strImgPath = dialog.SelectedPath;
                tBImgPath.Text = dialog.SelectedPath;

            }
        }
    }
}
