using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ChuPiao.AutoPrint.Common;
using ChuPiao.AutoPrint.Desktop.Dialogs;
using ChuPiao.AutoPrint.Desktop.Properties;
using ChuPiao.AutoPrint.Desktop.StatusBar;
using ChuPiao.AutoPrint.Machine;
using ChuPiao.AutoPrint.Machine.Models;
using ChuPiao.AutoPrint.Services;
using ChuPiao.AutoPrint.Services.AutoPrint;
using ChuPiao.Common.Enums;
using ChuPiao.Common.Models;
using ChuPiao.Common.Utils;
using System.Threading;
using ChuPiao.Common.Communication.GtdJson.Packs;
using ChuPiao.Common.Communication.GtdJson;

namespace ChuPiao.AutoPrint.Desktop.AutoPrint
{
    public partial class AutoPrintView : BaseUserControl
    {
        private string _localConfig_SnapJcMatchMacKey = "SnapJcMatchMac";
        private static string _localConfig_SnapJcMatchMacValue = "";
        private string _localConfig_SnapZcPsKey = "SnapZcPs";
        private static string _localConfig_SnapZcPsValue = "";
        private string _localConfig_SnapOpenCodeKey = "SnapOpenCode";
        private static string _localConfig_SnapOpenCodeValue = "";
        private AutoPrintService _service;
        private string _errorMessage;
        private L_LotTicketEntity _ticket;

        private System.Windows.Forms.Timer refreshingTimer;
        private int updateInterval = 300000;

        private Thread _workThread;

        public AutoPrintView()
        {
            InitializeComponent();
            this.chkMacNumber.Checked = true;
            this.llblErrorView.Text = "";
            this.lblTotalMoney.Text = "0";
            this.llblViewLastTicket.Text = "0";
            //this.lblMoney.Text = "0";
            this.llblTicketSn.Text = "无";
            this.llblViewQueue.Text = "0";
            this.lblTicketCode.Text = "无";
            _ticket = new L_LotTicketEntity();

            //refreshingTimer = new Timer();
            //refreshingTimer.Interval = updateInterval;
            //refreshingTimer.Tick += delegate
            //{
            //    //refreshingTimer.Enabled = false;
            //    //_service.LotMachine.Log.Debug("更新可出票玩法");
            //    string playtype = GetMachinePlayType();
            //    this.toolTip1.SetToolTip(this.llblMachineConfig, playtype);
            //};
            //refreshingTimer.Enabled = true;

            //报错时的线程
            _workThread = new Thread(new ThreadStart(DoTask));
            _workThread.Start();
        }

        /// <summary>
        /// 终止接收指令的线程
        /// </summary>
        public void StopWorkThread()
        {
            if (_workThread != null)
            {
                _workThread.Abort();
            }
        }

