using System;
using System.Collections.Generic;
using System.IO;
using Aitex.Core.Common;
using Aitex.Core.RT.Device;
using Aitex.Core.RT.OperationCenter;
using Aitex.Core.RT.SCCore;
using Aitex.RT.Device.Custom;
using MECF.Framework.Common.Equipment;
using Venus_RT.Modules;
using Venus_RT.Devices;
using Venus_RT.Devices.EPD;
using Venus_Core;
using Aitex.Common.Util;
using System.Reflection;

namespace Venus_RT.Instances
{
    public class DeviceEntity : DeviceEntityT<DeviceManager>
    {
        public DeviceEntity()
        {
        }
    }

    public class DeviceManager : DeviceManagerBase
    {
        private  string device_model_file;
        private readonly string device_model_file_MF;
        private readonly string device_model_file_SEMF;
        public DeviceManager()
        {
            //switch (RtInstance.CurrentChamber)
            //{
            //    case JetChamber.Venus:
            //        device_model_file= PathManager.GetCfgDir() + RtInstance.CurrentChamber.ToString() + "\\" + "DeviceModelVenus.xml.xml";
            //        break;

            //    case JetChamber.Kepler2300:
            //    case JetChamber.Kepler2200A:
            //    case JetChamber.Kepler2200B:
            //        device_model_file = PathManager.GetCfgDir() + RtInstance.CurrentChamber.ToString() + "\\" + "DeviceModelKepler.xml.xml";
            //        break;
            //}

            device_model_file_MF = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config\\","TM", RtInstance.DeviceModelFileName_MF);
            device_model_file_SEMF = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config\\", "TM", RtInstance.DeviceModelFileName_SE);
        }

        public override bool Initialize()
        {
            if (ModuleHelper.IsInstalled(ModuleName.PMA))
            {
                JetChamber jetChamber = (JetChamber)SC.GetValue<int>("PMA.ChamberType");
                InitPM(ModuleName.PMA, jetChamber);
            }
            if (ModuleHelper.IsInstalled(ModuleName.PMB))
            {
                JetChamber jetChamber = (JetChamber)SC.GetValue<int>("PMB.ChamberType");
                InitPM(ModuleName.PMB, jetChamber);
            }
            if (ModuleHelper.IsInstalled(ModuleName.PMC))
            {
                JetChamber jetChamber = (JetChamber)SC.GetValue<int>("PMC.ChamberType");
                InitPM(ModuleName.PMC, jetChamber);
            }
            if (ModuleHelper.IsInstalled(ModuleName.PMD))
            {
                JetChamber jetChamber = (JetChamber)SC.GetValue<int>("PMD.ChamberType");
                InitPM(ModuleName.PMD, jetChamber);
            }

            if (ModuleHelper.IsInstalled(ModuleName.TM))
                InitTM(ModuleName.TM);

            if(ModuleHelper.IsInstalled(ModuleName.SETM))
            {
                InitSETM(ModuleName.SETM);
            }

            AddCustomModuleDevice(new VenusSignalTower("System", "SignalTower"));


            OP.Subscribe("DeviceOperation", this.Invoke);

            return true;
        }

