using System.Data;

using Sqler.SqlVersion.DataSource;

using Vit.AutoTemp;
using Vit.AutoTemp.DataProvider;
using Vit.Extensions;

using Vitorm;
using Vitorm.Sql;

namespace App.Module.Sqler.Logical.SqlVersion
{
    public class SqlVersionHelp
    {
        static ModuleRepository moduleRepository;
        public static SqlCode_DataProvider[] sqlCode_DataProviders => moduleRepository.sqlCode_DataProviders;


        static readonly List<IDataProvider> dataProviders = new List<IDataProvider>();

        #region static Init
        public static SqlDbContext CreateDbContext()
        {
            return Data.DataProvider<sqler_version>().CreateSqlDbContext();
        }

        public static IDbConnection CreateOpenedDbConnection()
        {
            var conn = Data.DataProvider<sqler_version>().CreateSqlDbContext()?.dbConnection;
            conn?.Open();
            return conn;
        }



        public static void InitEnvironment()
        {

            #region #1 init dataSource
            {
                var dataSourceConfig = SqlerHelp.sqlerConfig.GetByPath<Dictionary<string, object>>("SqlVersion.Config");
                dataSourceConfig["namespace"] = typeof(sqler_version).Namespace;

                var moduleLabel = "configBy_SqlerSqlVersion";
                dataSourceConfig[moduleLabel] = true;

                Data.ClearDataProviders(provider => provider.dataSourceConfig.ContainsKey(moduleLabel));
                var success = Data.AddDataProvider(dataSourceConfig);
            }
            #endregion

        }



        public static void InitAutoTemp()
        {
            // #1 unregister AutoTemp
            if (dataProviders.Count > 0)
            {
                Vit.AutoTemp.AutoTempHelp.UnRegisterDataProvider(dataProviders.ToArray());
                dataProviders.Clear();
            }
            IDataProvider dataProvider;

            // #2 register SqlVersion Config
            dataProvider = new global::App.Module.Sqler.Logical.SqlVersion.ConfigRepository().ToDataProvider("Sqler_SqlVersion_Config");
            dataProviders.Add(dataProvider);
      

            // #3 register ModuleMng
            moduleRepository = new ModuleRepository();
            dataProvider = moduleRepository.ToDataProvider("Sqler_SqlVersion_Module");
            dataProviders.Add(dataProvider);


            // #4 register sqlCode_DataProviders
            dataProviders.AddRange(sqlCode_DataProviders);


            // #5 register VersionResult(from database)
            dataProvider = AutoTempHelp.CreateDataProvider<sqler_version>("Sqler_SqlVersion_VersionInfo", CreateDbContext);
            dataProviders.Add(dataProvider);



            Vit.AutoTemp.AutoTempHelp.RegisterDataProvider(dataProviders.ToArray());


            // #6 ensure table sqler_version exist
            SqlVersionService.EnsureTableCreate();

        }


        public static void InitEnvironmentAndAutoTemp()
        {
            InitEnvironment();
            InitAutoTemp();
        }

        #endregion





    }
}
