using System;
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 GcDevicePc.Common;
using WeifenLuo.WinFormsUI.Docking;
using GcDevicePc.Controls;

namespace GcDevicePc
{
    public partial class Test : DockContent
    {
        public Test()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int ret;
            string remoteFolder = "\\" + this.textBox1.Text + "\\"+ this.textBox3.Text + "\\";
            string getfile = this.textBox2.Text;
            string localfile = globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.RunFolder+"\\Debugfile\\"+getfile;

            try
            {
                if (System.IO.File.Exists(localfile))
                {
                    System.IO.File.Delete(localfile);
                }
                if (globaldata.connection_ip != null && globaldata.connection_ip !="")
                {
                    FileServerClient mainclient = new FileServerClient();
                    mainclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                    ret = mainclient.WtClientCopyfromServer(getfile, localfile, remoteFolder);
                    mainclient.WtClientClose();
                }

                if (System.IO.File.Exists(localfile))
                {
                    MessageBox.Show("获取成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch 
            {
                string message = "获取失败！";
                MessageBox.Show(message.ToString(), "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private bool useflag = false;
        private void button2_Click(object sender, EventArgs e)
        {
            if (!useflag)
            {
                this.pictureMenu1.StatusText = "连接";
                this.pictureMenu1.PMOptbtnstatus = 3;
                this.pictureMenu1.WaingShow = false;
                useflag = !useflag;
            }
            else
            {
                this.pictureMenu1.StatusText = "为连接";
                this.pictureMenu1.PMOptbtnstatus = 0;
                this.pictureMenu1.WaingShow = true;
                useflag = !useflag;
            }
        }

        private void pictureMenu1_PMOptBtnClicked(object sender, EventArgs e)
        {
            PictureBox btn = sender as PictureBox;
            MessageBox.Show(btn.Tag.ToString());
        }

        private void pictureMenu1_PMOrdBtnClicked(object sender, EventArgs e)
        {
            PictureBox btn = sender as PictureBox;
            MessageBox.Show(btn.Tag.ToString());
        }

        private bool sysflag = false;
        private void pictureMenu1_PMSysBtnClicked(object sender, EventArgs e)
        {
            if(!sysflag){
                this.pictureMenu1.PMSysbtnstatus = 1;
                sysflag = !sysflag;
            }else
            {
                this.pictureMenu1.PMSysbtnstatus = 0;
                sysflag = !sysflag;
            }
        }


        private int mycount = 0;

        public void file_test_Click(object sender, EventArgs e)
        {
            int ret;
            string remoteFolder1 = "\\Hard Disk2\\仪器方法\\";
          //  string remoteFolder2 = "\\Hard Disk2\\仪器状态\\";
         //   string remoteFolder3 = "\\Hard Disk2\\运行表\\";

            List<String> filename = new List<string>();
         //   filename.Add("2016方法2.ini");
            filename.Add("A.ini");
            filename.Add("ben.ini");
            filename.Add("NMHC.ini");
            filename.Add("开机.ini");
           // string devfile = "DevStatus.ini";
           // string runfile = "RunMethod.ini";

            String localfile = System.Windows.Forms.Application.StartupPath + "\\Debugfile\\";

            try
            {
                if (!String.IsNullOrEmpty(globaldata.connection_ip))
                {
                    int i = 0;
                    int count = 1000;

                    int fileret = 0;
                    FileTools test = new FileTools();
                   
                   
                    while (count -- > 0)
                    {
                        FileServerClient mainclient = new FileServerClient();
                        for (i = 0; i < filename.Count; i++)
                        {
                            String getfile = localfile + filename[i];

                            Console.WriteLine("要求传输文件：" + filename[i]);

                            fileret = test.IsUse(getfile);
                            if (fileret == 0 || fileret == 1)
                            {
                                if (System.IO.File.Exists(getfile))
                                {
                                    System.IO.File.Delete(getfile);
                                }

                                fileret = test.IsUse(getfile);
                                if (fileret == 1)
                                {
                                    mycount++;
                                    Console.WriteLine("准备进行第" + mycount.ToString() +"次传输");
                                    mainclient.WtClientInit(globaldata.connection_ip, globaldata.fileserverport);
                                    ret = mainclient.WtClientCopyfromServer(filename[i], getfile, remoteFolder1);
                                    
                                    fileret = test.IsUse(getfile);
                                    while (fileret != 0)
                                    {
                                        Console.WriteLine("设备文件没有传输好,进度为：" + fileret.ToString());
                                        Thread.Sleep(200);
                                        fileret = test.IsUse(getfile);
                                    }

                                    Console.WriteLine(filename[i] + " 文件传输完成");
                                }
                                else
                                {
                                    Console.WriteLine(filename[i] + " 删除未完成===============");
                                }                            
                            }
                            else
                            {
                                Console.WriteLine(filename[i] + " 文件传被占用++++++++++++++++");
                            }
                        }

                        mainclient.WtClientClose();
                    }
                   



                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error:" + ex.Message);
            }
        }
    }
}
