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

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

        public InstrConfig fatherForm;

        public SendConfig()
        {
            InitializeComponent();
        }

        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 statuesend_Click(object sender, EventArgs e)
        //{
        //    string sendfilename = null;
        //    string checkfilename = null;
        //    string receiveip = null;

        //    for (int i = 0; i < localStatusList.Items.Count; i++)
        //    {
        //        if (localStatusList.GetItemChecked(i))
        //        {
        //            checkfilename =  localStatusList.GetItemText(localStatusList.Items[i]);
        //            sendfilename = System.IO.Path.Combine(globaldata.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, "\\Hard Disk\\仪器状态\\");                 
        //        }               
        //    }

        //    string message = "设备状态文件发送完成";
        //    MessageBox.Show(message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

        //}

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

            for (int i = 0; i < localStatusList.Items.Count; i++)
            {
                if (localStatusList.GetItemChecked(i))
                {
                    checkfilename = localStatusList.GetItemText(localStatusList.Items[i]);
                    sendfilename = System.IO.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);
            }       
        }

    }
}
