using ServiceCenter.Extensions;
using ServiceCenter.Logs;
using ServiceCenter.Redis;
using WCS.Core;
using WCS.Entity.Protocol.Station;

namespace WCS.WorkEngineering.Extensions
{
    public class Station : Device<IStation520, IStation521, IStation523>
    {
        public Station(Device device, World world) : base(device, world)
        {
        }

        /// <summary>
        ///  入库站点是否被禁止
        /// </summary>
        /// <returns></returns>
        public void 入库站点是否被禁止()
        {
            var config = RedisHub.Default.Check("ForbidTubuEnter") ?? throw new Exception("请在Redis中配置入库口禁用");
            var configs = config.Split(",");
            if (configs.Contains(Entity.Code)) throw new KnownException("当前入库口已被禁用，请联系运维人员了解具体情况", LogLevelEnum.High);
        }

        /// <summary>
        ///  入库站点是否满足执行条件
        /// </summary>
        /// <returns></returns>
        public void 入库站点是否满足执行条件()
        {
            if (Data.VoucherNo != Data2.VoucherNo) throw new KnownException($"凭证号不一致,DB520:{Data.VoucherNo}-DB521:{Data2.VoucherNo}", LogLevelEnum.High);
            if (Data3.Status.HasFlag(StationStatus.Run)) throw new KnownException("设备运行中", LogLevelEnum.Low);
            if (Data3.Status.HasFlag(StationStatus.PH_Status) && Data2.Request == 0) throw new KnownException("有光电无请求", LogLevelEnum.Mid);
            if (!Data3.Status.HasFlag(StationStatus.PH_Status) && Data2.Request == 1) throw new KnownException("无光电有请求", LogLevelEnum.Mid);
            if (!Data3.Status.HasFlag(StationStatus.OT_Status)) throw new KnownException("站台货物信息与实际占用不一致", LogLevelEnum.Low);
        }

        /// <summary>
        ///  主线分流点是否满足执行条件
        /// </summary>
        /// <returns></returns>
        public void 主线分流点是否满足执行条件()
        {
            if (Data.VoucherNo != Data2.VoucherNo) throw new KnownException($"凭证号不一致,DB520:{Data.VoucherNo}-DB521:{Data2.VoucherNo}", LogLevelEnum.High);
            if (Data3.Status.HasFlag(StationStatus.Run)) throw new KnownException("设备运行中", LogLevelEnum.Low);
            if (Data3.Status.HasFlag(StationStatus.PH_Status) && Data2.Request == 0) throw new KnownException("有光电无请求", LogLevelEnum.Mid);
            if (!Data3.Status.HasFlag(StationStatus.PH_Status) && Data2.Request == 1) throw new KnownException("无光电有请求", LogLevelEnum.Mid);
            if (!Data3.Status.HasFlag(StationStatus.OT_Status)) throw new KnownException("站台货物信息与实际占用不一致", LogLevelEnum.Low);
        }

        public string GetBarCode(World world)
        {
            var code = "BCR" + Entity.Code;
            var bcrdev = Device.All.FirstOrDefault(v => v.Code == code) ?? throw new KnownException($"未找到扫码器{code}", LogLevelEnum.High);
            var bcr = new BCR(bcrdev, world);
            return bcr.GetBCRCode();
        }
    }
}
