using System;
using ChuPiao.Common.Utils;
namespace ChuPiao.AutoPrint.Machine.Models
{
    /// <summary>
    /// 键码信息
    /// </summary>
    public class KeycodeInfo
    {
        private static Random random = new Random();

        #region 构造函数
        public KeycodeInfo()
        {

        }
        public KeycodeInfo(string name, int sendInterval)
        {
            
            _valueHex = null;
            _valueBytes = null;
            if (name.StartsWith("wait"))
            {
                if(name== "wait")
                {
                    _name = name + sendInterval;
                    _sendInterval = sendInterval;
                }
                else
                {
                    _name = name;
                    _sendInterval = int.Parse(name.Replace("wait", ""));
                }
            }
            else
            {
                _name = name;
                _sendInterval = sendInterval;
            }

            //间隔加个随机值
            //_sendInterval += random.Next(70, 270);
            //_sendInterval += random.Next(150, 300);

        }

        /// <summary>
        /// 没有随机间隔的，这种情况适合没有结束标志，需要计算按键时间来休眠
        /// </summary>
        /// <param name="name"></param>
        /// <param name="sendInterval"></param>
        /// <param name="noRandom"></param>
        public KeycodeInfo(string name, int sendInterval,bool noRandom)
        {
            _valueHex = null;
            _valueBytes = null;
            if (name.StartsWith("wait"))
            {
                if (name == "wait")
                {
                    _name = name + sendInterval;
                    _sendInterval = sendInterval;
                }
                else
                {
                    _name = name;
                    _sendInterval = int.Parse(name.Replace("wait", ""));
                }
            }
            else
            {
                _name = name;
                _sendInterval = sendInterval;
            }

            //间隔加个随机值
            Random random = new Random();
            int min = LocalConfigUtil.KeyInterval - 50;
            int max = LocalConfigUtil.KeyInterval + 50;
            _sendInterval += random.Next(min, max);
        }
        //public KeycodeInfo(string name, string valueHex, byte[] valueBytes, int sendInterval)
        //{
        //    _name = name;
        //    _valueHex = valueHex;
        //    _valueBytes = valueBytes;
        //    _sendInterval = sendInterval;
        //}
        #endregion
        #region 属性

        private string _name;
        private string _valueHex;
        private byte[] _valueBytes;
        private int _sendInterval;
        /// <summary>
        /// 键码名称
        /// </summary>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        /// <summary>
        /// 键码值的十六进制表示
        /// </summary>
        public string ValueHex
        {
            get { return _valueHex; }
            set { _valueHex = value; }
        }
        /// <summary>
        /// 键码值的字节表示
        /// </summary>
        public byte[] ValueBytes
        {
            get { return _valueBytes; }
            set { _valueBytes = value; }
        }
        /// <summary>
        /// 键码发送间隔时间（毫秒）
        /// </summary>
        public int SendInterval
        {
            get { return _sendInterval; }
            set { _sendInterval = value; }
        }

        #endregion

        #region 方法
        #endregion

        #region override
        public override string ToString()
        {
            return Name;
        }
        #endregion
    }
}
