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;
//using System.Threading;

namespace GcDevicePc
{
    public partial class BrowseForm : Form
    {
        FileServerClient bfclient = new FileServerClient();
        List<string> syncfilename = new List<string>();

        public BrowseForm()
        {
            InitializeComponent();
        }

        private void BrowseForm_Load(object sender, EventArgs e)
        {
         //   this.textBox1.Text = globaldata.cur_StatusName;
         //   this.textBox1.Enabled = false;
            this.remotesync.Enabled = false;

            if(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName == null)
            {
                this.MethodName.Text = "无";
            }else
            {
                this.MethodName.Text = globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName.Substring(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_MethodName.LastIndexOf("\\") + 1);
            }

            if (globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_StatusName == null)
            {
                this.StatueName.Text = "无";
            }else
            {
                this.StatueName.Text = globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_StatusName.Substring(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_StatusName.LastIndexOf("\\") + 1);
            }

            if (globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab == null)
            {
                this.RunTabName.Text = "无";
            }else
            {
                this.RunTabName.Text = globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab.Substring(globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab.LastIndexOf("\\") + 1);
            }



            DataGridViewCheckBoxColumn checkfile = new DataGridViewCheckBoxColumn();
            checkfile.Name = "bfilename";
            checkfile.Width = 40;
            checkfile.HeaderText = "是否同步";
            LocalList.Columns.Add(checkfile);
            DataGridViewTextBoxColumn filename = new DataGridViewTextBoxColumn();
            filename.Name = "lasttime";
            filename.Width = 80;
            filename.HeaderText = "文件名";
            LocalList.Columns.Add(filename);
            DataGridViewTextBoxColumn lasttime = new DataGridViewTextBoxColumn();
            lasttime.Name = "lasttime";
            lasttime.Width = 140;
            lasttime.HeaderText = "最后修改时间";
            LocalList.Columns.Add(lasttime);
   
            //int i = dataGridView1.Rows.Add();
            //DataGridViewRow r = dataGridView1.Rows[i];
            
            string folderName = System.Windows.Forms.Application.StartupPath;
            string tmpDir = System.IO.Path.Combine(folderName, "仪器方法");

            DirectoryInfo MethodDir = new DirectoryInfo(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder);

            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);
                Console.WriteLine(file.LastWriteTime.ToString());
            }
        }

        private void getmlist_Click(object sender, EventArgs e)
        {
             if(globaldata.connection_ip != null)
             {
                 int bret;
                 string methodlistname = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder, "rMethodList");

                 if (System.IO.File.Exists(methodlistname))
                 {
                     System.IO.File.Delete(methodlistname);
                 }

                 getmlist.Enabled = false;
                 bfclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                 bret = bfclient.WtClientCopyfromServer("MethodList", methodlistname, "\\Hard Disk\\仪器方法\\");
                 getmlist.Enabled = true;

                 Thread.Sleep(1000);

                 if (true)
                 {
                     //按行读取为字符串数组
                     FileStream fs = new FileStream(methodlistname, FileMode.Open, FileAccess.Read);
                     StreamReader sr = new StreamReader(fs, Encoding.Unicode);
                     String line;
                     remoteList.Items.Clear();
                     while (sr.Peek() != -1)
                     {
                         line = sr.ReadLine();
                         if (line != "MethodList")
                             remoteList.Items.Add(new ListItem(line, line));
                     }
                     sr.Close();
                 }
                 MessageBox.Show("获取完成", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 this.remotesync.Enabled = true;

             }else
             {
                 string message = "远程设备未连接";
                 MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
             }

            
        }

        private void getsyncdfilename(int flag)
        {
            syncfilename.Clear();
            if(flag == 0)
            {
                for (int i = 0; i < LocalList.Rows.Count; i++)
                {
                    if ((bool)LocalList.Rows[i].Cells[0].EditedFormattedValue == true)
                    {
                        syncfilename.Add((string)LocalList.Rows[i].Cells[1].Value);
                    }
                }
            }else
            {
                for (int i = 0; i < remoteList.Items.Count; i++)
                {
                    if (remoteList.GetItemChecked(i))
                        syncfilename.Add(remoteList.GetItemText(remoteList.Items[i]));
                }
            }
        }

        private void localsync_Click(object sender, EventArgs e)
        {
             if(globaldata.connection_ip != null)
             {
                 DialogResult dr = MessageBox.Show("确认将本地方法同步到设备？", "", MessageBoxButtons.YesNo);
                 if (dr == DialogResult.Yes)
                 {
                     getsyncdfilename(0);

                     // string folderName = System.Windows.Forms.Application.StartupPath;
                     string sendname;

                     for (int i = 0; i < syncfilename.Count; i++)
                     {
                         sendname = null;
                         bfclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                         // folderName = System.IO.Path.Combine(folderName, "仪器方法");
                         sendname = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder, syncfilename[i]);
                         bfclient.WtClientCopytoServer(syncfilename[i], sendname, "\\Hard Disk\\仪器方法\\");
                         Thread.Sleep(2000);

                     }

                     if (this.statuscheck.Checked)
                     {
                         if (globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_StatusName != null)
                         {
                             sendname = null;
                             bfclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                             sendname = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder, StatueName.Text);
                             bfclient.WtClientCopytoServer("DevStatus.ini", sendname, "\\Hard Disk\\仪器状态\\");
                             Thread.Sleep(1000);
                         }
                     }

                     if (this.runtabcheck.Checked)
                     {
                         if (globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab != null)
                         {
                             sendname = null;
                             bfclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                             sendname = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder, "RunMethod.ini");
                             bfclient.WtClientCopytoServer("RunMethod.ini", sendname, "\\Hard Disk\\运行表\\");
                             Thread.Sleep(1000);
                         }
                     }
                     string message = "本地同步完成";
                     MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                 }
                 else if (dr == DialogResult.No)
                 {

                 }

             }else
             {
                 string message = "远程设备未连接";
                 MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
             }     
        }

        private void remotesync_Click(object sender, EventArgs e)
        {
            if(globaldata.connection_ip != null)
            {
                DialogResult dr = MessageBox.Show("确认将设备方法下载到本地？", "", MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    getsyncdfilename(1);

                    string sendname;

                    for (int i = 0; i < syncfilename.Count; i++)
                    {
                        sendname = null;
                        bfclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);

                        sendname = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.MethodFolder, syncfilename[i]);
                        bfclient.WtClientCopyfromServer(syncfilename[i], sendname, "\\Hard Disk\\仪器方法\\");
                        Thread.Sleep(2000);

                    }


                    if (this.statuscheck.Checked)
                    {
                        if (globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_StatusName != null)
                        {
                            sendname = null;
                            bfclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                            sendname = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.StatusFolder, StatueName.Text);
                            bfclient.WtClientCopyfromServer(StatueName.Text, sendname, "\\Hard Disk\\仪器状态\\");
                            Thread.Sleep(1000);
                        }
                    }

                    if (this.runtabcheck.Checked)
                    {
                        if (globaldata.m_pcbuffer.gcpcinfo.pcfileinfo.cur_RunTab != null)
                        {
                            sendname = null;
                            bfclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                            sendname = System.IO.Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunTabFolder, RunTabName.Text);
                            bfclient.WtClientCopyfromServer(RunTabName.Text, sendname, "\\Hard Disk\\运行表\\");
                            Thread.Sleep(1000);
                        }
                    }

                    string message = "远程同步完成";
                    MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else if (dr == DialogResult.No)
                {

                }
            }else
            {
                string message = "远程设备未连接";
                MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

           
        }

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

        private void opengcstatus_Click(object sender, EventArgs e)
        {

            string folderName = System.Windows.Forms.Application.StartupPath;
            string pathString = System.IO.Path.Combine(folderName, "仪器状态");
            System.Diagnostics.Process.Start("explorer.exe", pathString);
        }

    }
}
