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 Tiobon.Core.OPS.Tool.OPS.Tool.Src;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace Tiobon.Core.OPS.Tool.OPS.Tool.View
{
    public partial class Frm_Login : Form
    {
        #region 全局变量
        Login_method _Method = new Login_method();
        List<Config> configs = new List<Config>();
        bool Isexit = true;
        int index = 0;
        #endregion

        #region Init
        public static Frm_Login m_Login = null;
        public static Frm_Login Create_Login()
        {
            if (m_Login == null)
                m_Login = new Frm_Login();
            return m_Login;
        }

        public Frm_Login()
        {
            //AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            InitializeComponent();
            this.skinDataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.skinDataGridView1.AutoGenerateColumns = false;
            //first = false;
        }
        #endregion

        #region 加载配置
        private void Login_Shown(object sender, EventArgs e)
        {
            try
            {
                if (Load_config())
                {
                    if (Directory.Exists(configs.FirstOrDefault().install_dir + "/app/asdasd"))
                    {
                        Isexit = false;
                        this.Close();
                    }
                    else
                    {
                        DataTable dt = configs.ToDataTable();
                        skinDataGridView1.DataSource = dt.DefaultView;
                    }
                }
            }
            catch (Exception ex)
            {
                Const.write_task_log($"加载配置异常 {ex.ToString()}");
            }
        }

        public bool Load_config()
        {
            configs = _Method.Loadconfig();
            if (configs.Count == 0)
            {
                Frm_AddConfig add_Config = new Frm_AddConfig();
                add_Config.ShowDialog();
                configs = _Method.Loadconfig();
            }

            DataTable dt = configs.ToDataTable();
            skinDataGridView1.DataSource = dt.DefaultView;

            if (configs.Count == 1 && configs.FirstOrDefault().system != "Linux")
            {
                Const.config = configs.FirstOrDefault();
                Const.config.tmp_dir = Path.GetTempPath();
                return true;
            }
            return false;
        }
        #endregion

        #region 登录方法
        private void LoginMain()
        {
            bool flag = false;
            index = skinDataGridView1.CurrentRow.Index;
            Const.config = configs[index];
            string message = string.Empty;
            if (configs[index].system == "Linux")
            {
                #region Linux登录
                LoadingHelper.ShowLoading("正在连接服务器，请稍等...", this, (obj) =>
                {
                    flag = _Method.TestPing(ref message);
                });
                if (!flag)
                {
                    if (MessageBox.Show(message, "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        Frm_AddConfig add_Config = new Frm_AddConfig(configs, index);
                        add_Config.ShowDialog();
                        Load_config();
                        return;
                    }
                }
                LoadingHelper.ShowLoading("正在连接服务器，请稍等...", this, (obj) =>
                {
                    flag = _Method.Testcon(ref message);
                });
                if (!flag)
                {
                    if (message == "连接失败，是否使用普通用户尝试？")
                    {
                        if (MessageBox.Show(message, "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            message = "again";
                            LoadingHelper.ShowLoading("正在连接服务器，请稍等...", this, (obj) => { flag = _Method.Testcon(ref message); });
                            if (!flag)
                            {
                                MessageBox.Show(message, "提示");
                                Frm_AddConfig add_Config = new Frm_AddConfig(configs, index);
                                add_Config.ShowDialog();
                                Load_config();
                                return;
                            }
                        }
                        else
                        {
                            Frm_AddConfig add_Config = new Frm_AddConfig(configs, index);
                            add_Config.ShowDialog();
                            Load_config();
                            return;
                        }
                    }
                    else
                    {
                        if (MessageBox.Show(message, "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            Frm_AddConfig add_Config = new Frm_AddConfig(configs, index);
                            add_Config.ShowDialog();
                            Load_config();
                        }
                        return;
                    }
                }
                #endregion
            }
            Isexit = false;
            this.Close();
        }
        #endregion

        #region 双击登录
        private void SkinDataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                return;
            }
            LoginMain();
        }
        #endregion

        #region 添加配置
        private void 添加配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_AddConfig add_Config = new Frm_AddConfig();
            add_Config.ShowDialog();
            Load_config();
        }
        #endregion

        #region 修改配置
        private void 修改配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            index = skinDataGridView1.CurrentRow.Index;
            Frm_AddConfig add_Config = new Frm_AddConfig(configs, index);
            add_Config.ShowDialog();
            Load_config();
        }
        #endregion

        #region 删除
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBoxEx.Show("是否确定删除?", "删除配置", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                index = skinDataGridView1.CurrentRow.Index;
                configs.RemoveAt(index);
                _Method.Saveconfig(configs);
                MessageBoxEx.Show("删除成功");
                Load_config();
            }
        }
        #endregion

        private void Login_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Isexit)
            {
                try
                {
                    Environment.Exit(0);
                }
                catch (Exception)
                {
                    Environment.Exit(0);
                }
            }
            else
            {
                Frm_Main.Createfrm_Main().Show();
                //Mainfrm.CreateMainfrm().Show();
                this.Hide();
                e.Cancel = true;
            }
        }


        private void skinDataGridView1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                LoginMain();
            }
        }

        private void skinDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
