using PlcSiemens.Core.Extension;
using WCS.Core;
using WCS.Entity.Protocol.BCR;
using WCS.WorkEngineering.Worlds.Logs;

namespace WCS.WorkEngineering.Extensions
{
    /// <summary>
    ///  BCR扩展
    /// </summary>
    public class BCR : Device<IBCR81>
    {
        /// <summary>
        ///  BCR扩展
        /// </summary>
        /// <param name="device"></param>
        /// <param name="world"></param>
        public BCR(Device device, World world) : base(device, world)
        {
        }

        /// <summary>
        ///  获取BCR码
        /// </summary>
        /// <returns></returns>
        public string GetBCRCode()
        {
            var barcode = Data.Content.Trim('\r');

            if (barcode.IsNullOrWhiteSpace()) throw new KnownException($"{Entity.Code}--扫码失败，内容为空", LogLevelEnum.High);

            return barcode;
        }
    }

    /// <summary>
    ///  BCR扩展
    /// </summary>
    public static class BCRExtension
    {
        /// <summary>
        ///  获取BCR码
        /// </summary>
        /// <param name="bs"></param>
        /// <param name="code">BCR对应站点设备号</param>
        /// <returns></returns>
        public static string GetBCRCode(this List<BCR> bs, string code)
        {
            code = "BCR" + code;
            var bcr = bs.FirstOrDefault(p => p.Entity.Code == code) ?? throw new KnownException($"未找到扫码器{code}", LogLevelEnum.High);
            return bcr.GetBCRCode();
        }
    }
}
