using System.Diagnostics;
using Venus_RT.Devices;
using System;
using Venus_Core;
using Aitex.Core.RT.Routine;

namespace Venus_RT.Modules.PMs
{
    class PMRoutineBase : ModuleRoutineBase
    {
        protected readonly JetPMBase _chamber;

        public PMRoutineBase(JetPMBase chamber) : base(chamber.Module)
        {
            _chamber = chamber;
        }

        public bool CheckLid()
        {
            if (!_chamber.IsLidClosed)
            {
                this.Stop("Chamber 盖子必须关");
                return false;
            }
            return true;
        }

        public bool CheckLidLoadLock()
        {
            if (_chamber.ChamberType == JetChamber.VenusSE || _chamber.ChamberType == JetChamber.VenusDE)
                return true;

            if (!_chamber.IsLidLoadlockClosed)
            {
                this.Stop("LoadLock 盖子必须关");
                return false;
            }
            return true;
        }

        protected bool CheckSlitDoor()
        {
            if (!_chamber.IsSlitDoorClosed)
            {
                Stop("传送门必须关");
                return false;
            }
            if (_chamber.ChamberType== JetChamber.VenusDE)
            {
                if (!_chamber.IsLinerDoorClosed)
                {
                    Stop("Liner door 必须关");
                    return false;
                }
            }
            return true;

        }

        protected bool CheckDryPump()
        {
            if (!_chamber.IsPumpRunning)
            {
                Stop("干泵没有启动");
                return false;
            }
            if (_chamber.HasPumpError)
            {
                Stop("干泵状态有错误");
                return false;
            }

            return true;
        }

        protected bool CheckTurboPump()
        {
            if (!_chamber.IsTurboPumpRunning)
            {
                Stop("分子泵没有启动");
                return false;
            }

            if (_chamber.HasTurboPumpError)
            {
                Stop("分子泵状态有错误");
                return false;
            }

            return true;
        }

        protected bool CheckCDA()
        {
            if (!_chamber.IsCDA_OK)
            {
                Stop("CDA 压力信号不正确");
                return false;
            }
            return true;
        }

        public bool CheckATM()
        {
            if (_chamber.IsATM)
            {
                this.Stop("Chamber 是ATM状态");
                return false;
            }
            return true;
        }

        protected bool CloseAllValves(int? delayTime = null)
        {
            Notify("关闭所有的阀门");
            _chamber.CloseValves(delayTime);
            return true;
        }

        protected bool TurnValve(ValveType vlv, bool bOpen)
        {
            _chamber.OpenValve(vlv, bOpen);
            Notify($"{(bOpen ? "打开" : "关闭")} {vlv} 阀");
            return true;
        }

        protected bool CheckValve(ValveType vlv, bool bOpen)
        {
            if (vlv == ValveType.FastPump)
                return _chamber.IsFastPumpOpened == bOpen;
            else if (vlv == ValveType.SoftPump)
                return _chamber.IsSoftPumpOpened == bOpen;

            return true;
        }

        protected bool SetCoolantTemp(double target, double offset)
        {
            if (!_chamber.CheckChillerStatus())
            {
                Stop("Chiller status error");
                return false;
            }

            if (_chamber.CoolantOutletTempFB > target)
            {
                Notify($"水冷当前温度{_chamber.CoolantOutletTempFB} ℃, 大于目标 {target.ToString()} ℃");
            }

            _chamber.HeatChiller(ChillerType.Chiller,target, offset);
            Notify($"检查水冷温度值,当前温度{_chamber.CoolantOutletTempFB} ℃, 目标 {target.ToString()} ℃");

            return true;
        }

        protected bool CheckCoolantTemp(double target, double tolerance)
        {
            return Math.Abs(_chamber.CoolantOutletTempFB - target) <= tolerance;
        }
       
    }
}