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, IDisplayUI
    {
        public int DisplayDegree { get; set; }

        public LogUI()
        {
            InitializeComponent();
        }

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

                int textLength = richTextBox1.TextLength;

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

                richTextBox1.Select(textLength, 4);
                richTextBox1.SelectionColor = GetDegreeColor((HslMessageDegree)degree);

                if (checkBox1.Checked)
                {
                    richTextBox1.ScrollToCaret();
                }
            }));
        }

        private Color GetDegreeColor(HslMessageDegree degree)
        {
            switch (degree)
            {
                case HslMessageDegree.None:
                    return Color.DarkGray;
                case HslMessageDegree.FATAL:
                    return Color.DarkRed;
                case HslMessageDegree.ERROR:
                    return Color.Red;
                case HslMessageDegree.WARN:
                    return Color.Yellow;
                case HslMessageDegree.INFO:
                    return Color.Black;
                case HslMessageDegree.DEBUG:
                    return Color.Green;
                default:
                    return Color.Black;
            }
        }

        private void LogUI_Load(object sender, EventArgs e)
        {
            this.comboBox1.ValueMember = "Value";
            this.comboBox1.DisplayMember = "Display";
            this.comboBox1.DataSource = HslMessageDegree.None.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.Logger.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 = $"确定要开始打包所有日志和配置？（日志压缩包：PmsLog.zip）";
            var dialogResult = MessageBox.Show(text, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (dialogResult != DialogResult.OK)
            {
                return;
            }

            Task.Factory.StartNew(() =>
            {
                try
                {
                    string s = System.Reflection.Assembly.GetExecutingAssembly().Location;
                    FileInfo finfo = new FileInfo(s);
                    string currPath = finfo.DirectoryName;
                    string currDir = Path.Combine(currPath, "PMS");
                    string logZip = Path.Combine(currPath, "PmsLog.zip");

                    if (File.Exists(logZip))
                    {
                        File.Delete(logZip);
                    }

                    ZipUtility zipUtility = new ZipUtility();
                    zipUtility.ZipFileFromDirectory(currDir, logZip, 9);

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

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

        public bool Init()
        {
            return true;
        }

        public void UpdateDisplay()
        {
        }
    }
}
