using Aitex.Common.Util;
using Aitex.Core.RT.ConfigCenter;
using Aitex.Core.RT.DataCenter;
using Aitex.Core.RT.DataCollection;
using Aitex.Core.RT.DBCore;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.Log;
using Aitex.Core.RT.OperationCenter;
using Aitex.Core.RT.RecipeCenter;
using Aitex.Core.RT.SCCore;
using Aitex.Core.Util;
using Aitex.Core.WCF;
using MECF.Framework.Common.Account;
using MECF.Framework.Common.Communications;
using MECF.Framework.Common.Equipment;
using MECF.Framework.Common.IOCore;
using MECF.Framework.Common.SCCore;
using MECF.Framework.Common.SubstrateTrackings;
using MECF.Framework.RT.Core.Applications;
using MECF.Framework.RT.Core.Backend;
using MECF.Framework.RT.Core.IoProviders;
using SorterRT.Modules;
using SicRT.Equipments.Systems;
using SicModules.PMs.RecipeExecutions;
using MECF.Framework.UI.Client.TrayThickness.HistoryData;
using MECF.Framework.Common.Aitex.Core.Common.DeviceData;
using MECF.Framework.Common.MultiProcess.ProcessCenter;
using MECF.Framework.Common.Aitex.Core.RT.EMS;
using Aitex.Core.RT.DataCollection.HighPerformance;
using Aitex.Core.RT.IOCore.Interlock;
using MECF.Framework.Common.Gem;


namespace SicRT.Instances
{
    internal class ToolLoader : IRtLoader
    {
        #region Variables

        

        #endregion

        public void Initialize()
        {
            #region Prepare Env

            Singleton<LogManager>.Instance.Initialize();
            LOG.Write($"RT v{RtInstance.SystemVersion} launch ...");
            Singleton<ConfigManager>.Instance.Initialize();
            Singleton<DataBaseManager>.Instance.Initialize();
            Singleton<DataManager>.Instance.Initialize();
            Singleton<OperationManager>.Instance.Initialize();
            Singleton<EventManager>.Instance.Initialize(PathManager.GetCfgDir() + "EventDefine.xml");
            Singleton<SystemConfigManager>.Instance.Initialize(PathManager.GetCfgDir() + "System.sccfg");
            RecipeFileManager.Instance.Initialize(new SicRecipeFileContext());
            AccountExManager.Instance.Initialize(true);
            #endregion

            ConnectionManager.Instance.Initialize();

            MultiProcessClient.Instance.Initialize();

            string ioProviderPathFile = PathManager.GetCfgDir() + "IoProviderConfig.xml";

            IoProviderManager.Instance.Initialize(ioProviderPathFile);

            WaferManager.Instance.Initialize();

            Singleton<DeviceManager>.Instance.DisableAsyncInitialize = true;

            Singleton<DeviceManager>.Instance.Initialize(PathManager.GetCfgDir() + "pm\\pm1\\DeviceModelPM1.xml", "Sic",
                ModuleName.PM1, "PM1", false);
            Singleton<DeviceManager>.Instance.Initialize(PathManager.GetCfgDir() + "pm\\pm2\\DeviceModelPM2.xml", "Sic",
                ModuleName.PM2, "PM2", false);
            Singleton<DeviceManager>.Instance.Initialize(PathManager.GetCfgDir() + "tm\\DeviceModelSystem.xml", "Sic",
                ModuleName.System, "TM", true);

            //测温初始化
            Singleton<DeviceManager>.Instance.Initialize(PathManager.GetCfgDir() + "Common\\Common.xml", "Sic",
                ModuleName.System, "System", false);

            RtSystemManager.Instance.AddCustomBackend("SC", new BackendSCConfigView());

            Singleton<EquipmentManager>.Instance.Initialize();

            //! ！！！！注意 ！！！！！！
            // 注意构建和启动DeviceManager状态机的时机，不要早于EquipmentManager，因为EquipmentManger中
            // 的InitModules()方法构建模组时，如果模组被配置为NotInstalled，则模组所属的Device会被禁用。
            // 若过早的使能DeviceManager，被卸载的Module可能产生误报警。
            Singleton<DeviceEntity>.Instance.Initialize();

            // Singleton<RouteManager>.Instance.Initialize();
            //Singleton<FAJobManager>.Instance.Initialize();

            // make sure the PLC data has been synced at least once to avoid erroneous warnings or alarms.
            IoProviderManager.Instance.WaitFirstSync(5000);

            HistoryCoatingManager.Instance.Initialize();

            //if (Singleton<EquipmentManager>.Instance.Modules[ModuleName.TM].IsInstalled)
            //    Singleton<InterlockManager>.Instance.Initialize(PathManager.GetCfgDir() + "tm\\interlock.xml",
            //        IoManager.Instance);

            if (Singleton<EquipmentManager>.Instance.Modules[ModuleName.PM1].IsInstalled)
                Singleton<InterlockManager>.Instance.Initialize($"{PathManager.GetCfgDir()}pm\\pm1\\interlockPM1.xml",
                    IoManager.Instance);

            //if (Singleton<EquipmentManager>.Instance.Modules[ModuleName.PM2].IsInstalled)
            //    Singleton<InterlockManager>.Instance.Initialize($"{PathManager.GetCfgDir()}pm\\pm2\\interlockPM2.xml",
            //        IoManager.Instance);

            MaintainManager2.Instance.Initialize();

            Singleton<ProcessHistoryItemsDefault>.Instance.Initialize();


            #region Post Prepare Env

            //! DataCollectionManager必须在最后初始化，否则在其之后注册的DATA数据均会被忽略

            //Singleton<DataCollectionManager>.Instance.Initialize();
            Singleton<DataTraceManager>.Instance.Initialize(["System", "IO.PM1", "IO.PM2", "IO.TM", "PM1", "PM2"]);

            //开启循环删除数据库
            Singleton<DataBaseCleanerManager>.Instance.Initialize();

            #endregion

            if(SC.GetValue<bool>("Gem.Enable"))
            {
                GemManager.Instance.Initialize();
            }
        }


        public void Terminate()
        {
            GemManager.Instance?.Terminate();

            Singleton<DeviceEntity>.Instance.Terminate();

            Singleton<DeviceManager>.Instance.Terminate();

            IoProviderManager.Instance.Terminate();

            Singleton<SystemConfigManager>.Instance.Terminate();

            Singleton<DataCollectionManager>.Instance.Terminate();

            Singleton<WcfServiceManager>.Instance.Terminate();

            Singleton<EventManager>.Instance.Terminate();

            Singleton<LogManager>.Instance.Terminate();

            Singleton<DataBaseManager>.Instance.Terminate();
        }
    }
}