        private void InitPM(ModuleName mod,JetChamber jetChamber)
        {
            //switch (jetChamber)
            //{
            //    case JetChamber.Venus:
            //        device_model_file = PathManager.GetCfgDir() +"PM"+"\\"+ jetChamber.ToString() + "\\" + "VenusDeviceModel.xml";
            //        break;

            //    case JetChamber.Kepler2300:
            //        device_model_file = PathManager.GetCfgDir() + "PM" + "\\" + jetChamber.ToString() + "\\" + "Kepler2300DeviceModel.xml";

            //        break;
            //    case JetChamber.Kepler2200A:
            //    case JetChamber.Kepler2200B:
            //        device_model_file = PathManager.GetCfgDir() + jetChamber.ToString() + "\\" + "DeviceModelKepler.xml";
            //        break;
            //    default:
            //        break;
            //}
            device_model_file = PathManager.GetCfgDir() + "PM" + "\\" + jetChamber.ToString() + "\\" + $"{jetChamber.ToString()}DeviceModel.xml";
            Initialize(device_model_file, jetChamber.ToString(), mod, mod.ToString());

            if (SC.GetValue<int>($"{mod}.Rf.CommunicationType") == (int)CommunicationType.RS232 &&
                SC.GetValue<int>($"{mod}.Rf.MFG") == (int)GeneratorMFG.AdTec)
            {
                AddCustomModuleDevice(new AdTecGenerator(mod, Venus_Core.VenusDevice.Rf));
            }

            if (SC.GetValue<bool>($"{mod}.BiasRf.EnableBiasRF"))
            {
                if(SC.GetValue<int>($"{mod}.BiasRf.CommunicationType") == (int)CommunicationType.Ethernet &&
                SC.GetValue<int>($"{mod}.BiasRf.MFG") == (int)GeneratorMFG.Comet)
                {
                    AddCustomModuleDevice(new CometRF(mod, SC.GetStringValue($"{mod}.BiasRf.IPAddress")));
                }
                else if(SC.GetValue<int>($"{mod}.BiasRf.MFG") == (int)GeneratorMFG.AdTec)
                {
                    AddCustomModuleDevice(new AdTecGenerator(mod, Venus_Core.VenusDevice.BiasRf));
                }
            }
                

            if (SC.GetValue<bool>($"{mod}.Chiller.EnableChiller") &&
                SC.GetValue<int>($"{mod}.Chiller.CommunicationType") == (int)CommunicationType.RS232)
            {
                if(SC.GetValue<int>($"{mod}.Chiller.MFG") == (int)ChillerMFG.SMC)
                {
                    AddCustomModuleDevice(new SMCChiller(mod, "Chiller"));
                }
                else if(SC.GetValue<int>($"{mod}.Chiller.MFG") == (int)ChillerMFG.AIRSYS)
                {
                    AddCustomModuleDevice(new AIRSYSChiller(mod, "Chiller"));
                }
            }
            if (SC.GetValue<bool>($"{mod}.InnerChiller.EnableChiller") &&
               SC.GetValue<int>($"{mod}.InnerChiller.CommunicationType") == (int)CommunicationType.RS232)
            {
                if (SC.GetValue<int>($"{mod}.InnerChiller.MFG") == (int)ChillerMFG.SMC)
                {
                    AddCustomModuleDevice(new SMCChiller(mod, "InnerChiller"));
                }
                else if (SC.GetValue<int>($"{mod}.InnerChiller.MFG") == (int)ChillerMFG.AIRSYS)
                {
                    AddCustomModuleDevice(new AIRSYSChiller(mod, "InnerChiller"));
                }
            }
            if (SC.GetValue<bool>($"{mod}.OuterChiller.EnableChiller") &&
               SC.GetValue<int>($"{mod}.OuterChiller.CommunicationType") == (int)CommunicationType.RS232)
            {
                if (SC.GetValue<int>($"{mod}.OuterChiller.MFG") == (int)ChillerMFG.SMC)
                {
                    AddCustomModuleDevice(new SMCChiller(mod, "OuterChiller"));
                }
                else if (SC.GetValue<int>($"{mod}.OuterChiller.MFG") == (int)ChillerMFG.AIRSYS)
                {
                    AddCustomModuleDevice(new AIRSYSChiller(mod, "OuterChiller"));
                }
            }
            if (SC.GetValue<bool>($"{mod}.TopChiller.EnableChiller") &&
              SC.GetValue<int>($"{mod}.TopChiller.CommunicationType") == (int)CommunicationType.RS232)
            {
                if (SC.GetValue<int>($"{mod}.TopChiller.MFG") == (int)ChillerMFG.SMC)
                {
                    AddCustomModuleDevice(new SMCChiller(mod, "TopChiller"));
                }
                else if (SC.GetValue<int>($"{mod}.TopChiller.MFG") == (int)ChillerMFG.AIRSYS)
                {
                    AddCustomModuleDevice(new AIRSYSChiller(mod, "TopChiller"));
                }
            }
            if (SC.GetValue<bool>($"{mod}.Match.EnableMatch") &&
                SC.GetValue<int>($"{mod}.Match.CommunicationType") == (int)CommunicationType.RS232 &&
                SC.GetValue<int>($"{mod}.Match.MFG") == (int)MatchMFG.AdTec)
            {
                AddCustomModuleDevice(new AdTecMatch(mod, Venus_Core.VenusDevice.Match));
            }
            else if (SC.GetValue<bool>($"{mod}.Match.EnableMatch") &&
                SC.GetValue<int>($"{mod}.Match.CommunicationType") == (int)CommunicationType.RS232 &&
                SC.GetValue<int>($"{mod}.Match.MFG") == (int)MatchMFG.Revtech)
            {
                AddCustomModuleDevice(new RevtechMatch(mod, Venus_Core.VenusDevice.Match, MatchCommunicationType.RS232));
            }
            else if (SC.GetValue<bool>($"{mod}.Match.EnableMatch") &&
                SC.GetValue<int>($"{mod}.Match.CommunicationType") == (int)CommunicationType.Ethernet&&
                SC.GetValue<int>($"{mod}.Match.MFG") == (int)MatchMFG.Revtech)
            {
                AddCustomModuleDevice(new RevtechMatch(mod, Venus_Core.VenusDevice.Match, MatchCommunicationType.Ethernet));
            }

            if (SC.GetValue<bool>($"{mod}.BiasMatch.EnableBiasMatch") &&
                SC.GetValue<int>($"{mod}.BiasMatch.CommunicationType") == (int)CommunicationType.RS232 &&
                SC.GetValue<int>($"{mod}.BiasMatch.MFG") == (int)MatchMFG.AdTec)
            {
                AddCustomModuleDevice(new AdTecMatch(mod, Venus_Core.VenusDevice.BiasMatch));
            }
            else if (SC.GetValue<bool>($"{mod}.BiasMatch.EnableBiasMatch") &&
                SC.GetValue<int>($"{mod}.BiasMatch.CommunicationType") == (int)CommunicationType.RS232 &&
                SC.GetValue<int>($"{mod}.BiasMatch.MFG") == (int)MatchMFG.Revtech)
            {
                AddCustomModuleDevice(new RevtechMatch(mod, Venus_Core.VenusDevice.BiasMatch,MatchCommunicationType.RS232));
            }
            else if (SC.GetValue<bool>($"{mod}.BiasMatch.EnableBiasMatch") &&
                SC.GetValue<int>($"{mod}.BiasMatch.CommunicationType") == (int)CommunicationType.Ethernet &&
                SC.GetValue<int>($"{mod}.BiasMatch.MFG") == (int)MatchMFG.Revtech)
            {
                AddCustomModuleDevice(new RevtechMatch(mod, Venus_Core.VenusDevice.BiasMatch, MatchCommunicationType.Ethernet));
            }

            if (SC.GetValue<int>($"{mod}.DryPump.CommunicationType") == (int)CommunicationType.RS232)
            {
                if (SC.GetValue<int>($"{mod}.DryPump.MFG") == (int)DryPumpMFG.SKY)
                {
                    AddCustomModuleDevice(new SkyPump(mod));
                }
                else if (SC.GetValue<int>($"{mod}.DryPump.MFG") == (int)DryPumpMFG.Edwards)
                {
                    AddCustomModuleDevice(new EdwardsPump(mod));
                }
            }

            AddCustomModuleDevice(new ESC5HighVoltage(mod));
            AddCustomModuleDevice(new AdixenTurboPump(mod));
            AddCustomModuleDevice(new PendulumValve(mod));

            if (SC.GetValue<bool>($"{mod}.EPD.IsEnabled") == true)
            {
                if (SC.GetValue<int>($"{mod}.EPD.EPDType") == 0)
                {
                    AddCustomModuleDevice(new EPDClient(mod));
                }
                else
                {
                    AddCustomModuleDevice(new EPDDevice(mod));
                }
            }
           
            
            //AddCustomDevice(new JetPM(mod));

            switch (jetChamber)
            {
                case JetChamber.Venus:
                    AddCustomDevice(new JetVenusPM(mod));
                    break;
                case JetChamber.Kepler2300:
                    AddCustomDevice(new JetKepler2300PM(mod));
                    break;
                case JetChamber.Kepler2200A:
                    AddCustomDevice(new JetKepler2200APM(mod));
                    break;
                case JetChamber.Kepler2200B:
                    AddCustomDevice(new JetKepler2200BPM(mod));
                    break;               
                case JetChamber.VenusSE:
                    AddCustomDevice(new JetVenusSEPM(mod));
                    break;
                case JetChamber.VenusDE:
                    AddCustomDevice(new JetVenusDEPM(mod));
                    break;
            }

        }