        private void DoTask()
        {
            while (true)
            {
                if (_service.LotMachine.MachineInfo.M_Status == (int)MachineStatusEnum.Error)
                {
                    try
                    {
                        //获取指令
                        A10009GetCommand pack = new A10009GetCommand();
                        pack.AgentId = _service.LotMachine.MachineInfo.M_Number;
                        pack.Salt = _service.LotMachine.MachineInfo.M_Salt;
                        A10009GetCommandRet packRet = PackManager.Instance.Send(pack) as A10009GetCommandRet;
                        if (packRet.Code > 0)
                        {
                            Logger.Log.Info("报错状态获取指令:" + packRet.Command);
                            if (packRet.Command == "enter")    //竞篮条件已改变，报错处理，清弹窗，彩票机恢复出票状态，取消票
                            {
                                //让分或大小分已改变
                                //if (_ticket.LotId == (int)Lottype.Jl)
                                if (_ticket.L_PlayId == (int)Playtype.JlRFSF || _ticket.L_PlayId == (int)Playtype.JlDXF ||
                                       (
                                           _ticket.L_PlayId == (int)Playtype.JlHHGG &&
                                          (
                                              _ticket.L_Code.Contains(Convert.ToString((int)Playtype.JlRFSF)) || _ticket.L_Code.Contains(Convert.ToString((int)Playtype.JlDXF))
                                          )
                                       )
                                   )                     
                                {
                                    try
                                    {
                                        //取消票
                                        A10002NotifyTicket pack1 = new A10002NotifyTicket();
                                        pack1.AgentId = _service.LotMachine.MachineInfo.M_Number;
                                        pack1.Salt = _service.LotMachine.MachineInfo.M_Salt;
                                        _ticket.L_Status = 100;
                                        _ticket.L_LimitType = 0;
                                        _ticket.L_SucTime = DateTime.Now;
                                        pack1.Ticket = _ticket;
                                        A10002NotifyTicketRet packRet1 = PackManager.Instance.Send(pack1) as A10002NotifyTicketRet;
                                        if (packRet1.Code <= 0)
                                        {
                                            throw new Exception("篮球报错【条件已改变】，取消票失败，" + packRet1.Code.ToString() + " " + packRet1.Message);
                                        }
                                        else
                                        {
                                            Logger.Log.Info("篮球报错【条件已改变】，取消票成功！");
                                        }


                                        _service.ClearError((int)MachineErrorType.JlConditionChange);
                                        Logger.Log.Info("篮球报错【条件已改变】，彩票机恢复出票");

                                        _service.UpdateMachineStatus(MachineStatusEnum.Printing);
                                        _service.Wait4Job.Set();
                                        WorkbenchSingleton.SafeThreadAsyncCall(new Action(ErrorCleared));
                                        Logger.Log.Info("篮球报错【条件已改变】，软件恢复出票");


                                    }
                                    catch (Exception ex)
                                    {
                                        Logger.Log.Error("篮球报错【条件已改变】，处理失败：" + ex.ToString());
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Log.Error("报错状态获取指令失败！" + ex.ToString());
                    }
                }

                Thread.Sleep(5 * 1000);
            }
        }


        public void Checked()
        {
            this.chkMacNumber.Checked = true;
        }
        public bool IsSelected
        {
            get { return this.chkMacNumber.Checked; }
        }

        public AutoPrintService Service
        {
            get { return _service; }
            set
            {
                _service = value;
                ShowLotMachineInfo();
                _service.Printing += new EventHandler<TicketEventArgs>(_service_Printing);
                _service.Printed += new EventHandler<TicketEventArgs>(_service_Printed);
                _service.StatusChanged += new EventHandler<MachineStatusArgs>(LotMachine_StatusChanged);
                _service.PrintTicketError += new EventHandler<ErrorTicketEventArgs>(_service_PrintTicketError);

                _service.AmountCleared += new EventHandler(_service_AmountCleared);
                _service.ErrorCleared += new EventHandler(_service_ErrorCleared);
            }
        }

        void _service_ErrorCleared(object sender, EventArgs e)
        {
            try
            {
                WorkbenchSingleton.SafeThreadAsyncCall(new Action(ErrorCleared));

            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("执行彩票机错误清除后事件异常。错误信息：", exception.Message));
            }
        }

        private void StopWarn()
        {
            bool hasError = false;
            foreach (KeyValuePair<string, ILotMachine> keyValuePair in LotMachineService.LotMachineList)
            {
                if (keyValuePair.Value.MachineInfo.M_Status == (int)MachineStatusEnum.Error)
                {
                    hasError = true;
                    break;
                }
            }
            if (!hasError)
            {
                StatusBarService.StopWarnButton();
            }
        }

        private void ErrorCleared()
        {
            this.llblErrorView.Text = "";
            StopWarn();
        }

        void _service_AmountCleared(object sender, EventArgs e)
        {
            try
            {
                //WorkbenchSingleton.SafeThreadCall(new Action<L_LotTicketEntity>(ShowTicketPrinting), e.TicketEntity);
                WorkbenchSingleton.SafeThreadAsyncCall(new Action(ShowAmount));
            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("更新出票合计界面异常。错误信息：", exception.Message));
            }
        }

        public void ShowAmount()
        {
            this.lblTotalMoney.Text = _service.TotalMoney.ToString("0");
            this.llblViewLastTicket.Text = _service.TotalTicket.ToString();
        }

        private DecodePrintData _decodePrintData = null;

        void _service_PrintTicketError(object sender, ErrorTicketEventArgs e)
        {
            try
            {
                //_service.LotMachine.Log.Debug(string.Concat("生产流水号：", e.Ticket.L_TicketSn, " 开始更新错误信息界面"));
                _ticket = e.Ticket;
                _decodePrintData = e.DecodePrintData;
                _errorMessage = e.Message;
                //WorkbenchSingleton.SafeThreadCall(new Action<L_LotTicketEntity, string>(ShowError), e.TicketEntity, e.Message);
                WorkbenchSingleton.SafeThreadAsyncCall(new Action<L_LotTicketEntity, string>(ShowError), e.Ticket, e.Message);
            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("出票错误更新界面异常。错误信息：", exception.Message));
            }
        }

        void LotMachine_StatusChanged(object sender, MachineStatusArgs e)
        {
            try
            {
                //_service.LotMachine.Log.Debug(string.Concat("更新机器状态：", e.MachineStatus, "界面开始"));
                //WorkbenchSingleton.SafeThreadCall(new Action<MachineStatusEnum>(ShowMachineStatus), e.MachineStatus);
                WorkbenchSingleton.SafeThreadAsyncCall(new Action<MachineStatusEnum>(ShowMachineStatus), e.MachineStatus);
            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("机器状态变化更新界面异常。错误信息：", exception.Message));
            }
        }

