using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HslCommunication.LogNet;
using ProjectManagementSystem.Common.Extenions;
using ProjectManagementSystem.Common.Function;
using System.IO;

namespace ProjectManagementSystem.UI
{
    public partial class LogUI : UserControl
    {
        public int DisplayDegree { get; set; }

        public LogUI()
        {
            InitializeComponent();
        }

        public void AddLog(string log)
        {
            this.BeginInvoke((Action)(() =>
            {
                if(richTextBox1.Lines.Length > 1000)
                {
                    richTextBox1.Clear();
                }

                richTextBox1.AppendText(log);
                richTextBox1.AppendText(Environment.NewLine);
                richTextBox1.ScrollToCaret();
            }));
        }

        private void LogUI_Load(object sender, EventArgs e)
        {
            this.comboBox1.ValueMember = "Value";
            this.comboBox1.DisplayMember = "Display";
            this.comboBox1.DataSource = typeof(HslMessageDegree).EnumToDataTable("Value", "Display");

            this.comboBox1.SelectedIndex = this.comboBox1.Items.Count - 2;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            DisplayDegree = (int)this.comboBox1.SelectedValue;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string filePath = Common.Log.CLog.Instance.LogDir;
                System.Diagnostics.Process.Start(filePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"操作失败：{ex.Message}");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            using(FormLogNetView logNetView = new FormLogNetView())
            {
                logNetView.StartPosition = FormStartPosition.CenterScreen;
                logNetView.WindowState = FormWindowState.Normal;
                logNetView.ShowDialog();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string text = $"确定要开始打包所有日志？（日志压缩包：Log.zip）";
            var dialogResult = MessageBox.Show(text, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (dialogResult != DialogResult.OK)
            {
                return;
            }

            Task.Factory.StartNew(() =>
            {
                try
                {
                    string logDir = Common.Log.CLog.Instance.LogDir;
                    FileInfo finfo = new FileInfo(logDir);
                    string logZip = Path.Combine(finfo.Directory.FullName, $"{finfo.Name}.zip");
                    if (File.Exists(logZip))
                    {
                        File.Delete(logZip);
                    }

                    ZipUtility zipUtility = new ZipUtility();
                    zipUtility.ZipFileFromDirectory(Common.Log.CLog.Instance.LogDir, logZip, 9);

                    System.Diagnostics.Process.Start(finfo.Directory.FullName, logZip);

                }
                catch (Exception ex)
                {
                    MessageBox.Show($"操作失败：{ex.Message}");
                }
            });
        }
    }
}
