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

namespace PowerEasy.WebFuture.DataMigrations.Foundation.Service
{
    /// <summary>
    /// 数据库基础服务类。
    /// </summary>
    public class DatabaseService
    {
        /// <summary>
        /// 数据库实例。
        /// </summary>
        private static DatabaseService instance;

        /// <summary>
        /// 目标数据库提供者。
        /// </summary>
        private IDatabaseProvider targetDatabaseProvider;

        /// <summary>
        /// 来源数据库提供者。
        /// </summary>
        private IDatabaseProvider sourceDatabaseProvider;

        /// <summary>
        /// 迁移项目标识符。
        /// </summary>
        private string migrationProgrammeIdentifier;

        /// <summary>
        /// 构造函数。
        /// </summary>
        private DatabaseService()
        {
        }

        /// <summary>
        /// 构造函数。
        /// </summary>
        /// <param name="migrationProgrammeIdentifier">迁移项目标识符。</param>
        private DatabaseService(string migrationProgrammeIdentifier)
        {
            this.migrationProgrammeIdentifier = migrationProgrammeIdentifier;
        }

        /// <summary>
        /// 提供一个公有静态方法来获取唯一实例。
        /// </summary>
        /// <param name="migrationProgrammeIdentifier">迁移项目标识符。</param>
        /// <returns>返回实例。</returns>
        public static DatabaseService GetInstance(string migrationProgrammeIdentifier)
        {
            if (instance == null)
            {
                instance = new DatabaseService(migrationProgrammeIdentifier);
            }

            // 返回唯一实例。
            return instance;
        }

        /// <summary>
        /// 添加销毁单例的方法
        /// </summary>
        public static void DestroyInstance()
        {
            instance = null;
        }

        /// <summary>
        /// 获取源数据库提供者。
        /// </summary>
        /// <param name="migrationProgrammeIdentifier">迁移方案标识符。</param>
        /// <returns>返回源数据库提供者。</returns>
        public static IDatabaseProvider GetSourceDatabaseProvider(string migrationProgrammeIdentifier)
        {
            return DatabaseService.GetInstance(migrationProgrammeIdentifier).GetSourceDatabaseProvider();
        }

        /// <summary>
        /// 获取目标数据库提供者。
        /// </summary>
        /// <param name="migrationProgrammeIdentifier">迁移方案标识符。</param>
        /// <returns>返回目标数据库提供者。</returns>
        public static IDatabaseProvider GetTargetDatabaseProvider(string migrationProgrammeIdentifier)
        {
            return DatabaseService.GetInstance(migrationProgrammeIdentifier).GetTargetDatabaseProvider();
        }

        /// <summary>
        /// 获取目标数据库类型。
        /// </summary>
        /// <param name="migrationProgrammeIdentifier">迁移方案标识符。</param>
        /// <returns>返回目标数据库类型。</returns>
        public static DatabaseType GetTargetDatabaseType(string migrationProgrammeIdentifier)
        {
            var migrationProgramme = MigrationProgrammeHelper.GetMigrationProgrammeByIdentifier(migrationProgrammeIdentifier);

            return migrationProgramme.TargetDataBaseType;
        }

        /// <summary>
        /// 获取来源数据库类型。
        /// </summary>
        /// <param name="migrationProgrammeIdentifier">迁移方案标识符。</param>
        /// <returns>返回来源数据库类型。</returns>
        public static DatabaseType GetSourceDatabaseType(string migrationProgrammeIdentifier)
        {
            var migrationProgramme = MigrationProgrammeHelper.GetMigrationProgrammeByIdentifier(migrationProgrammeIdentifier);

            return migrationProgramme.SourceDataBaseType;
        }

        /// <summary>
        /// 获取来源数据库提供者。
        /// </summary>
        /// <returns>返回来源数据库提供者。</returns>
        public IDatabaseProvider GetSourceDatabaseProvider()
        {
            if (this.sourceDatabaseProvider == null)
            {
                var migrationProgramme = MigrationProgrammeHelper.GetMigrationProgrammeByIdentifier(this.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;
                this.sourceDatabaseProvider = sourceDatabaseConnection.CreateProvider();
            }

            return this.sourceDatabaseProvider;
        }

        /// <summary>
        /// 获取目标数据库提供者。
        /// </summary>
        /// <returns>返回目标数据库提供者。</returns>
        public IDatabaseProvider GetTargetDatabaseProvider()
        {
            if (this.targetDatabaseProvider == null)
            {
                var migrationProgramme = MigrationProgrammeHelper.GetMigrationProgrammeByIdentifier(this.migrationProgrammeIdentifier);
                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;
                this.targetDatabaseProvider = targetDatabaseConnection.CreateProvider();
            }

            return this.targetDatabaseProvider;
        }
    }
}
