using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace wcfControlMonitorClient
{
    public partial class FrmBrowseDarkCasket : Form
    {
        private static FrmBrowseDarkCasket _formInstance;

        public static FrmBrowseDarkCasket FormInstance
        {
            get
            {
                if (_formInstance == null)
                {
                    _formInstance = new FrmBrowseDarkCasket();
                }
                return _formInstance;
            }
            set { _formInstance = value; }
        }
        public FrmBrowseDarkCasket()
        {
            InitializeComponent();
            _formInstance = this;
        }

        // 刷新按钮, 窗口登录时调用该函数。
        private void button1_Click(object sender, EventArgs e)
        {
            #region 浏览本地黑匣子
            
            //string filePath = System.IO.Path.GetFullPath(System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString());
            //filePath = System.IO.Path.GetDirectoryName(filePath);
            //filePath = System.IO.Path.Combine(filePath, "DarkCasket");
            //// 没找到目录返回
            //if (Directory.Exists(filePath) == false)
            //{
            //    return ;
            //}
            //string[] files=Directory.GetFiles(filePath);
            //Array.Sort(files);
            //// 目录为空时候返回
            //if (files.Length < 1) return;
            //string opf=files[files.Length - 1];
            //string[] cc=new string[1]{"**"};
            //string[] sp = null;
            //using( StreamReader sr = new StreamReader(opf))
            //{
            //    this.listView1.Items.Clear();
            //    sr.ReadLine();
            //    while(!sr.EndOfStream)
            //    {
            //        sp=sr.ReadLine().Split(cc,StringSplitOptions.RemoveEmptyEntries);
                    
            //        // 添加判断条件过滤信息 2011-05-26 zb
            //        if (comboBoxDevice.SelectedIndex > 0 || comboBoxDevice.Text != String.Empty)
            //        {
            //            if (comboBoxDevice.Text != sp[3])
            //            {
            //                continue;
            //            }
                        
            //        }

            //        string ss = sp[0];
            //        char[] st = new char[1] { ':' };
            //        string[] sf=sp[0].Split(st);
            //        DateTime dt = Convert.ToDateTime(sf[0]+":"+sf[1]+":"+sf[2]);
                    
            //        if ((dt.Hour < this.numericUpDown1.Value) || ((dt.Hour > this.numericUpDown2.Value)))
            //        {
            //            continue;
            //        }


            //        if (sp.GetLength(0) > 0)
            //        {
            //            ListViewItem lvi = new ListViewItem();
            //            lvi.Text = sp[0];
            //            for (int i = 1; i < sp.GetLength(0); i++)
            //            {
            //                lvi.SubItems.Add(sp[i]);
            //            }
            //            listView1.Items.Add(lvi);
            //            lvi = null;
            //        }
                    
                    
            //    }
            //}

            #endregion

            #region 浏览服务器黑匣子
            if (comboBoxDevice.Text.Trim() == "")
            {
                MessageBox.Show("请输入设备索引或者条码!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;

            }
            listBox1.Items.Clear();
            CStaticClass.WcfControl.BeginGetBlackBoxInfo(comboBoxDevice.Text, Convert.ToDateTime(this.numericUpDown1.Value + ":00:00"), Convert.ToDateTime(this.numericUpDown2.Value + ":59:59"), new AsyncCallback(CallbackGetBlackBoxInfo), null);
            #endregion
        }

        // 刷新按钮, 窗口登录时调用该函数。
        private void button4_Click(object sender, EventArgs e)
        {
            #region 浏览服务器黑匣子
            if (comboBoxDevice.Text.Trim() == "")
            {
                MessageBox.Show("请输入设备索引或者条码!", "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;

            }
            listBox1.Items.Clear();
            CStaticClass.WcfControl.BeginGetBlackBoxInfo("wms" + comboBoxDevice.Text, Convert.ToDateTime(this.numericUpDown1.Value + ":00:00"), Convert.ToDateTime(this.numericUpDown2.Value + ":59:59"), new AsyncCallback(CallbackGetBlackBoxInfo), null);
            #endregion
        }
        
        
        void CallbackGetBlackBoxInfo(IAsyncResult ar)
        {
            try
            {
                char[] cc = new char[2] { '\r', '\n' };
                string strerr = string.Empty;
                string BlackBox = CStaticClass.WcfControl.EndGetBlackBoxInfo(out strerr, ar);
                if (BlackBox != "")
                {
                    this.BeginInvoke(new MethodInvoker(delegate()
                    {
                        listBox1.Items.Clear();
                        string[] sp = BlackBox.Split(cc);
                        for (int i = 0; i < sp.GetLength(0); i++)
                        {
                            listBox1.Items.Add(sp[i]);
                        }
                    }));
                }
                else
                {
                    if (strerr != "")
                    {
                        MessageBox.Show(strerr, "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "操作提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要删除当前黑匣子文件吗", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
            {
                return;
            }
            this.listBox1.Items.Clear();
        }

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

        private void FrmBrowseDarkCasket_Load(object sender, EventArgs e)
        {
           
        }

        private void FrmBrowseDarkCasket_FormClosing(object sender, FormClosingEventArgs e)
        {
            _formInstance = null;
        }

        
    }
}