using System.Collections.Generic;
using System.Diagnostics;
using Aitex.Core.RT.Device;
using Aitex.Core.RT.Event;
using Aitex.Core.RT.Routine;
using Aitex.Core.RT.SCCore;
using Aitex.Core.Util;
using MECF.Framework.Common.Equipment;
using SicModules.Buffers;
using SicModules.LLs;
using SicModules.PMs;
using SicModules.TMs;
using SicRT.Equipments.Systems;

namespace SicRT.Modules
{
    public class HomeAll
    {
        private bool IsPM2Install = SC.GetConfigItem("System.SetUp.IsPM2Installed").BoolValue;

        List<List<IModuleDevice>> _lstModules = new List<List<IModuleDevice>>();
        List<IModuleDevice> _homingModules = new List<IModuleDevice>();

        public HomeAll()
        {

        }

        public Result Start(params object[] objs)
        {
            if(Debugger.IsAttached)
                EV.PostInfoLog("System",
                "这是一个长字符串用于测试MainWindow的LOG下拉框是否约束了显示宽度这是一个长字符串用于测试MainWindow的LOG下拉框是否约束了显示宽度这是一个长字符串用于测试MainWindow的LOG下拉框是否约束了显示宽度");

            _lstModules.Clear();
            _homingModules.Clear();

            var dicModules = new Dictionary<string, bool>();

            var tm = Singleton<EquipmentManager>.Instance.Modules[ModuleName.TM] as TMModule;
            List<IModuleDevice> lstA = new List<IModuleDevice>();
            if (tm.IsInstalled)
            {
                lstA.Add(Singleton<EquipmentManager>.Instance.Modules[ModuleName.TM] as IModuleDevice );
                dicModules.Add(tm.Module, tm.IsBusy);
            }

            var lstModules = new List<IModuleDevice>();

            _lstModules.Add(lstA);


            var ll = Singleton<EquipmentManager>.Instance.Modules[ModuleName.LoadLock] as LoadLockModule;
            if (ll.IsInstalled)
            {
                lstModules.Add(Singleton<EquipmentManager>.Instance.Modules[ModuleName.LoadLock] as IModuleDevice);
                dicModules.Add(ll.Module, ll.IsBusy);
            }

            var buffer = Singleton<EquipmentManager>.Instance.Modules[ModuleName.Buffer] as BufferModule;
            if (buffer.IsInstalled)
            {
                lstModules.Add(Singleton<EquipmentManager>.Instance.Modules[ModuleName.Buffer] as IModuleDevice);
                dicModules.Add(buffer.Module, buffer.IsBusy);
            }

            var pm1 = Singleton<EquipmentManager>.Instance.Modules[ModuleName.PM1] as PMModule;
            if (pm1.IsInstalled)
            {
                lstModules.Add(Singleton<EquipmentManager>.Instance.Modules[ModuleName.PM1] as IModuleDevice);
                dicModules.Add(pm1.Module, pm1.IsBusy);
            }

            var pm2 = Singleton<EquipmentManager>.Instance.Modules[ModuleName.PM2] as PMModule;
            if (pm2.IsInstalled)
            {
                lstModules.Add(Singleton<EquipmentManager>.Instance.Modules[ModuleName.PM2] as IModuleDevice);
                dicModules.Add(pm2.Module, pm2.IsBusy);
            }

            _lstModules.Add(lstModules);


            foreach (var item in dicModules)
            {
                if (item.Value)
                {
                    EV.PostWarningLog("System", $"{item.Key} is busy，can not do Initialize");
                    return Result.FAIL;
                }
            }

            return Result.RUN;
        }

        public Result Monitor(params object[] objs)
        {
            if (_homingModules.Count == 0 && _lstModules.Count == 0)
                return Result.DONE;

            if (_homingModules.Count > 0)
            {
                foreach (var module in _homingModules)
                {
                    if (module.IsError)
                        return Result.FAIL;

                    if (!module.IsReady)
                        return Result.RUN;
                }

                _homingModules.Clear();
                if (_lstModules.Count == 0)
                    return Result.DONE;
            }

            if (_homingModules.Count == 0)
            {
                foreach (var moduleEntity in _lstModules[0])
                {
                    if (!moduleEntity.IsReady)
                    {
                        if (!moduleEntity.Home(out string reason))
                            return Result.FAIL;

                        _homingModules.Add(moduleEntity);
                    }
                }

                _lstModules.RemoveAt(0);
            }


            return Result.RUN;
        }

        public void Clear()
        {
            _lstModules.Clear();
            _homingModules.Clear();
        }


    }
}