        private void InitTM(ModuleName mod)
        {
            Initialize(device_model_file_MF, mod.ToString(), mod, mod.ToString());
            if (SC.GetValue<int>($"LLA.DryPump.CommunicationType") == (int)CommunicationType.RS232)
            {
                if (SC.GetValue<int>($"LLA.DryPump.MFG") == (int)DryPumpMFG.SKY)
                {
                    AddCustomModuleDevice(new SkyPump(ModuleName.LLA));
                }
                else if (SC.GetValue<int>($"LLA.DryPump.MFG") == (int)DryPumpMFG.Edwards)
                {
                    AddCustomModuleDevice(new EdwardsPump(ModuleName.LLA));
                }
            }

            if (SC.GetValue<int>($"{mod}.DryPump.CommunicationType") == (int)CommunicationType.RS232)
            {
                if (SC.GetValue<int>($"{mod}.DryPump.MFG") == (int)DryPumpMFG.SKY)
                {
                    AddCustomModuleDevice(new SkyPump(mod));
                }
                else if (SC.GetValue<int>($"{mod}.DryPump.MFG") == (int)DryPumpMFG.Edwards)
                {
                    AddCustomModuleDevice(new EdwardsPump(mod));
                }
            }
           
            //AddCustomModuleDevice(new SkyPump(mod));
            //AddCustomModuleDevice(new SkyPump(ModuleName.LLA));


            AddCustomDevice(new JetTM());
        }

