using System;
using System.IO;
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 GcDevicePc.ConfigDlg;

namespace GcDevicePc
{
    public partial class MethodNameInputForm : Form
    {
        public MethodNameInputForm()
        {
            InitializeComponent();
        }
       
        private void confirm_Click(object sender, EventArgs e)
        {
            string tmpname;
            if(this.MFiileName.Text != "")
            {
                globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder, MFiileName.Text + ".ini");
                globaldata.newfile = true;

                if (System.IO.File.Exists(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName) == false)
                {
                    StreamWriter sw = new StreamWriter(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName, false, System.Text.Encoding.Unicode);
                    sw.WriteLine("[仪器编号]");
                    sw.WriteLine("[方法名称]");
                    tmpname = globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName.Substring(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName.LastIndexOf("\\") + 1);
                    sw.WriteLine("名称=" + tmpname.ToString());
                    sw.Close();
                    globaldata.createfile = true;
                    globaldata.m_profileMethod.SetMethodFullName(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName);
                    globaldata.m_profileMethod.InitDevMethodIniFile();
                }

                globaldata.m_profileMethod.SetMethodFullName(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName); //传递路径和全名给MainForm

                this.Hide();

                globaldata.m_configDlg.m_InjPortDlg.reflashContent();
                globaldata.m_configDlg.m_InjSampleDlg.reflashContent();
                globaldata.m_configDlg.m_InjValveDlg.reflashContent();
                globaldata.m_configDlg.m_chromeDlg.reflashContent();
                globaldata.m_configDlg.m_RealColDlg.reflashContent();
                globaldata.m_configDlg.m_det.reflashContent();
                globaldata.m_configDlg.m_AuxDlg.reflashContent();
               // globaldata.m_configDlg.m_SignalDlg.reflashContent();
         
                globaldata.m_configDlg.Show();

                this.MFiileName.Text = "";
            }
            else
            {
                this.nameverify.Text = "请输入文件名";
                this.nameverify.ForeColor = Color.Red;
            }
            
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.Hide();
        }

        bool VerifyName()
        {
            string strtmp;

            DirectoryInfo MethodDir = new DirectoryInfo(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder);
            foreach (FileInfo file in MethodDir.GetFiles("*.ini"))
            {
                strtmp = this.MFiileName.Text + ".ini";          
                if(String.Compare(strtmp,file.Name,true) ==  0)
                {
                    return false;
                }
            }
            return true;
        }

        private void MFiileName_TextChanged(object sender, EventArgs e)
        {
            if (VerifyName())
           {
               this.nameverify.Text = "文件名可用";
               this.nameverify.ForeColor = Color.Green;
               this.prompt.Text = "";
           }
            else
           {
                this.nameverify.Text = "文件名存在";
                this.nameverify.ForeColor = Color.Red;
                this.prompt.Text = "ps:如文件存在，会打开原有方法而不创建新方法";
           }

           if (this.MFiileName.Text == "")
           {
                this.nameverify.Text ="";
           }
        }
    }
}
