using PowerEasy.WebFuture.DataMigrations.Foundation.Enum;
using PowerEasy.WebFuture.DataMigrations.Foundation.Provider;

namespace PowerEasy.WebFuture.DataMigrations.Foundation.Database
{
    /// <summary>
    /// 数据库链接。
    /// </summary>
    public class DataBaseConnectionHelper
    {
        /// <summary>
        /// 数据库类型。
        /// </summary>
        public DatabaseType DatabaseType { get; set; }

        /// <summary>
        /// 服务器名字。
        /// </summary>
        public string ServerName { get; set; }

        /// <summary>
        /// 服务器端口。
        /// </summary>
        public string Port { get; set; }

        /// <summary>
        /// 身份验证。
        /// </summary>
        public AuthenticationType AuthenticationType { get; set; } = AuthenticationType.SqlServer;

        /// <summary>
        /// 数据库名字。
        /// </summary>
        public string Database { get; set; }

        /// <summary>
        /// 数据库登录用户名字。
        /// </summary>
        public string UserName { get; set; }

        /// <summary>
        /// 数据库登录用户密码。
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 获取数据库链接字符串。
        /// </summary>
        /// <returns>返回数据库链接字符串。</returns>
        public string GetConnectionString()
        {
            var databaseName = this.Database;
            switch (this.DatabaseType)
            {
                case DatabaseType.SqlServer:
                    switch (this.AuthenticationType)
                    {
                        case AuthenticationType.Windows:
                            return $"Data Source={this.ServerName};Initial Catalog={databaseName};Integrated Security=True;Trusted_Connection=True;Encrypt=False;";
                        case AuthenticationType.SqlServer:
                            return $"Data Source={this.ServerName};Initial Catalog={databaseName};User ID={this.UserName};Password={this.Password}";
                        default:
                            return string.Empty;
                    }

                case DatabaseType.MySql:
                    return $"Server={this.ServerName};Port={this.Port};Database={databaseName};User Id={this.UserName};Password={this.Password}";
                case DatabaseType.PostgreSql:
                    return $"Host={this.ServerName};Port={this.Port};Database={databaseName};Username={this.UserName};Password={this.Password}";
                case DatabaseType.Oracle:
                    return $"Data Source={this.ServerName};User Id={this.UserName};Password={this.Password}";
                case DatabaseType.DaMeng:
                    return $"Server={this.ServerName};User={this.UserName};Password={this.Password}";
                case DatabaseType.KingBase:
                    return $"Server={this.ServerName};Port={this.Port};Database={databaseName};User Id={this.UserName};Password={this.Password}";
                default:
                    return string.Empty;
            }
        }

        /// <summary>
        /// 创建数据库实例。
        /// </summary>
        /// <returns>返回数据库实例。</returns>
        public IDatabaseProvider CreateProvider()
        {
            switch (this.DatabaseType)
            {
                case DatabaseType.SqlServer:
                    return new SqlServerProvider(this.GetConnectionString());
                case DatabaseType.MySql:
                    return new MySqlProvider(this.GetConnectionString());
                case DatabaseType.PostgreSql:
                    return new PostgreSqlProvider(this.GetConnectionString());
                case DatabaseType.Oracle:
                    return new OracleProvider(this.GetConnectionString());
                case DatabaseType.DaMeng:
                    return new DaMengProvider(this.GetConnectionString());
                case DatabaseType.KingBase:
                    return new KingBaseProvider(this.GetConnectionString());
                default:
                    return new SqlServerProvider(this.GetConnectionString());
            }
        }
    }
}
