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;
        private static string strCurrentLog = string.Empty;
        private static string strFormTitle = string.Empty;
        private static int iCurrentDayLogs = 0;
        private static int iEcho = 0;

        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,"*.log",SearchOption.TopDirectoryOnly);//20101220，只查找LOG文件，避免查找到其它类型的文件
            if (files != null)
            {
                Array.Sort(files);//20100726
                if (files.Length < 1) return;
            }
            else
            {
                return;
            }

            bool bExist = false;
            string tempStr = string.Empty;
            string str = string.Empty;

            //Get Selected Date's log file count
            if (0 >= iEcho)
            {
                foreach (string strTemp in files)
                {
                    if (strTemp.IndexOf(this.dateTimePicker1.Value.ToString("yyyy-MM-dd")) != -1)
                    {
                        iCurrentDayLogs++;
                    }
                    iEcho++;
                }
                this.label_TotalNo.Text = iCurrentDayLogs.ToString().Trim();
                if (iCurrentDayLogs > 0)
                {
                    this.numericUpDown1.Maximum = iCurrentDayLogs;
                    this.numericUpDown1.Minimum = 1;
                }
                else
                {
                    this.numericUpDown1.Minimum = iCurrentDayLogs;
                    this.numericUpDown1.Maximum = iCurrentDayLogs;
                }
            }
            iCurrentDayLogs = 0;
            iEcho = 0;

            for (int j = files.Length - 1; j > -1; j--)
            {
                str = files[j];
                tempStr = str.Substring(str.LastIndexOf('\\') + 1);
                tempStr = tempStr.Substring(0, tempStr.LastIndexOf('.'));
                if (tempStr == (this.dateTimePicker1.Value.ToString("yyyy-MM-dd") + "0" + this.numericUpDown1.Value.ToString().Trim()))
                {
                    //string opf=files[files.Length - 1];
                    string[] cc = new string[1] { "**" };
                    string[] sp = null;
                    using (StreamReader sr = new StreamReader(str))
                    {
                        this.listView1.Items.Clear();
                        sr.ReadLine();
                        while (!sr.EndOfStream)
                        {
                            sp = sr.ReadLine().Split(cc, StringSplitOptions.RemoveEmptyEntries);

                            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;
                            }
                        }
                    }
                    strCurrentLog = str;
                    strFormTitle = tempStr;
                    bExist = true;
                    break;
                }
            }

            if (!bExist)
            {
                listView1.Items.Clear();
                strCurrentLog = string.Empty;
                strFormTitle = this.dateTimePicker1.Value.ToString("yyyy-MM-dd");
                //MessageBox.Show("指定文件不存在！");
            }
        }

        private void button2_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);
            //if (files.Length < 1) return;
            if (MessageBox.Show(this,"您确定要删除指定日志文件吗？该操作将彻底删除指定日志文件！", "系统提示：", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                if (File.Exists(strCurrentLog))
                {
                    File.Delete(strCurrentLog);
                }
                else
                {
                    MessageBox.Show("选择日期不存在黑匣子！请重新选择日期！");
                }
                this.listView1.Items.Clear();

                button1_Click(sender, e);
                this.Text = "浏览黑匣子：" + strFormTitle;
            }
        }

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

        private void FrmBrowseDarkCasket_Load(object sender, EventArgs e)
        {
            SetColumnTitle();
            this.numericUpDown1.Value = this.numericUpDown1.Minimum;
            button1_Click(sender, e);
            this.Text = "浏览黑匣子：" + strFormTitle;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            this.numericUpDown1.Value = this.numericUpDown1.Minimum;
            button1_Click(sender, e);
            this.Text = "浏览黑匣子：" + strFormTitle;
        }
        private void SetColumnTitle()
        {
            listView1.Columns[0].Text = "通讯时间";
            listView1.Columns[1].Text = "接口类型";
            listView1.Columns[2].Text = "命令来源";
            listView1.Columns[3].Text = "地址/端口";
            listView1.Columns[4].Text = "报文内容";
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            button1_Click(sender, e);
            this.Text = "浏览黑匣子：" + strFormTitle;
        }
    }
}