        void _service_Printed(object sender, TicketEventArgs e)
        {
            //WorkbenchSingleton.SafeThreadCall(new Action<L_LotTicketEntity>(ShowTicketPrinted), e.TicketEntity);
            try
            {
                //_service.LotMachine.Log.Debug(string.Concat("生产流水号：", e.TicketEntity.L_TicketSn, " 开始更新出票完成界面"));
                WorkbenchSingleton.SafeThreadAsyncCall(new Action<L_LotTicketEntity>(ShowTicketPrinted), e.TicketEntity);
            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("更新出票完成界面异常。错误信息：", exception.Message));
            }

        }

        void _service_Printing(object sender, TicketEventArgs e)
        {
            try
            {
                //WorkbenchSingleton.SafeThreadCall(new Action<L_LotTicketEntity>(ShowTicketPrinting), e.TicketEntity);
                WorkbenchSingleton.SafeThreadAsyncCall(new Action<L_LotTicketEntity>(ShowTicketPrinting), e.TicketEntity);
            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("更新出票中界面异常。错误信息：", exception.Message));
            }

        }

        private void ShowError(L_LotTicketEntity ticketEntity, string message)
        {
            try
            {
                //new RedisClient().Publish(_service.LotMachine.MachineInfo.M_Number + " 报错了：" + message);

                this.llblErrorView.Text = "彩票机报错啦";
                this.toolTip1.SetToolTip(this.llblErrorView, message);
                this.lblStatus.Image = Resources.Warning;

                //this.llblInputTicketCode.Visible = true;
            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("更新错误信息界面异常。错误信息：", exception.Message));
            }

        }
        void ShowMachineStatus(MachineStatusEnum machineStatus)
        {
            try
            {
                switch (machineStatus)
                {
                    case MachineStatusEnum.Printing:
                        this.lblStatus.Image = Resources.Run;
                        this.gbxMachine.BackColor = Color.Empty;

                        this.chkMacNumber.Checked = false;
                        break;
                    case MachineStatusEnum.Stopped:
                        this.lblStatus.Image = Resources.Stop;
                        this.gbxMachine.BackColor = Color.Empty;
                        this.chkMacNumber.Checked = true;
                        break;
                    case MachineStatusEnum.Stopping:
                        this.lblStatus.Image = Resources.Pause;
                        this.gbxMachine.BackColor = Color.Empty;
                        break;
                    case MachineStatusEnum.Error:
                        this.lblStatus.Image = Resources.Warning;
                        //this.gbxMachine.BackColor = Color.Orange;
                        //this.gbxMachine.BackColor = Color.RosyBrown;//001
                        //this.gbxMachine.BackColor = Color.DarkKhaki;//002
                        //this.gbxMachine.BackColor = Color.DarkSeaGreen;//003
                        this.gbxMachine.BackColor = Color.PaleGoldenrod;//004
                        this.chkMacNumber.Checked = true;
                        break;
                    case MachineStatusEnum.Encashing:
                        this.lblStatus.Image = Resources.Run;
                        this.gbxMachine.BackColor = Color.Empty;
                        this.chkMacNumber.Checked = false;
                        break;
                }
                //_service.LotMachine.Log.Info(string.Concat("更新机器状态：", machineStatus, "界面结束"));
            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("更新机器状态界面异常。错误信息：", exception.Message));
            }

        }
        void ShowTicketPrinting(L_LotTicketEntity ticketEntity)
        {
            try
            {
                _ticket = ticketEntity;

                int length = _service.WaitPrintTicketCount;
                this.llblViewQueue.Text = length.ToString();

                this.llblErrorView.Text = "";
                if (ticketEntity.L_Code == "recharge")
                {
                    this.lblTicketCode.Text = "充值中……";
                }
                else if (ticketEntity.L_Code == "getbouns")
                {
                    this.lblTicketCode.Text = "兑奖中……";
                }
                else
                {
                    this.lblTicketCode.Text = "出票中……";
                }
                this.llblTicketSn.Text = ticketEntity.L_TicketSn;
                //this.lblMoney.Text = ticketEntity.L_Money.ToString("0");

                this.llblViewLastTicket.Text = Service.TotalTicket.ToString();
                this.lblTotalMoney.Text = Service.TotalMoney.ToString("0");
                //Application.DoEvents();
            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("更新出票中界面异常。错误信息：", exception.Message));
            }

        }
        void ShowTicketPrinted(L_LotTicketEntity ticketEntity)
        {
            try
            {
                _ticket = ticketEntity;

                this.lblTicketCode.Text = "完成！";
                this.llblTicketSn.Text = "";
                this.llblViewLastTicket.Text = Service.TotalTicket.ToString();
                this.lblTotalMoney.Text = Service.TotalMoney.ToString("0");

                //_service.LotMachine.Log.Debug(string.Concat("生产流水号：", ticketEntity.L_TicketSn, " 结束更新出票完成界面"));
            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("更新出票完成界面异常。错误信息：", exception.Message));
            }

        }
        public void ShowLotMachineInfo()
        {
            try
            {
                M_MachineEntity machineEntity = Service.LotMachine.MachineInfo;
                string typeDesc = EnumDescription.GetFieldText((MachineType)machineEntity.M_Type);

                this.gbxMachine.Text = string.Concat(machineEntity.M_ZxNumber, "[", typeDesc, "]");
                //this.chkMacNumber.Text = machineEntity.M_Number;
                this.llblMachineConfig.Text = machineEntity.M_Number;
                //string playtype = GetMachinePlayType();
                //this.toolTip1.SetToolTip(this.llblMachineConfig, playtype);
                //this.lblPlayType.
                //Application.DoEvents();

                //抓取竞彩对阵
                this.chkAllowSnapJcMatch.Checked = false;
                string address = System.Configuration.ConfigurationManager.AppSettings.Get("Address");
                if (address == null) address = "";
                //if (machineEntity.M_Type == (int)MachineType.TjJingCaiV1||machineEntity.M_Type == (int)MachineType.JxJingCaiV1 || (machineEntity.M_Type == (int)MachineType.HnTiCai && address.IndexOf("竞彩") > -1) || (machineEntity.M_Type == (int)MachineType.JsTiCai && address.IndexOf("竞彩") > -1))
                //{
                //    this.chkAllowSnapJcMatch.Visible = true;

                //    //抓取对阵勾选保存到本地配置
                //    string macStr = LocalConfigUtil.GetConfig(_localConfig_SnapJcMatchMacKey);
                //    if (null != macStr)
                //    {
                //        _localConfig_SnapJcMatchMacValue = macStr;
                //        this.chkAllowSnapJcMatch.Checked = _localConfig_SnapJcMatchMacValue.Contains("[" + machineEntity.M_Number + "]");
                //    }
                //}
                //else
                //{
                //    this.chkAllowSnapJcMatch.Visible = false;

                //}


                ////抓取足彩对阵、排三限号
                //this.checkBoxSnapZcPs.Checked = false;
                //if (machineEntity.M_Type == (int)MachineType.JxTiCaiMachineV2 || (machineEntity.M_Type == (int)MachineType.JsTiCai && address.IndexOf("体彩") > -1) || (machineEntity.M_Type == (int)MachineType.HnTiCai && address.IndexOf("体彩") > -1))
                //{
                //    this.checkBoxSnapZcPs.Visible = true;

                //    //抓取对阵勾选保存到本地配置
                //    string macStr = LocalConfigUtil.GetConfig(_localConfig_SnapZcPsKey);
                //    if (null != macStr)
                //    {
                //        _localConfig_SnapZcPsValue = macStr;
                //        this.checkBoxSnapZcPs.Checked = _localConfig_SnapZcPsValue.Contains("[" + machineEntity.M_Number + "]");
                //    }
                //}
                //else
                //{
                //    this.checkBoxSnapZcPs.Visible = false;
                //}



                //抓取多乐彩开奖号码
                //this.checkBoxSnapOpenCode.Checked = true;
                //this.checkBoxSnapOpenCode_force.Checked = false;
                //if (machineEntity.M_Type == (int)MachineType.JxTiCaiMachineV3 || (machineEntity.M_Type == (int)MachineType.JsTiCai && address.IndexOf("多乐彩") > -1) || machineEntity.M_Type == (int)MachineType.HbGaoPinCai)
                //{

                //抓取对阵勾选保存到本地配置
                string macStr = LocalConfigUtil.GetConfig(_localConfig_SnapOpenCodeKey);
                if (null != macStr)
                {
                    _localConfig_SnapOpenCodeValue = macStr;
                    this.checkBoxSnapOpenCode.Checked = _localConfig_SnapOpenCodeValue.Contains("[" + machineEntity.M_Number + "]");
                }
                //}
                //else
                //{
                //    this.checkBoxSnapOpenCode.Visible = false;
                //}
            }
            catch (Exception exception)
            {
                LoggingService.Info(string.Concat("更新机器信息界面异常。错误信息：", exception.Message));
            }

        }
        //private string GetMachinePlayType()
        //{
        //    try
        //    {
        //        M_MachinePlayService MachinePlayService = new M_MachinePlayService();
        //        List<M_MachinePlayEntity> MachinePlayList = MachinePlayService.GetMachineLotListByMid(Service.LotMachine.MachineInfo);
        //        if (null != MachinePlayList && MachinePlayList.Count > 0)
        //        {
        //            StringBuilder PlayTypesString = new StringBuilder();
        //            foreach (M_MachinePlayEntity MachinePlay in MachinePlayList)
        //            {
        //                if (PlayTypesString.Length > 0)
        //                {
        //                    PlayTypesString.Append("、");
        //                }
        //                PlayTypesString.Append(LotPlayTypeHashTable.GetLotName(MachinePlay.P_LotId));
        //                PlayTypesString.Append(LotPlayTypeHashTable.GetPlayName(MachinePlay.P_PlayId));
        //            }
        //            if (PlayTypesString.Length > 0)
        //            {
        //                return string.Format("【{0}】", PlayTypesString.ToString());
        //            }
        //            else
        //            {
        //                return "【配置的可出彩票类型不可用】";
        //            }
        //        }
        //        else
        //        {
        //            return "【没有配置可出彩票类型】";
        //        }
        //    }
        //    catch (Exception exception)
        //    {
        //        LoggingService.Info(string.Concat("更新机器可出票玩法异常。错误信息：", exception.Message));
        //        return "";
        //    }
        //}

        public void ReverseSelect()
        {
            this.chkMacNumber.Checked = !this.chkMacNumber.Checked;
        }

        public void Start()
        {
            if (IsSelected)
            {
                Service.Start();
                this.chkMacNumber.Checked = false;//启动后，默认取消机器号的选中状态
            }
        }

        public void Stop()
        {
            if (IsSelected)
            {
                Service.Stop();
            }
        }
        public void ReGetdata()
        {
            if (IsSelected)
            {
                try
                {
                    DecodePrintData decodePrintData = new DecodePrintData();
                    Service.ReGetdata(Service.CurrentTicket, decodePrintData);
                    this.llblErrorView.Text = "";
                    StopWarn();
                }
                catch (Exception ex)
                {
                    Service.Stop();
                    Service.LotMachine.Log.Error(ex);
                }
                Service.Wait4Job.Set();
            }
        }
        public void Continue()
        {
            if (IsSelected)
            {
                if (Service.LotMachine.MachineInfo.M_Status == (int)MachineStatusEnum.Error)
                {
                    Service.LotMachine.Log.Info("清除错误并继续出票");
                    Service.ClearError((int)MachineErrorType.BetFail);
                }
            }
        }
        public void ClearLimitError()
        {
            if (IsSelected)
            {
                if (Service.LotMachine.MachineInfo.M_Status == (int)MachineStatusEnum.Error)
                {
                    Service.LotMachine.Log.Info("清除停售/限号弹窗并继续出票");
                    Service.ClearError((int)MachineErrorType.JcStopSale);
                }
            }
        }
        public void ForceStop()
        {
            if (IsSelected)
            {
                Service.LotMachine.ForceStop();
            }
        }
        public void ReturnTopGui()
        {
            if (IsSelected)
            {
                if (_service.LotMachine.MachineInfo.M_Status == (int)MachineStatusEnum.Stopped)
                {
                    new MethodInvoker(_service.LotMachine.ReturnTopGui).BeginInvoke(null, null);
                }
            }
        }
        public void Login()
        {
            if (IsSelected)
            {
                if (_service.LotMachine.MachineInfo.M_Status == (int)MachineStatusEnum.Stopped)
                {
                    new MethodInvoker(_service.LotMachine.Login).BeginInvoke(null, null);
                }
            }
        }
        public void Test()
        {
            if (IsSelected)
            {
                if (_service.LotMachine.MachineInfo.M_Status == (int)MachineStatusEnum.Stopped)
                {
                    new MethodInvoker(_service.PrintTestTicket).BeginInvoke(null, null);
                }
            }
        }
        public void UpdateMoney()
        {
            if (IsSelected)
            {
                if (_service.LotMachine.MachineInfo.M_Status == (int)MachineStatusEnum.Stopped)
                {
                    new MethodInvoker(_service.UpdateMachineMoney).BeginInvoke(null, null);
                    //new Action<int, string, DateTime, DateTime>(_service.UpdateMachineMoney).BeginInvoke(type, typename, from, to, null, null);
                }
            }
        }
        public void PrintDayReport(int type, string typename, DateTime from, DateTime to)
        {
            if (IsSelected)
            {
                if (_service.LotMachine.MachineInfo.M_Status == (int)MachineStatusEnum.Stopped)
                {
                    //new MethodInvoker(_service.PrintDayReport).BeginInvoke(null, null);
                    new Action<int, string, DateTime, DateTime>(_service.PrintDayReport).BeginInvoke(type, typename, from, to, null, null);
                }
            }
        }
        public void Clear()
        {
            if (IsSelected)
            {
                if (_service.LotMachine.MachineInfo.M_Status == (int)MachineStatusEnum.Stopped)
                {
                    new MethodInvoker(_service.ClearAmount).BeginInvoke(null, null);
                }
            }
        }
        public void GetAndPrintLastTicket()
        {
            if (IsSelected)
            {
                new MethodInvoker(_service.GetAndPrintLastTicket).BeginInvoke(null, null);
            }
        }

        //public void TransferTasks(List<M_MachineEntity> desMachineList)
        //{
        //    if(IsSelected)
        //    {
        //        try
        //        {
        //            string srcMachineNumber = _service.LotMachine.MachineInfo.M_Number;
        //            if (_service.LotMachine.MachineInfo.M_Status == (int)MachineStatusEnum.Stopped)
        //            {
        //                _service.TransferTasks(desMachineList);
        //            }
        //            else
        //            {
        //                MessageService.ShowMessage(string.Concat("切票前请先停止", srcMachineNumber, "出票"),"操作提示");
        //            }
        //        }
        //        catch(Exception exception)
        //        {
        //            MessageService.ShowMessage(string.Concat("转移票异常。错误信息：", exception), "操作提示");
        //        }
        //    }
        //}

        public event EventHandler CheckedChanged;
        private void chkMacNumber_CheckedChanged(object sender, EventArgs e)
        {
            if (CheckedChanged != null)
            {
                CheckedChanged(sender, e);
            }
        }

        private void llblErrorView_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.llblErrorView.Text))
                {
                    return;
                }
                ErrorDialog errorDialog = new ErrorDialog(_service, _ticket, _decodePrintData, _errorMessage);
                errorDialog.ShowDialog();
                switch (errorDialog.Result)
                {
                    case ErrorProcessEnum.Abort:
                        this.chkMacNumber.Checked = true;
                        break;
                }
                this.llblErrorView.Text = "";
            }
            catch (Exception exception)
            {
                MessageService.ShowMessage(string.Concat("错误处理对话框异常。错误信息：", exception.Message));
            }
        }

        private void llblViewQueue_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                TicketList ticketList = new TicketList(_service.LotMachine.MachineInfo.M_Number, _service.WaitPrintTickets.ToArray());
                ticketList.Show();
            }
            catch (Exception exception)
            {
                MessageService.ShowMessage(string.Concat("查看等待出票队列异常。错误信息：", exception.Message));
            }
        }

        private void llblViewLastTicket_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                LastTicket ticketList = new LastTicket(_service.LotMachine.MachineInfo.M_Number, _service.LotMachine.VirtualPrinter.LogPath);
                ticketList.Show();
            }
            catch (Exception exception)
            {
                MessageService.ShowMessage(string.Concat("查看出票记录异常。错误信息：", exception.Message));
            }
        }

        private void llblMachineConfig_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                if (_service.LotMachine.MachineInfo.M_Status != (int)MachineStatusEnum.Stopped)
                {
                    MessageService.ShowMessage("修改机器配置前需要停止出票");
                    return;
                }
                DlgMachineEdit dlgMachineEdit = new DlgMachineEdit(_service.LotMachine.MachineInfo);
                dlgMachineEdit.ShowDialog();
            }
            catch (Exception exception)
            {
                MessageService.ShowMessage(string.Concat("查看机器配置异常。错误信息：", exception.Message));
            }
        }

        private void llblInputTicketCode_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                InputTicketCodeDialog dlgInputTicketCode = new InputTicketCodeDialog(_service);
                DialogResult dialogResult = dlgInputTicketCode.ShowDialog();
                if (dialogResult == DialogResult.OK)
                {
                    ShowTicketPrinted(_service.CurrentTicket);
                }
            }
            catch (Exception exception)
            {
                MessageService.ShowMessage(string.Concat("手工输入票号对话框异常。错误信息：", exception.Message));
            }
        }

        private void chkAllowSnapJcMatch_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                _service.LotMachine.MachineInfo.AllowSnapJcMatch = this.chkAllowSnapJcMatch.Checked;

                string macStr = "[" + Service.LotMachine.MachineInfo.M_Number + "]";
                _localConfig_SnapJcMatchMacValue = _localConfig_SnapJcMatchMacValue.Replace(macStr, "");
                if (this.chkAllowSnapJcMatch.Checked)
                {
                    _localConfig_SnapJcMatchMacValue += macStr;
                }

                LocalConfigUtil.SaveConfig(_localConfig_SnapJcMatchMacKey, _localConfig_SnapJcMatchMacValue);
            }
            catch (Exception exception)
            {
                Service.LotMachine.Log.Info(string.Concat("更改抓取【竟彩对阵】选项异常。错误信息：", exception.ToString()));
                MessageService.ShowMessage(string.Concat("更改抓取【竟彩对阵】选项异常。错误信息：", exception.Message));
            }
        }

        private void llblTicketSn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                TicketInfo ticket = new TicketInfo(_ticket);
                ticket.Show();
            }
            catch (Exception exception)
            {
                MessageService.ShowMessage(string.Concat("查看票信息异常。错误信息：", exception.Message));
            }
        }

        private void checkBoxSnapZcPs_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                _service.LotMachine.MachineInfo.AllowSnapZcPs = this.checkBoxSnapZcPs.Checked;

                string macStr = "[" + Service.LotMachine.MachineInfo.M_Number + "]";
                _localConfig_SnapZcPsValue = _localConfig_SnapZcPsValue.Replace(macStr, "");
                if (this.checkBoxSnapZcPs.Checked)
                {
                    _localConfig_SnapZcPsValue += macStr;
                }

                LocalConfigUtil.SaveConfig(_localConfig_SnapZcPsKey, _localConfig_SnapZcPsValue);
            }
            catch (Exception exception)
            {
                Service.LotMachine.Log.Info(string.Concat("更改抓取【足彩对阵和排三限号列表】选项异常。错误信息：", exception.ToString()));
                MessageService.ShowMessage(string.Concat("更改抓取【足彩对阵和排三限号列表】选项异常。错误信息：", exception.Message));
            }
        }

        private void checkBoxSnapOpenCode_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                _service.LotMachine.MachineInfo.AllowSnapOpenCode = this.checkBoxSnapOpenCode.Checked;

                string macStr = "[" + Service.LotMachine.MachineInfo.M_Number + "]";
                _localConfig_SnapOpenCodeValue = _localConfig_SnapOpenCodeValue.Replace(macStr, "");
                if (this.checkBoxSnapOpenCode.Checked)
                {
                    _localConfig_SnapOpenCodeValue += macStr;
                }

                LocalConfigUtil.SaveConfig(_localConfig_SnapOpenCodeKey, _localConfig_SnapOpenCodeValue);
            }
            catch (Exception exception)
            {
                Service.LotMachine.Log.Info(string.Concat("更改抓取【多乐彩开奖号码】选项异常。错误信息：", exception.ToString()));
                MessageService.ShowMessage(string.Concat("更改抓取【多乐彩开奖号码】选项异常。错误信息：", exception.Message));
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            new DlgMachineLot(_service.LotMachine.MachineInfo).ShowDialog();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            new DlgDaySales(_service.LotMachine.MachineInfo).ShowDialog();
        }

        private void checkBoxSnapOpenCode_force_CheckedChanged(object sender, EventArgs e)
        {
            _service.LotMachine.MachineInfo.AllowSnapOpenCode_force = this.checkBoxSnapOpenCode_force.Checked;
        }
    }
}

