using Aitex.Core.RT.Log;
using Aitex.Core.Util;
using MECF.Framework.Common.Device.Bases;
using CyberX8_RT.Devices.EFEM;
using CyberX8_RT.Modules;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MECF.Framework.Common.IOCore;
using System.Runtime.InteropServices;

namespace CyberX8_RT.Devices.SignalLight
{
    public class SunWaySignalLight : SignalLightBase
    {
        #region 常量 
        private const string LIGHTER_TOWER = "LightTower";
        private const string BUZZER = "Buzzer";
        private const string RED_LIGHT = "RedLight";
        private const string YELLOW_LIGHT = "YellowLight";
        private const string GREEN_LIGHT = "GreenLight";
        private const string BLUE_LIGHT = "BlueLight";
        #endregion
        public MECF.Framework.Common.Device.Bases.LightType Type { get; set; }
        public EfemBase EfemController { get; set; }

        private F_TRIG _trigError = new F_TRIG();

        private bool _isInit;

        private TowerLightStatus _preState = TowerLightStatus.Unknown;
        //IoSignalTower _SignalTower;
        public SunWaySignalLight(string module, string name, EfemBase _efem) : base(module, name)
        {
            _isInit = true;
            //EfemController = _efem;
        }
        protected override void SetOn()
        {
            SetLight(TowerLightStatus.On);
        }

        protected override void SetOff()
        {
            SetLight(TowerLightStatus.Off);
        }

        protected override void SetBlinking(bool token)
        {
            SetLight(token?TowerLightStatus.On:TowerLightStatus.Off);
        }

        private bool SetSignalLight(MECF.Framework.Common.Device.Bases.LightType type, TowerLightStatus setpoint)
        {
            if(EfemController==null)
            {
                return false;
            }
            LightType lightType;
            switch (type)
            {
                case MECF.Framework.Common.Device.Bases.LightType.Red:
                    lightType = LightType.RED;
                    break;
                case MECF.Framework.Common.Device.Bases.LightType.Yellow:
                    lightType = LightType.YELLOW;
                    break;
                case MECF.Framework.Common.Device.Bases.LightType.Green:
                    lightType = LightType.GREEN;
                    break;
                case MECF.Framework.Common.Device.Bases.LightType.White:
                    lightType = LightType.WHITE;
                    break;
                case MECF.Framework.Common.Device.Bases.LightType.Blue:
                    lightType = LightType.BLUE;
                    break;
                case MECF.Framework.Common.Device.Bases.LightType.Buzzer:
                case MECF.Framework.Common.Device.Bases.LightType.Buzzer1:
                    lightType = LightType.BUZZER1;
                    break;
                default:
                    lightType = LightType.BUZZER1;
                    break;
            }

            LightStatus lightSetpoint;
            switch (setpoint)
            {
                case TowerLightStatus.Blinking:
                    lightSetpoint = LightStatus.BLINK;
                    //lightState = LightState.Blink;
                    break;
                case TowerLightStatus.On:
                    lightSetpoint = LightStatus.ON;
                    break;
                case TowerLightStatus.Off:
                    lightSetpoint = LightStatus.OFF;
                    break;
                default:
                    lightSetpoint = LightStatus.OFF;
                    break;
            }
            bool lightValue=lightSetpoint==LightStatus.ON?true:false;
            return  IOModuleManager.Instance.WriteIoValue(GetIOLight(lightType), lightValue);
        }

        private string GetIOLight(LightType lightType)
        {
            switch (lightType)
            {
                case LightType.RED:
                    return $"{LIGHTER_TOWER}.{RED_LIGHT}";
                case LightType.GREEN:
                    return $"{LIGHTER_TOWER}.{GREEN_LIGHT}";
                case LightType.BLUE:
                    return $"{LIGHTER_TOWER}.{BLUE_LIGHT}";
                case LightType.YELLOW:
                    return $"{LIGHTER_TOWER}.{YELLOW_LIGHT}";
                case LightType.BUZZER1:
                    return $"{LIGHTER_TOWER}.{BUZZER}";
                default:
                    return $"{LIGHTER_TOWER}.{BUZZER}";
            }
        }

        private void SetLight(TowerLightStatus setpoint)
        {
            try
            {
                if (setpoint != _preState || _isInit)
                {
                    _trigError.CLK = SetSignalLight(Type, setpoint);
                    //if (_trigError.Q)
                    //{
                    //    //EV.PostWarningLog(Module, $"Set {Type} signal light {setpoint} error, {reason}");
                    //}
                    if (_trigError.M)
                    {
                        _isInit = false;
                        _preState = setpoint;
                    }
                }

            }
            catch (Exception ex)
            {
                LOG.WriteExeption(ex);
            }
        }

        public void ResetData()
        {
            _isInit = true;
        }

        public override void Reset()
        {
            _trigError.RST = true;
            base.Reset();
        }
    }
}