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 ControlSystem
{
    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)
        {
            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;
                    }
                    
                    
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("您确认要删除当前黑匣子文件吗", "操作提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
            {
                return;
            }
            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);
            if (files.Length < 1) return;
            File.Delete(files[files.Length - 1]);
            this.listView1.Items.Clear();
        }

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

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

        
    }
}