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;
using System.Windows.Forms;
using System.Web.UI.WebControls;

namespace GcDevicePc
{
    public delegate void changemethodname(string name);
    public partial class MethodScan : Form
    {
         string checkedfilename;
         string readfile;

        public MethodScan()
        {
            InitializeComponent();
        }

        private void MethodScan_Load(object sender, EventArgs e)
        {
            this.MethodInfo.ReadOnly = true;
           // this.MethodInfo.Enabled = false;
            InitMethodList();
        }

        private void InitMethodList()
        {
            string folderName = System.Windows.Forms.Application.StartupPath;
            string tmpDir = System.IO.Path.Combine(folderName, "仪器方法");

            DirectoryInfo MethodDir = new DirectoryInfo(tmpDir);

            foreach (FileInfo file in MethodDir.GetFiles("*.ini"))
            {
                //int index = LocalList.Rows.Add();
                //LocalList.Rows[index].Cells[1].Value = file.Name;
                //LocalList.Rows[index].Cells[2].Value = file.LastWriteTime.ToString();
                //Console.WriteLine(file.Name);
                string line = file.Name;
                //Console.WriteLine(file.LastWriteTime.ToString());
                this.MethodList.Items.Add(new ListItem(line, line));
            }
        }

        private void MethodList_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (this.MethodList.CheckedItems.Count > 0)
            {
                for (int i = 0; i < MethodList.Items.Count; i++)
                {
                    if (i != e.Index)
                    {
                        this.MethodList.SetItemChecked(i, false);
                    }
                }              
            }
        }

        private void MethodList_SelectedIndexChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < MethodList.Items.Count; i++)
            {
                if (MethodList.GetItemChecked(i))
                {
                    checkedfilename = MethodList.GetItemText(MethodList.Items[i]);
                    readfile = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder, checkedfilename);
                }
            }
            MethodInfo.Text = "";
            MethodInfo.Text = File.ReadAllText(readfile);
        }

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

        public event changemethodname ChangeMName;
        private void openbutton_Click(object sender, EventArgs e)
        {
            if (globaldata.connection_ip != null)
            {
               
              
                FileServerClient singlesend = new FileServerClient();
                singlesend.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                singlesend.WtClientCopytoServer("SingleAnal.ini", readfile, "\\Hard Disk\\仪器方法\\");
                singlesend.WtClientClose();

                globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName = readfile;
                //GcDataReq smodbus = new GcDataReq(globaldata.connection_ip, globaldata.datamodbusport);
                //smodbus.WriteStopVal();
                //Thread.Sleep(500);
                //smodbus.WriteSingleVal();
                this.Close();

                ChangeMName(checkedfilename);
             
             //   MainBase.textconent.Text = "运行中...";
              //  MainBase.textconent.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(100)))), ((int)(((byte)(149)))), ((int)(((byte)(237)))));

               // MainBase.now_method.Text = "当前运行方法：" + globaldata.cur_MethodName.Substring(globaldata.cur_MethodName.LastIndexOf("\\") + 1);
            }
            else
            {
               //  MainBase.now_method.Text = "当前运行方法：" + "未传输";
              //   MainBase.textconent.Text = "停止";
              //    MainBase.textconent.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(100)))), ((int)(((byte)(149)))), ((int)(((byte)(237)))));
            }
        }
    }
}
