using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace ChuPiao.Common.Models
{
    /// <summary>
    /// 彩票机实体类
    /// </summary>
    public class M_MachineEntity:INotifyPropertyChanged,ICloneable,IComparable
    {
		#region Model

        private bool isAutoEncash = false;
        /// <summary>
        /// 是否自动兑奖
        /// </summary>
        public bool M_IsAutoEncash
        {
            set { isAutoEncash = value; }
            get { return isAutoEncash; }
        }

        private bool isAutoClearTotal = false;
        /// <summary>
        /// 是否自动清除合计(2分钟没出票清一次,否则随机1-5票清一次)
        /// </summary>
        public bool M_IsAutoClearTotal
        {
            set { isAutoClearTotal = value; }
            get { return isAutoClearTotal; }
        }


        private bool _m_isSimulate = false;
        /// <summary>
        /// 是否模拟出票，测试用
        /// </summary>
        public bool M_IsSimulate
        {
            set { _m_isSimulate = value; }
            get { return _m_isSimulate; }
        }

        private bool _m_hasJc = true;
        /// <summary>
        /// 是否有竞彩权限，有：缴款09时段10，无缴款08时段09
        /// </summary>
        public bool M_HasJc
        {
            set { _m_hasJc = value; }
            get { return _m_hasJc; }
        }


		private int _m_type=0;
		private string _m_number;
        private string _m_zxnumber = string.Empty;
		private string _m_zxpassword=string.Empty;

		private bool _m_enabled=true;
		private bool _m_isuse=false;
		private bool _m_allowpaperout=false;
		private int _m_status;
        private int _m_loginstatus;
		private int _m_errornum;
        private int _m_ticketnum;
		private string _m_lasterrortitle=string.Empty;
		private DateTime _m_lasterrortime=new DateTime(1900,1,1);
        private DateTime _m_lastprinttime;
        private int _m_lastprintcounter = 0;
		private decimal _m_money;
		private DateTime _m_moneyupdatetime= new DateTime(1900,1,1);
		private int _m_keyboardtype=1002;
        private int _m_inputdevice = 100;
		private string _m_virtualkeyboardport="com1";
		private int _m_virtualkeyboardportbps=9600;
		private string _m_virtualscannerport="com2";
		private int _m_virtualscannerportbps=115200;
		private string _m_virtualprinterport="com3";
		private int _m_virtualprinterportbps=115200;
        private string _m_playstr = string.Empty;
        private string _m_remark = "";


        private string _salt;
        /// <summary>
        /// 通讯MD5加密时的参数
        /// </summary>
        public string M_Salt
        {
            get { return _salt; }
            set { _salt = value; }
        }

        private int _m_monthsaled;
        /// <summary>
        /// 月销量（近似值，错票、测试票没有体现）后续可以通过打月报表修正
        /// </summary>
        public int M_MonthSaled
        {
            get { return _m_monthsaled; }
            set { _m_monthsaled = value; }
        }

		/// <summary>
		/// 机器类型（1001--江西老机器；1002--江西新机器；1003--江西多乐彩机器；1004--江西竟彩机器；2001--天津北单机器；2002--天津竟彩机器；3001--深圳北单机器；4001--重庆福彩机器）
		/// </summary>
		public int M_Type
		{
			set{ _m_type=value;}
			get{return _m_type;}
		}
		/// <summary>
		/// 机器号
		/// </summary>
		public string M_Number
		{
            set
            {
                _m_number = value;

                if (null != PropertyChanged)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("M_Number"));
                }
            }
            get
            {

                return _m_number;

            }
		}
		/// <summary>
		/// 中心机器号
		/// </summary>
		public string M_ZxNumber
		{
			set{ _m_zxnumber=value;}
			get{return _m_zxnumber;}
		}
		/// <summary>
		/// 中心登录密码
		/// </summary>
		public string M_ZxPassword
		{
			set{ _m_zxpassword=value;}
			get{return _m_zxpassword;}
		}
		
		/// <summary>
		/// 是否有效（0--无效，机器故障等原因无法出票；1--有效，默认为1）
		/// </summary>
		public bool M_Enabled
		{
			set{ _m_enabled=value;}
			get{return _m_enabled;}
		}
		/// <summary>
		/// 是否启用（0--不启用，不会分配任务给该机器；1--启用，默认为1）
		/// </summary>
		public bool M_IsUse
		{
			set{ _m_isuse=value;}
			get{return _m_isuse;}
		}
		/// <summary>
		/// 允许出纸
		/// </summary>
		public bool M_AllowPaperout
		{
			set{ _m_allowpaperout=value;}
			get{return _m_allowpaperout;}
		}
		/// <summary>
		/// 状态：0--默认、停止，-50--停止中，--100--机器报错，100--正在出票，200--正在兑奖 
		/// </summary>
		public int M_Status
		{
			set{ _m_status=value;}
			get{return _m_status;}
		}
        /// <summary>
        /// 登录状态：0--默认，100--已登录
        /// </summary>
        public int M_LoginStatus
        {
            get { return _m_loginstatus; }
            set { _m_loginstatus = value; }
        }
        /// <summary>
        /// 等待出票的票数
        /// </summary>
        public int M_TicketNum
        {
            set { _m_ticketnum = value; }
            get { return _m_ticketnum; }
            
        }
		/// <summary>
		/// 错误次数
		/// </summary>
		public int M_ErrorNum
		{
			set{ _m_errornum=value;}
			get{return _m_errornum;}
		}
		/// <summary>
		/// 最后一次错误标题
		/// </summary>
		public string M_LastErrorTitle
		{
			set{ _m_lasterrortitle=value;}
			get{return _m_lasterrortitle;}
		}
		/// <summary>
		/// 最后一次错误时间
		/// </summary>
		public DateTime M_LastErrorTime
		{
			set{ _m_lasterrortime=value;}
			get{return _m_lasterrortime;}
		}

        /// <summary>
        /// 最后一张票的打印时间，这个字段是为分配票加的，保证任务数相同的彩票机先出票的优先分配
        /// </summary>
        public DateTime M_LastPrintTime
        {
            set { _m_lastprinttime = value; }
            get { return _m_lastprinttime; }
        }

        /// <summary>
        /// 最后n条打印记录计数器
        /// </summary>
        public int M_LastPrintCounter
        {
            get { return _m_lastprintcounter; }
            set { _m_lastprintcounter = value; }
        }
		/// <summary>
		/// 机器额度
		/// </summary>
		public decimal M_Money
		{
			set{ _m_money=value;}
			get{return _m_money;}
		}
		/// <summary>
		/// 机器额度更新时间
		/// </summary>
		public DateTime M_MoneyUpdateTime
		{
			set{ _m_moneyupdatetime=value;}
			get{return _m_moneyupdatetime;}
		}
		/// <summary>
		/// 键盘线类别（1001--老线；1002--新线）
		/// </summary>
		public int M_KeyboardType
		{
			set{ _m_keyboardtype=value;}
			get{return _m_keyboardtype;}
        }
        /// <summary>
        /// 输入设备类型（100--键盘；200--扫描仪）
        /// </summary>
        public int M_InputDeviceType
        {
            set { _m_inputdevice = value; }
            get { return _m_inputdevice; }
        }
		/// <summary>
		/// 模拟键盘（键盘线）端口号
		/// </summary>
		public string M_VirtualKeyboardPort
		{
			set{ _m_virtualkeyboardport=value;}
			get{return _m_virtualkeyboardport;}
		}
		/// <summary>
		/// 模拟键盘（键盘线）端口波特率
		/// </summary>
		public int M_VirtualKeyboardPortBps
		{
			set{ _m_virtualkeyboardportbps=value;}
			get{return _m_virtualkeyboardportbps;}
		}
		/// <summary>
		/// 模拟扫描仪（读票机）端口号
		/// </summary>
		public string M_VirtualScannerPort
		{
			set{ _m_virtualscannerport=value;}
			get{return _m_virtualscannerport;}
		}
		/// <summary>
		/// 模拟扫描仪（读票机）端口波特率
		/// </summary>
		public int M_VirtualScannerPortBps
		{
			set{ _m_virtualscannerportbps=value;}
			get{return _m_virtualscannerportbps;}
		}
		/// <summary>
		/// 模拟打印机（打印模块）端口号
		/// </summary>
		public string M_VirtualPrinterPort
		{
			set{ _m_virtualprinterport=value;}
			get{return _m_virtualprinterport;}
		}
		/// <summary>
		/// 模拟打印机（打印模块）端口波特率
		/// </summary>
		public int M_VirtualPrinterPortBps
		{
			set{ _m_virtualprinterportbps=value;}
			get{return _m_virtualprinterportbps;}
		}
        /// <summary>
        /// 默认"911,701,"
        /// </summary>
        public string M_LotStr
        {
            set { _m_playstr = value; }
            get { return _m_playstr; }
        }
		/// <summary>
		/// 备注
		/// </summary>
		public string M_Remark
		{
			set{ _m_remark=value;}
			get{return _m_remark;}
        }

        
		#endregion Model
        #region 扩展属性

        private bool _allowSnapJcMatch = false;
        /// <summary>
        /// 是否允许抓取竟彩对阵
        /// </summary>
        public bool AllowSnapJcMatch
        {
            get { return _allowSnapJcMatch; }
            set { _allowSnapJcMatch = value; }
        }

        private bool _allowSnapZcPs = false;
        /// <summary>
        /// 是否允许抓取足彩对阵、排三限号
        /// </summary>
        public bool AllowSnapZcPs
        {
            get { return _allowSnapZcPs; }
            set { _allowSnapZcPs = value; }
        }

        private bool _allowSnapOpenCode_force = false;
        /// <summary>
        /// 是否允许抓取开奖号码（多乐彩）只要空闲就抓
        /// </summary>
        public bool AllowSnapOpenCode_force
        {
            get { return _allowSnapOpenCode_force; }
            set { _allowSnapOpenCode_force = value; }
        }

        private bool _allowSnapOpenCode = false;
        /// <summary>
        /// 是否允许抓取开奖号码（多乐彩）
        /// </summary>
        public bool AllowSnapOpenCode
        {
            get { return _allowSnapOpenCode; }
            set { _allowSnapOpenCode = value; }
        }

        private M_MachineConfigEntity _machineConfigEntity;
        /// <summary>
        /// 机器配置
        /// </summary>
        public M_MachineConfigEntity MachineConfigEntity
        {
            get { return _machineConfigEntity; }
            set { _machineConfigEntity = value; }
        }

        private SortedDictionary<int, M_MachineModeConfigEntity> _machineModeConfigs;
        /// <summary>
        /// 机器各种投注模式配置
        /// </summary>
        public SortedDictionary<int, M_MachineModeConfigEntity> MachineModeConfigs
        {
            get { return _machineModeConfigs; }
            set { _machineModeConfigs = value; }
        }

        private SortedDictionary<int, SortedDictionary<int, M_MachinePlayEntity>> _machinePlayConfigs = new SortedDictionary<int, SortedDictionary<int, M_MachinePlayEntity>>();

        /// <summary>
        /// 彩票机玩法配置
        /// </summary>
        public SortedDictionary<int, SortedDictionary<int, M_MachinePlayEntity>> MachinePlayConfigs
        {
            get { return _machinePlayConfigs; }
            set { _machinePlayConfigs = value; }
        }

        #endregion

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region ICloneable 成员

        public object Clone()
        {
            return this.MemberwiseClone();
        }

        #endregion

        public override bool Equals(object obj)
        {
            if (null == obj || ! (obj is M_MachineEntity))
                return false;
            if (this == obj)
                return true;
            M_MachineEntity machine = obj as M_MachineEntity;
            if(machine._m_number == this._m_number)
                return true;
            return false;
        }

        #region IComparable 成员

        /// <summary>
        /// 机器号按任务由少到多排序，如果任务数相同，则按最后出票时间由早到晚排序
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public int CompareTo(object obj)
        {
            M_MachineEntity mac = (M_MachineEntity)obj;

            if (M_TicketNum != mac.M_TicketNum)
            {
                return M_TicketNum.CompareTo(mac.M_TicketNum);
            }
            else
            {
                return M_LastPrintTime.CompareTo(mac.M_LastPrintTime);
            }
        }

        #endregion

        public override string ToString()
        {
            return _m_number;
        }
    }
}
