using App.Module.Sqler.Logical.DataEditor.GridManage;

using Sqler.Module.Sqler.Logical.DataEditor.GridManage;

using Vit.Core.Module.Log;


namespace App.Module.Sqler.Logical.DataEditor
{
    public class DataEditorHelp
    {
        const string configNamespace = "Sqler.DataEditor.Config";
        public const string entityNamespace = "Sqler.DataEditor.Entity";


        public static void Init()
        {

            Logger.Info("[Sqler][DataEditor] init ...");


            if (!RegisterDataProvider())
            {
                Logger.Info("[Sqler][DataEditor] not config Database Connection, not inited.");
                return;
            }

            Logger.Info("[Sqler][DataEditor] init succeed!");
        }


        static bool RegisterDataProvider()
        {
            // #1 unregister DataProvider
            if (GridService.dataProviders?.Any() == true)
            {
                Vit.AutoTemp.AutoTempHelp.UnRegisterDataProvider(GridService.dataProviders);
                GridService.dataProviders = null;
            }


            // #2 GridManage
            {
                try
                {
                    Vit.AutoTemp.AutoTempHelp.RegisterDataProvider(new GridManageRepository().ToDataProvider());
                }
                catch (System.Exception ex)
                {
                    Logger.Error(ex);
                }
            }

            // #3 BuildDataProvidersFromConfig
            {
                try
                {
                    var dataProviders = GridService.BuildDataProvidersFromConfig();
                    Vit.AutoTemp.AutoTempHelp.RegisterDataProvider(dataProviders);
                }
                catch (System.Exception ex)
                {
                    Logger.Error(ex);
                }
            }

            return true;
        }





    }
}
