using PowerEasy.WebFuture.DataMigrations.Foundation.Database;
using PowerEasy.WebFuture.DataMigrations.Foundation.MigrationProgramme;

namespace PowerEasy.WebFuture.DataMigrations
{
    /// <summary>
    /// MainForm。
    /// </summary>
    public partial class MainForm : Form
    {
        /// <summary>
        /// 构造函数。
        /// </summary>
        public MainForm()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// 窗体加载事件。
        /// </summary>
        /// <param name="sender">sender。</param>
        /// <param name="e">e。</param>
        private void MainForm_Load(object sender, EventArgs e)
        {
            this.InitMigrationProgramme();
        }

        /// <summary>
        /// 迁移方案ComboBox控件选择事件。
        /// </summary>
        /// <param name="sender">sender。</param>
        /// <param name="e">e。</param>
        private void ComboBoxMigrationProgramme_SelectedIndexChanged(object sender, EventArgs e)
        {
            var migrationProgrammeIdentifier = this.comboBoxMigrationProgramme.SelectedValue.ToString();
            this.InitDataBaseConnect(migrationProgrammeIdentifier);
        }

        /// <summary>
        /// 下一步按钮点击事件。
        /// </summary>
        /// <param name="sender">sender。</param>
        /// <param name="e">e。</param>
        private void ButtonNextStep_Click(object sender, EventArgs e)
        {
            var migrationProgrammeIdentifier = this.comboBoxMigrationProgramme.SelectedValue.ToString();
            this.UpdateDataBaseConnect(migrationProgrammeIdentifier);
            if (!this.TestDataBaseConnect(migrationProgrammeIdentifier))
            {
                return;
            }

            var migrationForm = new MigrationForm(migrationProgrammeIdentifier);
            migrationForm.ShowDialog(this);
        }

        /// <summary>
        /// 初始化迁移方案ComboBox控件。
        /// </summary>
        private void InitMigrationProgramme()
        {
            this.comboBoxMigrationProgramme.DataSource = MigrationProgrammeHelper.GetMigrationProgrammeDataSource();
            this.comboBoxMigrationProgramme.DisplayMember = "Value";
            this.comboBoxMigrationProgramme.ValueMember = "Key";
        }

        /// <summary>
        /// 初始化数据库连接。
        /// </summary>
        /// <param name="migrationProgrammeIdentifier">迁移方案标识符。</param>
        private void InitDataBaseConnect(string migrationProgrammeIdentifier)
        {
            this.groupBoxSourceDataBase.Text = "源数据库 ";
            this.groupBoxTargetDataBase.Text = "目标数据库 ";

            this.textBoxSourceDataBaseServer.Text = string.Empty;
            this.textBoxSourceDataBasePort.Text = string.Empty;
            this.textBoxSourceDataBaseName.Text = string.Empty;
            this.textBoxSourceDataBaseUserName.Text = string.Empty;
            this.textBoxSourceDataBasePassword.Text = string.Empty;

            this.textBoxTargetDataBaseServer.Text = string.Empty;
            this.textBoxTargetDataBasePort.Text = string.Empty;
            this.textBoxTargetDataBaseName.Text = string.Empty;
            this.textBoxTargetDataBaseUserName.Text = string.Empty;
            this.textBoxTargetDataBasePassword.Text = string.Empty;

            this.buttonNextStep.Enabled = false;

            var migrationProgramme = MigrationProgrammeHelper.GetMigrationProgrammeByIdentifier(migrationProgrammeIdentifier);

            if (migrationProgramme != null)
            {
                this.groupBoxSourceDataBase.Text = this.groupBoxSourceDataBase.Text + migrationProgramme.SourceDataBaseType.ToString();
                this.groupBoxTargetDataBase.Text = this.groupBoxTargetDataBase.Text + migrationProgramme.TargetDataBaseType.ToString();

                this.textBoxSourceDataBaseServer.Text = migrationProgramme.SourceDataBaseServer;
                this.textBoxSourceDataBasePort.Text = migrationProgramme.SourceDataBasePort;
                this.textBoxSourceDataBaseName.Text = migrationProgramme.SourceDataBaseName;
                this.textBoxSourceDataBaseUserName.Text = migrationProgramme.SourceDataBaseUserName;
                this.textBoxSourceDataBasePassword.Text = migrationProgramme.SourceDataBasePassword;

                this.textBoxTargetDataBaseServer.Text = migrationProgramme.TargetDataBaseServer;
                this.textBoxTargetDataBasePort.Text = migrationProgramme.TargetDataBasePort;
                this.textBoxTargetDataBaseName.Text = migrationProgramme.TargetDataBaseName;
                this.textBoxTargetDataBaseUserName.Text = migrationProgramme.TargetDataBaseUserName;
                this.textBoxTargetDataBasePassword.Text = migrationProgramme.TargetDataBasePassword;

                this.buttonNextStep.Enabled = true;
            }
        }