        private void InitSETM(ModuleName mod)
        {
            Initialize(device_model_file_SEMF, mod.ToString(), mod, mod.ToString());
            if (SC.GetValue<int>($"{mod}.DryPump.CommunicationType") == (int)CommunicationType.RS232)
            {
                if (SC.GetValue<int>($"{mod}.DryPump.MFG") == (int)DryPumpMFG.SKY)
                {
                    AddCustomModuleDevice(new SkyPump(mod));
                }
                else if (SC.GetValue<int>($"{mod}.DryPump.MFG") == (int)DryPumpMFG.Edwards)
                {
                    AddCustomModuleDevice(new EdwardsPump(mod));
                }
            }
            AddCustomDevice(new HongHuTM());
        }

        private WaferSize MapWaferSize(int value)
        {
            switch (value)
            {
                case 3: return WaferSize.WS3;
                case 4: return WaferSize.WS4;
                case 6: return WaferSize.WS6;
                case 8: return WaferSize.WS8;
            }

            return WaferSize.WS0;
        }

        private bool Invoke(string arg1, object[] args)
        {
            string name = (string)args[0];
            string func = (string)args[1];

            object[] param = new object[args.Length - 2];

            for (int i = 2; i < args.Length; i++)
                param[i - 2] = args[i].ToString();

            DEVICE.Do(string.Format("{0}.{1}", name, func), 0, true, param);

            return true;
        }
        public void RTExitPMEvent()
        {
            if (ModuleHelper.IsInstalled(ModuleName.PMA))
            {
                DEVICE.GetDevice<JetPMBase>("PMA")?.RTCloseEvent();
            }
            if (ModuleHelper.IsInstalled(ModuleName.PMB))
            {
                DEVICE.GetDevice<JetPMBase>("PMB")?.RTCloseEvent();
            }
            if (ModuleHelper.IsInstalled(ModuleName.PMC))
            {
                DEVICE.GetDevice<JetPMBase>("PMC")?.RTCloseEvent();
            }
            if (ModuleHelper.IsInstalled(ModuleName.PMD))
            {
                DEVICE.GetDevice<JetPMBase>("PMD")?.RTCloseEvent();
            }
        }
    }
}