using Tiobon.Core.OPS.Tool.OPS.Tool.Model;
using Tiobon.Core.OPS.Tool.OPS.Tool.Src;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace Tiobon.Core.OPS.Tool.OPS.Tool.View
{
    public partial class Frm_AddConfig : Form
    {
        #region 字段或属性
        Login_method _Method = new Login_method();
        bool flag = true;
        List<Config> _config = new List<Config>();
        int _index;
        #endregion

        #region 构造
        public Frm_AddConfig()
        {
            InitializeComponent();
        }

        public Frm_AddConfig(List<Config> config, int index)
        {
            InitializeComponent();
            this.Text = "修改配置";
            _config = config;
            _index = index;
            rad_Windows.Checked = true;
            if (config[index].system == "Linux")
            {
              
                rad_linux.Checked = true;
            }
            txt_ip.Text = config[index].ip;
            txt_pwd.Text = config[index].ssh_passwd;
            txt_port.Text = config[index].ssh_port;
            txt_user.Text = config[index].ssh_user;
            txt_supwd.Text = config[index].supasswd;
            txt_dbpwd.Text = config[index].db_passwd;
            txt_dbuser.Text = config[index].db_user;
            txt_name.Text = config[index].name; 
            txt_dbport.Text = config[index].db_port;
            //IsCloudDB.Checked = config[index].IsCloudDB;
            //if (IsCloudDB.Checked)
            //{
            //    txt_cloud_db_value.Visible = true;
            //    CouldDbAddresLabel.Visible = true;
            //}
            //else
            //{
            //    txt_cloud_db_value.Visible = false;
            //    CouldDbAddresLabel.Visible = false;
            //}
            txt_cloud_db_value.Text = config[index].CloudDBConnectionString;
            flag = !flag;

        }
        #endregion

        #region 按钮事件
        private void Rad_Windows_CheckedChanged(object sender, EventArgs e)
        {
            if (rad_Windows.Checked == true)
            {
                txt_dbpwd.Enabled = false;
                //txt_name.Enabled = false;
                txt_dbuser.Enabled = false;
                txt_dbport.Enabled = false; 
            }
        }

        private void Rad_linux_CheckedChanged(object sender, EventArgs e)
        {
            if (rad_linux.Checked == true)
            {
                txt_dbpwd.Enabled = true;
                //txt_name.Enabled = true;
                txt_dbuser.Enabled = true;
                txt_dbport.Enabled = true;
            }
        }
        #endregion

        #region 保存
        private void Button1_Click(object sender, EventArgs e)
        {
            if (rad_Windows.Checked)
            {
                if (string.IsNullOrEmpty(txt_name.Text))
                {
                    MessageBoxEx.Show("不能为空！", "提示");
                    return;
                } 
                if (string.IsNullOrEmpty(txt_dbuser.Text))
                {
                    MessageBoxEx.Show("不能为空！", "提示");
                    return;
                }
                if (string.IsNullOrEmpty(txt_dbpwd.Text))
                {
                    MessageBoxEx.Show("不能为空！", "提示");
                    return;
                }
                //if (Directory.Exists(txt_localdir.Text.Trim()))
                //{
                //    DirectoryInfo dir = new DirectoryInfo(txt_localdir.Text.Trim());
                //    var files = dir.GetFiles("*", SearchOption.AllDirectories);
                //    if (files.Length == 0)
                //    {
                //        if (MessageBox.Show("目录下已存在文件，一键安装将会清空目录下文件!\r\n是否确定保存？", "警告", MessageBoxButtons.YesNo) == DialogResult.No)
                //        {
                //            return;
                //        }
                //    }
                //}
            }
            if (rad_linux.Checked)
            {
                foreach (var item in this.Controls)
                {
                    if (item is TextBox)
                    {
                        if (string.IsNullOrEmpty((item as TextBox).Text))
                        {
                            if ((item as TextBox).Name == "txt_localdir" || (item as TextBox).Name == "txt_cloud_db_value")
                            {
                                continue;
                            }
                            MessageBoxEx.Show("不能为空！", "提示");
                            return;
                        }
                    }
                }
            }


            Config config = new Config
            {
                ID = Guid.NewGuid().ToString("N"),
                ip = txt_ip.Text.Trim(),
                db_passwd = txt_dbpwd.Text.Trim(),
                db_port = txt_dbport.Text.Trim(),
                db_user = txt_dbuser.Text.Trim(),
                //install_dir = rad_linux.Checked ? "/home/" + txt_user.Text.Trim() + "/ihdis" : txt_localdir.Text.Trim(),
                ssh_passwd = txt_pwd.Text.Trim(),
                ssh_port = txt_port.Text.Trim(),
                ssh_user = txt_user.Text.Trim(),
                system = rad_linux.Checked ? "Linux" : "Windows",
                name = txt_name.Text.Trim(),
                supasswd = txt_supwd.Text.Trim(),
                CloudDBConnectionString = txt_cloud_db_value.Text
            };
            if (flag)
            {
                _Method.Saveconfig(config);
                MessageBoxEx.Show("保存成功");
            }
            else
            {
                _config[_index].ip = txt_ip.Text.Trim();
                _config[_index].db_passwd = txt_dbpwd.Text.Trim();
                _config[_index].db_port = txt_dbport.Text.Trim();
                _config[_index].db_user = txt_dbuser.Text.Trim();
                _config[_index].tmp_dir = rad_linux.Checked ? "/home/" + txt_user.Text.Trim() + "/ihdis" : Path.GetTempPath();
                //_config[_index].install_dir = rad_linux.Checked ? "/home/" + txt_user.Text.Trim() + "/ihdis" : txt_localdir.Text.Trim();
                _config[_index].ssh_passwd = txt_pwd.Text.Trim();
                _config[_index].ssh_port = txt_port.Text.Trim();
                _config[_index].ssh_user = txt_user.Text.Trim();
                _config[_index].system = rad_linux.Checked ? "Linux" : "Windows";
                _config[_index].name = txt_name.Text.Trim();
                _config[_index].supasswd = txt_supwd.Text.Trim();
                _config[_index].CloudDBConnectionString = txt_cloud_db_value.Text.Trim();
                _Method.Saveconfig(_config);
                MessageBoxEx.Show("更新成功");
            }
            this.Close();
        }
        #endregion

        #region 返回
        private void Button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        private void CouldDbAddresLabel_Click(object sender, EventArgs e)
        {

        }

        private void Frm_AddConfig_Load(object sender, EventArgs e)
        {

        }
    }
}
