using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;//.Tasks;
using System.Windows.Forms;
using System.Web.UI.WebControls;

namespace GcDevicePc
{
    public partial class SendConfig : Form
    {
        FileServerClient SCclient = new FileServerClient();

        public InstrConfig fatherForm;

        public SendConfig()
        {
            InitializeComponent();
        }

        ~SendConfig()
        {
            SCclient.WtClientClose();
        }

        private void SendConfig_Load(object sender, EventArgs e)
        {
            DirectoryInfo subStatusDir = new DirectoryInfo(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.SubStatusFolder);

            localStatusList.Items.Clear();
            foreach (FileInfo file in subStatusDir.GetFiles("*.ini"))
            {
                localStatusList.Items.Add(new ListItem(file.Name, file.Name));
                //Console.WriteLine(file.Name);
                //Console.WriteLine(file.LastWriteTime.ToString());
            }

            // globaldata.m_DeviceInfo.IpAddr
            remoteStatusList.Items.Clear();
            for (int i = 0; i < globaldata.DeviceList.Count; i++ )
            {
                string strtmp = ((globaldata.DeviceInfo)globaldata.DeviceList[i]).IpAddr;
                remoteStatusList.Items.Add(new ListItem(strtmp,strtmp));
            }
        }

        private void localStatusList_ItemCheck(object sender, ItemCheckEventArgs e)
        {

            if(this.localStatusList.CheckedItems.Count > 0)
            {
                for (int i = 0; i < localStatusList.Items.Count; i++)
                {
                    if (i != e.Index)
                    {
                       // this.localStatusList.SetItemCheckState(i, System.Windows.Forms.CheckState.Unchecked);
                        this.localStatusList.SetItemChecked(i, false);                     
                    }
                }
            }                   
        }

        private void SendStatue_Click(object sender, EventArgs e)
        {
            string sendfilename = null;
            string checkfilename = null;
            string receiveip = null;

            if (globaldata.m_pcbuffer.gcpcinfo.pcworkinfo.system_Statue == false)
            {
                for (int i = 0; i < localStatusList.Items.Count; i++)
                {
                    if (localStatusList.GetItemChecked(i))
                    {
                        checkfilename = localStatusList.GetItemText(localStatusList.Items[i]);
                        sendfilename = Path.Combine(globaldata.m_pcbuffer.gcpcinfo.pcfolderinfo.SubStatusFolder, checkfilename);
                    }
                }

                for (int i = 0; i < remoteStatusList.Items.Count; i++)
                {
                    if (remoteStatusList.GetItemChecked(i))
                    {
                        receiveip = remoteStatusList.GetItemText(remoteStatusList.Items[i]);
                        SCclient.WtClientInit(receiveip, globaldata.fileserverport);
                        SCclient.WtClientCopytoServer("DevStatus.ini", sendfilename, globaldata.remoteFolder + "仪器状态\\");
                    }
                }

                if (remoteStatusList.Items.Count > 0)
                {
                    
                    string message = "设备状态文件发送完成";
                    MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);

                    if (HMIRestartCheck.Checked)
                    {
                        int ret = 0;
                        ret = SCclient.WtHMICloseRro("WtMainProc");
                        if (ret == 1)
                        {
                            ret = SCclient.WtHMIRunPro("GcDevice.exe", globaldata.exeremoteFolder);
                            if (ret == 1)
                            {

                                DialogResult result = MessageBox.Show("HMI自重启成功，是否重启PC软件以同步？", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
                                if (result == DialogResult.OK)
                                {
                                    Application.ExitThread();
                                    Application.Exit();
                                    Application.Restart();
                                    Process.GetCurrentProcess().Kill();
                                }
                                else if(result == DialogResult.Cancel)
                                {
                                    this.Close();
                                }
                                //message = "HMI自重启成功";
                                //MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                        }
                    }
                   
                }
            }
            else
            {
                MessageBox.Show("请停止系统，再更新配置！");
            }

        }

    }
}
