using Tiobon.Core.OPS.Tool.OPS.Tool.Helper;
using Tiobon.Core.OPS.Tool.OPS.Tool.Model;
using Tiobon.Core.OPS.Tool.OPS.Tool.Src;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;

namespace Tiobon.Core.OPS.Tool.OPS.Tool.View
{
    public partial class Frm_Moudule : Form
    {

        readonly Module_method _method = new Module_method();
        bool isLinux = Const.config.system == "Linux";

        public Frm_Moudule()
        {
            InitializeComponent();
            _method.End_Init = End;
        }

        private void btn_install_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_filepath.Text.Trim()))
                return;
            if (txt_filepath.Text.ToLower().EndsWith("fsc.zip") || (txt_filepath.Text.ToLower().Contains("hdis_qc") && txt_filepath.Text.ToLower().EndsWith(".zip")))
            {
                if (File.Exists(txt_filepath.Text))
                {
                    if (!Const.Is_BadZip(txt_filepath.Text, out string comment))
                    {
                        MessageBox.Show("压缩包已损坏，请检查！");
                        return;
                    }

                    if (!Show_conf())
                    {
                        return;
                    }
                    foreach (Control item in Controls)
                    {
                        item.Enabled = false;
                    }
                    timer_Timeout.Enabled = true;
                    lbl_usetime.Text = "已用时间：";
                    _run_time = DateTime.Now;
                    Task.Factory.StartNew(() =>
                    {
                        _method.Install(txt_filepath.Text, sendLog);
                    });
                }
                else
                {
                    MessageBox.Show("文件不存在，请检查文件路径！");
                    return;
                }
            }
            else
            {
                MessageBox.Show("只支持fsc.zip和qc*.zip插件！");
                return;
            }
        }

        private void sendLog(string str)
        {
            Invoke(new Action(() =>
            {
                this.Text = $"模块管理 - {str}";
            }));
        }


        private bool Show_conf()
        {
            Frm_Conf frm_Conf = new Frm_Conf();
            Dictionary<string, string> paras = new Dictionary<string, string>();
            var module = _method.GetConf(txt_filepath.Text);
            frm_Conf.GetConf(module);
            if (frm_Conf.ShowDialog() == DialogResult.OK)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void Frm_Moudule_Load(object sender, EventArgs e)
        {

        }

        private void btn_selectfile_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Multiselect = false;//该值确定是否可以选择多个文件
                ofd.Title = "请选择文件";
                ofd.Filter = "插件zip文件|*.zip";
                if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    this.txt_filepath.Text = ofd.FileName;
                }
            }
        }
        DateTime _run_time;
        private void timer_Timeout_Tick(object sender, EventArgs e)
        {
            var total = DateTime.Now.Subtract(_run_time);
            lbl_usetime2.Text = $"{total.Minutes}:{total.Seconds}";
        }

        #region 操作完毕触发事件
        public void End()
        {
            Invoke(new Action(() =>
            {
                timer_Timeout.Enabled = false;
                foreach (Control item in Controls)
                {
                    item.Enabled = true;
                }
                MessageBox.Show("安装完毕！");
            }));
        }
        #endregion
    }
}
