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

namespace Tiobon.Core.OPS.Tool.OPS.Tool.View
{
    public partial class Frm_DataExport : Form
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sSH"></param>
        public Frm_DataExport(string install_dir, string install_dir_item, Main_method _method, string path, string user, string password)
        {
            bool IsOver = false;

            InitializeComponent();
            this.Path = path;
            this.User = user;
            this._Method = _method;
            this.Install_dir = install_dir;
            this.Install_dir_Item = install_dir_item;
            this.Password = password;
        }
        public string Password { set; get; }
        public bool IsOver { set; get; }
        /// <summary>
        public string Path { set; get; }
        public string Host { set; get; }
        public string User { set; get; }
        public string Port { set; get; }
        public Main_method _Method { set; get; }
        public string Install_dir { set; get; }
        public string Install_dir_Item { set; get; }
        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataExport_Load(object sender, EventArgs e) { }
        /// <summary>
        /// 导出功能
        /// </summary>
        /// <param name="host"></param>
        /// <param name="user"></param>
        /// <param name="password"></param>
        /// <param name="port"></param>
        private void ExportALl(string user, string password, string Path)
        {
            try
            {


                string path = $"{Install_dir}/back/temp{DateTime.Now.ToString("yyyyMMdd")}";
                if (!_Method.sFTP.Exists(path))
                {
                    if (!_Method.sFTP.Connected)
                    {
                        _Method.sFTP.Connect();
                    }
                    _Method.sFTP.CreateDirectory(path);
                }
                string file = $"{path}/ihdis{DateTime.Now.ToString("yyyyMMdd")}.sql";
                string cmd = $"sudo docker-compose -f {Install_dir_Item}/docker-compose.yml  exec  -T mysql mysqldump  -u{user} -p{password} -hlocalhost --default-character-set=utf8   --skip-triggers \"hdis\" > {file}";
                if (!_Method.sSH.Connected) _Method.sSH.Connect();
                _Method.sSH.Excute_cmd(cmd, out string log);
                _Method.sFTP.Get(file, Path);
                Const.write_log(log);
            }
            catch (Exception error)
            {
                Const.write_log(error.ToString());
                MessageBox.Show("导出错误");
            }
        }
        /// <summary>
        /// 展示日志
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Showlogs(object sender, EventArgs e)
        {
            Task.Factory.StartNew(() =>
            {
                Task.Run(() =>
                {
                    //DataExpotor expotor = new DataExpotor(Host, User, Password, Port);
                    //expotor.ExportAll("ihdis", Path, out string log);
                    //Task.Delay(50000);
                    ExportALl(User, Password, Path);
                    Task.Delay(2000);
                    IsOver = true;
                });
                string txt = "正在下载.";
                string temp = string.Empty;
                int count = 0;
                while (!IsOver)
                {
                    if (count == 0)
                    {
                        temp = txt;
                        count += 1;
                    }
                    else if (count > 10)
                    {
                        count = 0;
                    }
                    else
                    {
                        count += 1;
                        temp = string.Concat(temp, ".");
                    }
                    this.Invoke(new Action(() =>
                    {
                        lb_info.Text = temp;
                    }));
                    Thread.Sleep(1000);
                }
                this.Invoke(new Action(() =>
                {
                    MessageBox.Show("导出完成！");
                    this.Dispose();
                }));
            });
        }
    }
}
