using PlcSiemens.Core.Extension;
using ServiceCenter.Logs;
using WCS.Core;
using WCS.Entity.Protocol.BCR;

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.RemoveEscapeCharacters();

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

            return barcode;
        }
    }

    ///// <summary>
    /////  BCR83
    ///// </summary>
    //public class BCR83 : Device<IBCR83>
    //{
    //    /// <summary>
    //    ///   BCR扩展
    //    /// </summary>
    //    /// <param name="device"></param>
    //    /// <param name="world"></param>
    //    public BCR83(Device device, World world) : base(device, world)
    //    {
    //    }

    //    /// <summary>
    //    ///  获取BCR码
    //    /// </summary>
    //    /// <returns></returns>
    //    public string GetBCRCode()
    //    {
    //        var barcode = Data.BcrCode.RemoveEscapeCharacters();

    //        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> bc, string code)
        {
            code = "BCR" + code;
            var bcr = bc.FirstOrDefault(p => p.Entity.Code == code) ?? throw new KnownException($"未找到扫码器{code}", LogLevelEnum.High);
            return bcr.GetBCRCode();
        }

        ///// <summary>
        /////  获取BCR83码集合
        ///// </summary>
        ///// <param name="bc"></param>
        ///// <returns></returns>
        //public static string GetBCRCodeList(this BCR83 bc)
        //{
        //}
    }
}