        /// <summary>
        /// 更新数据库连接。
        /// </summary>
        /// <param name="migrationProgrammeIdentifier">迁移方案标识符。</param>
        private void UpdateDataBaseConnect(string migrationProgrammeIdentifier)
        {
            var migrationProgramme = MigrationProgrammeHelper.GetMigrationProgrammeByIdentifier(migrationProgrammeIdentifier);

            if (migrationProgramme != null)
            {
                migrationProgramme.SourceDataBaseServer = this.textBoxSourceDataBaseServer.Text;
                migrationProgramme.SourceDataBasePort = this.textBoxSourceDataBasePort.Text;
                migrationProgramme.SourceDataBaseName = this.textBoxSourceDataBaseName.Text;
                migrationProgramme.SourceDataBaseUserName = this.textBoxSourceDataBaseUserName.Text;
                migrationProgramme.SourceDataBasePassword = this.textBoxSourceDataBasePassword.Text;

                migrationProgramme.TargetDataBaseServer = this.textBoxTargetDataBaseServer.Text;
                migrationProgramme.TargetDataBasePort = this.textBoxTargetDataBasePort.Text;
                migrationProgramme.TargetDataBaseName = this.textBoxTargetDataBaseName.Text;
                migrationProgramme.TargetDataBaseUserName = this.textBoxTargetDataBaseUserName.Text;
                migrationProgramme.TargetDataBasePassword = this.textBoxTargetDataBasePassword.Text;
            }
        }

        /// <summary>
        /// 测试数据库连接。
        /// </summary>
        /// <param name="migrationProgrammeIdentifier">迁移方案标识符。</param>
        /// <returns>是否测试成功。</returns>
        private bool TestDataBaseConnect(string migrationProgrammeIdentifier)
        {
            var migrationProgramme = MigrationProgrammeHelper.GetMigrationProgrammeByIdentifier(migrationProgrammeIdentifier);

            var sourceDatabaseConnection = new DataBaseConnectionHelper();
            sourceDatabaseConnection.DatabaseType = migrationProgramme.SourceDataBaseType;
            sourceDatabaseConnection.ServerName = migrationProgramme.SourceDataBaseServer;
            sourceDatabaseConnection.Port = migrationProgramme.SourceDataBasePort;
            sourceDatabaseConnection.Database = migrationProgramme.SourceDataBaseName;
            sourceDatabaseConnection.UserName = migrationProgramme.SourceDataBaseUserName;
            sourceDatabaseConnection.Password = migrationProgramme.SourceDataBasePassword;
            var sourceDatabaseProvider = sourceDatabaseConnection.CreateProvider();

            if (!sourceDatabaseProvider.CheckConnection())
            {
                MessageBox.Show("源数据库无法连接，请确认连接信息是否有误。", "系统信息");
                return false;
            }

            var targetDatabaseConnection = new DataBaseConnectionHelper();
            targetDatabaseConnection.DatabaseType = migrationProgramme.TargetDataBaseType;
            targetDatabaseConnection.ServerName = migrationProgramme.TargetDataBaseServer;
            targetDatabaseConnection.Port = migrationProgramme.TargetDataBasePort;
            targetDatabaseConnection.Database = migrationProgramme.TargetDataBaseName;
            targetDatabaseConnection.UserName = migrationProgramme.TargetDataBaseUserName;
            targetDatabaseConnection.Password = migrationProgramme.TargetDataBasePassword;
            var targetDatabaseProvider = targetDatabaseConnection.CreateProvider();

            if (!targetDatabaseProvider.CheckConnection())
            {
                MessageBox.Show("目标数据库无法连接，请确认连接信息是否有误。", "系统信息");
                return false;
            }

            return true;
        }
    }
}