using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using ChuPiao.AutoPrint.Machine.Models;
using ChuPiao.Common.Models;

namespace ChuPiao.AutoPrint.Machine.TjJingCaiV1.MacLotPlayMode
{
    /// <summary>
    /// 彩种玩法投注模式（键码发送、电子票解析）
    /// </summary>
    public abstract class MacLotPlayModeService : IMacLotPlayModeService
    {
        /// <summary>
        /// 多乐彩号码区域的分隔符
        /// </summary>
        public string DlSplitCode = "------------------------------------------";

        public string DtSplitStr = "-";

        /// <summary>
        /// 号码区域的分隔符
        /// </summary>
        public string SplitCode = "------------------------------------------";

        /// <summary>
        /// 演示票
        /// </summary>
        public string TestStr = "演示票";
        /// <summary>
        /// 某个场次多个投注结果分隔符 ,
        /// </summary>
        public string SplitStr = ",";
        /// <summary>
        /// 投注号码分隔符 /
        /// </summary>
        public string CodeSplitStr = "/";
        public string CodeSplitStr_Cg = "$";
        /// <summary>
        /// 场次与投注结果的分隔符 →
        /// </summary>
        public string MatchCodeSplitStr = "→";

        /// <summary>
        /// 竞篮 110129607500070234599800 【2020.6.17，竞足竞篮之前一致的】
        /// </summary>
        public string RegexTicketCode_Jl = "[0-9]{24}";
        /// <summary>
        /// 200243-945712-189922-67  【2020.9.3 竞足竞篮又一致了 [0-9]{24}】
        /// </summary>
        public string RegexTicketCode_Jc = "[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{2}";
        /// <summary>
        /// 110129-607500-070234-599800
        /// </summary>
        public string RegexTicketCode = "[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}";
        /// <summary>
        /// 金额提取的正则表达式 合计:\s{1,}\d{1,5}元
        /// </summary>
        //public string RegexMoney = @"合计:\s{1,}\d{1,5}元";
        //public string RegexMoney = @"合计:\s{1,}\d{1,5}\s{0,}\D{1}";
        public string RegexMoney = @"合计\s{1,}\d{1,5}\s{0,}\D{1}";
        /// <summary>
        /// 密码提取的正则表达式 \d{8}
        /// </summary>
        public string RegexPassword_Jc = @"\d{8}";
        public string RegexPassword = @"\d{6}";
        /// <summary>
        /// 出票时间提取的正则表达式 \d{4}-\d{2}-\d{2}\s{1}\d{2}:\d{2}:\d{2}
        /// </summary>
        //public string RegexSucTime = @"\d{4}-\d{2}-\d{2}\s{1}\d{2}:\d{2}:\d{2}";
        public string RegexSucTime = @"\d{2}/\d{2}/\d{2}\s{1}\d{2}:\d{2}:\d{2}";  //18/02/23
        /// <summary>
        /// 最高可能固定奖金提取的正则表达式 本票最高可能固定奖金:[\d,]{1,}\.\d{1,2}元
        /// </summary>
        public string RegexMaxGetMoney = @"本票最高可能固定奖金:[\d,]{1,}\.\d{1,2}元";

        /// <summary>
        /// 周几的前导键 break
        /// </summary>
        public string WeekInitKey = "break";
        /// <summary>
        /// 过关方式选择键 f2
        /// </summary>
        public string TzTypeSelectKey = "f2";
        /// <summary>
        /// 自由过关方式选择键 f8
        /// </summary>
        public string TzTypeSelectKey_freepass = "f8";
        public string TzTypeDeleteKey_freepass = "break";
        /// <summary>
        /// 投注结果确认键 f1
        /// </summary>
        public string ResultEnterKey = "f1";
        /// <summary>
        /// 大额票界限 500
        /// </summary>
        public int LargeMoneyLimit = 500;

        /// <summary>
        /// 票面玩法全称
        /// </summary>
        public string PlayFullName;
        #region Implementation of IMacLotPlayModeService

        /// <summary>
        /// 机器投注模式配置信息
        /// </summary>
        //M_MachineModeConfigEntity MachineModeConfig { get; set; }


        /// <summary>
        /// 获取大额票密码的键码
        /// </summary>
        /// <param name="ticketEntity"></param>
        /// <param name="machineEntity"></param>
        /// <returns></returns>
        public List<KeycodeInfo> GetPasswordKeycodeList(L_LotTicketEntity ticketEntity, M_MachineEntity machineEntity)
        {
            M_MachineModeConfigEntity machineModeConfig = machineEntity.MachineModeConfigs[ticketEntity.MacModeId];
            return GetPasswordKeycodeList(ticketEntity, machineEntity, machineModeConfig);
        }

        /// <summary>
        /// 获取大额票密码的键码
        /// </summary>
        /// <param name="ticketEntity"></param>
        /// <param name="machineEntity"></param>
        /// <param name="machineModeConfig"></param>
        /// <returns></returns>
        protected List<KeycodeInfo> GetPasswordKeycodeList(L_LotTicketEntity ticketEntity, M_MachineEntity machineEntity, M_MachineModeConfigEntity machineModeConfig)
        {
            List<KeycodeInfo> keycodeList = new List<KeycodeInfo>();
            if (ticketEntity.L_Money >= LargeMoneyLimit)
            {
                keycodeList.Add(new KeycodeInfo("wait1000", 1000));
                string password = machineEntity.M_ZxPassword;
                for (int i = 0; i < password.Length; i++)
                {
                    keycodeList.Add(new KeycodeInfo(password.Substring(i, 1), machineModeConfig.C_SendKeyInterval));
                }
                if (!string.IsNullOrEmpty(machineModeConfig.C_PasswordConfirmKey)) //确认输入密码
                {
                    keycodeList.Add(new KeycodeInfo(machineModeConfig.C_PasswordConfirmKey, machineModeConfig.C_PasswordConfirmKeyInterval));
                }
            }
            return keycodeList;
        }
        /// <summary>
        /// 机器投注模式配置信息
        /// </summary>
        /// <summary>
        /// 获得投注需要发送的键码列表
        /// </summary>
        /// <param name="ticketEntity"></param>
        /// <param name="machineEntity"></param>
        /// <returns></returns>
        public abstract List<KeycodeInfo> GetSendKeycodeList(L_LotTicketEntity ticketEntity, M_MachineEntity machineEntity);

        /// <summary>
        /// 电子票解析
        /// </summary>
        /// <param name="machineEntity"></param>
        /// <param name="decodePrintData"></param>
        /// <param name="ticketEntity"></param>
        public abstract void DecodeData(L_LotTicketEntity ticketEntity, M_MachineEntity machineEntity, DecodePrintData decodePrintData);

        public List<string> SplitString(string strContent, string strSplit)
        {
            List<string> result = new List<string>();
            if (!(strContent == null || strContent.Trim() == ""))
            {
                if (strContent.IndexOf(strSplit) < 0)
                {
                    result.Add(strContent);
                }
                else
                {
                    foreach (string s in Regex.Split(strContent, Regex.Escape(strSplit)))
                    {
                        if (string.IsNullOrEmpty(s)) continue; //忽略空字符串
                        result.Add(s.Trim());
                    }
                }
            }
            return result;
        }
        #endregion

        /// <summary>
        /// 解析大乐透加奖类型：0-无加奖(默认)；1-1.6倍；2-2倍；3-“加油中国”3元；4-“光芒焕新”3元
        /// </summary>
        /// <param name="ticketEntity"></param>
        /// <param name="decodePrintData"></param>
        /// <returns></returns>
        protected void DecodeLotoAddType(L_LotTicketEntity ticketEntity, DecodePrintData decodePrintData)
        {
            if (ticketEntity == null) throw new ArgumentNullException("ticketEntity");

            string input = decodePrintData.InputData.CaptureDataString;

            int index_start = input.IndexOf("★");
            if (index_start == -1)
                return;
            int index_end = input.IndexOf("★", index_start + 1);
            string tempstr = "";
            if (index_end != -1)
            {
                tempstr = input.Substring(index_start, index_end - index_start);
            }
            else
            {
                tempstr = input.Substring(index_start);
            }
            //★恭喜！二至九等奖奖金E\0M!aEX1.6E\0M\0!\0aE倍！★
            //★恭喜您获赠“光芒焕新”幸运奖E\0M!aE3E\0M\0!\0aE元！★


            if (tempstr.IndexOf("X2") != -1)
            {
                ticketEntity.L_AddType = 2;
            }
            else if (tempstr.IndexOf("X1.6") != -1)
            {
                ticketEntity.L_AddType = 1;
            }
            else if (tempstr.IndexOf("光芒焕新") != -1)
            {
                ticketEntity.L_AddType = 4;
            }
            else if (tempstr.IndexOf("加油中国") != -1)
            {
                ticketEntity.L_AddType = 3;
            }


            //Regex regex = new Regex(@"恭喜！二至九等奖奖金X2倍！");  //2倍
            //Match match = regex.Match(input);
            //if (match.Success)
            //{
            //    ticketEntity.L_AddType = 2;
            //}
            //else
            //{
            //    regex = new Regex(@"恭喜！二至九等奖奖金X1.6倍！");  //1.6倍
            //    match = regex.Match(input);
            //    if (match.Success)
            //    {
            //        ticketEntity.L_AddType = 1;
            //    }
            //    else
            //    {
            //        regex = new Regex(@"恭喜您获赠“加油中国”幸运奖3元！");  //“加油中国”3元
            //        match = regex.Match(input);
            //        if (match.Success)
            //        {
            //            ticketEntity.L_AddType = 3;
            //        }
            //        else
            //        {
            //            regex = new Regex(@"恭喜您获赠“光芒焕新”幸运奖3元！");  //“光芒焕新”3元
            //            match = regex.Match(input);
            //            if (match.Success)
            //            {
            //                ticketEntity.L_AddType = 4;
            //            }
            //        }

            //    }
            //}
        }

        /// <summary>
        /// 解析乐善码，大乐透加奖，追加玩法送一注机选乐善码
        /// </summary>
        /// <param name="ticketEntity"></param>
        /// <param name="decodePrintData"></param>
        /// <returns></returns>
        protected void DecodeLotoCode(L_LotTicketEntity ticketEntity, DecodePrintData decodePrintData)
        {
            if (ticketEntity == null) throw new ArgumentNullException("ticketEntity");

            string input = decodePrintData.InputData.CaptureDataString;

            string strTemp;

            int index = input.LastIndexOf(SplitCode);
            if (index != -1)
            {
                strTemp = input.Substring(index);

                Regex regex = new Regex(@"(\d{2}\s{1}){4}\d{2}(\s){1,4}(\+)(\s){1,4}(\d){2}\s{1}(\d){2}");  //02 06 14 18 24  +  04 07
                Match match = regex.Match(strTemp);
                if (match.Success)
                {
                    ticketEntity.L_Code = string.Format("{0};{1}", ticketEntity.L_Code, Regex.Replace(match.Value, @"(\s){1,4}(\+)(\s){1,4}", "|").Replace(" ", ","));
                }
            }
        }

        /// <summary>
        /// 解析电子票头部
        /// </summary>
        /// <param name="ticketEntity"></param>
        /// <param name="decodePrintData"></param>
        /// <returns></returns>
        protected bool DecodeHead(L_LotTicketEntity ticketEntity, DecodePrintData decodePrintData)
        {
            if (ticketEntity == null) throw new ArgumentNullException("ticketEntity");
            try
            {

                string input = decodePrintData.InputData.CaptureDataString;

                string strTemp;
                if (input.Length < 1)
                {
                    decodePrintData.ErrorMessage += "电子票的长度为0；";
                    return false;
                }

                //Regex regex = new Regex(@"第\s{0,}\d{" + ticketEntity.L_Expect.Length + "}期");
                //Match match = regex.Match(input);
                //if (!match.Success)
                //{
                //    decodePrintData.ErrorMessage += "没有找到期号；";
                //    return false;
                //}
                //string expect = Regex.Replace(match.Value, @"\D", "");
                //decodePrintData.L_PrintTicketEntity.P_Expect = expect;
                //if (expect != ticketEntity.L_Expect)
                //{
                //    decodePrintData.ErrorMessage += string.Format("期号不匹配，软件期号：{0} 电子票期号：{1}", ticketEntity.L_Expect,
                //                                                  expect);
                //    return false;
                //}

                Regex regex = new Regex(RegexTicketCode);
                Match match = regex.Match(input);
                if (!match.Success)
                {
                    decodePrintData.ErrorMessage += "没有找到票号；";
                    return false;
                }

                //票号   110129-607500-070234-599800 763963 8tQXuQ
                string ticketCode = match.Value;
                decodePrintData.L_PrintTicketEntity.P_TicketCode = Regex.Replace(ticketCode, @"\D", "");
                //查票号的位置
                int position = input.IndexOf(ticketCode);
                if (position == -1)
                {
                    decodePrintData.ErrorMessage += "没有找到票号；";
                    return false;
                }
                //指针移到票号的结束位置
                position += ticketCode.Length;
                //票号后面是3个空格
                position += 1;

                //密码
                string password = input.Substring(position, 6);
                regex = new Regex(RegexPassword);
                match = regex.Match(password);
                if (!match.Success)
                {
                    decodePrintData.ErrorMessage += string.Format("密码格式错误，非6位数字：{0}；", password);
                    return false;
                }
                decodePrintData.L_PrintTicketEntity.P_Password = password;
                //指针移到随机码的结束位置
                position += 6;
                //随机码后面是2个空格
                position += 2;

                //6位随机码
                string randomCode = input.Substring(position, 6);
                decodePrintData.L_PrintTicketEntity.P_RandomCode = randomCode;

                //期号  第 17055期
                strTemp = input.Substring(position);


                //销售点   13-013968-101
                regex = new Regex(@"-\d{6}-");
                match = regex.Match(strTemp);
                if (!match.Success)
                {
                    decodePrintData.ErrorMessage += "没有找到销售点；";
                    return false;
                }
                decodePrintData.L_PrintTicketEntity.P_SaleAddress = Regex.Replace(match.Value, @"\D", "").Substring(1);

                //演示票，不解析金额，因为只有：   “合计:    样票”字样
                if (input.IndexOf(TestStr) == -1)
                {
                    //金额 12187期 共1期  12/07/12开奖d倍:85  合计:170元
                    regex = new Regex(@"\d{1,5}元");
                    match = regex.Match(strTemp);
                    if (!match.Success)
                    {
                        decodePrintData.ErrorMessage += "没有找到金额";
                        return false;
                    }
                    string money = Regex.Replace(match.Value, @"\D", "");
                    decodePrintData.L_PrintTicketEntity.P_Money = decimal.Parse(money);
                    if (decimal.Parse(money) != ticketEntity.L_Money)
                    {
                        decodePrintData.ErrorMessage += string.Format("金额不匹配，软件金额：{0} 电子票金额：{1}", ticketEntity.L_Money,
                                                                      money);
                        return false;
                    }
                    decodePrintData.L_PrintTicketEntity.P_Money = decimal.Parse(money);
                }
                return true;
            }
            catch (Exception exception)
            {
                decodePrintData.ErrorMessage += exception.ToString();
                return false;
            }

        }

        /// <summary>
        /// 解析电子票头部
        /// </summary>
        /// <param name="ticketEntity"></param>
        /// <param name="decodePrintData"></param>
        /// <returns></returns>
        protected bool DecodeHeadJc(L_LotTicketEntity ticketEntity, M_MachineEntity machineEntity, DecodePrintData decodePrintData)
        {
            if (ticketEntity == null) throw new ArgumentNullException("ticketEntity");
            try
            {

                string input = decodePrintData.InputData.CaptureDataString;
                if (input.Length < 1)
                {
                    decodePrintData.ErrorMessage += "电子票的长度为0；";
                    return false;
                }

                Regex regex = new Regex(RegexTicketCode_Jc);
                Match match = regex.Match(input);
                string ticketCode = "";
                if (!match.Success)
                {
                    regex = new Regex(RegexTicketCode_Jl);
                    match = regex.Match(input);

                    if (!match.Success)
                    {
                        decodePrintData.ErrorMessage += "没有找到票号；";
                        return false;
                    }
                    else
                    {
                        ticketCode = match.Value.Trim();
                    }
                }
                else
                {
                    ticketCode = match.Value;
                }

                //票号

                decodePrintData.L_PrintTicketEntity.P_TicketCode = ticketCode;
                //查票号的位置
                int position = input.IndexOf(ticketCode);
                if (position == -1)
                {
                    decodePrintData.ErrorMessage += "没有找到票号；";
                    return false;
                }
                //指针移到票号的结束位置
                position += ticketCode.Length;
                //票号后面是3个空格
                //position += 3;
                //200243-945712-189922-67 3B2F90AE 00546858
                position += 1;
                ////8位随机码
                //string randomCode = input.Substring(position, 8);
                //decodePrintData.L_PrintTicketEntity.P_RandomCode = randomCode;
                ////指针移到随机码的结束位置
                //position += 8;
                ////随机码后面是3个空格
                ////position += 3;
                //position += 1;
                ////密码
                //string password = input.Substring(position, 8);
                //regex = new Regex(RegexPassword);
                //match = regex.Match(password);
                //if (!match.Success)
                //{
                //    decodePrintData.ErrorMessage += string.Format("密码格式错误，非8位数字：{0}；", password);
                //    return false;
                //}
                //decodePrintData.L_PrintTicketEntity.P_Password = password;

                //2018.2.23 密码跟随机码换了个位置
                //密码
                string password = input.Substring(position, 8);
                regex = new Regex(RegexPassword_Jc);
                match = regex.Match(password);
                if (!match.Success)
                {
                    decodePrintData.ErrorMessage += string.Format("密码格式错误，非8位数字：{0}；", password);
                    return false;
                }
                decodePrintData.L_PrintTicketEntity.P_Password = password;
                //指针移到随机码的结束位置
                position += 8;
                //随机码后面是3个空格
                //position += 3;
                position += 1;
                //8位随机码
                string randomCode = input.Substring(position, 8);
                decodePrintData.L_PrintTicketEntity.P_RandomCode = randomCode;

                //销售点
                //regex = new Regex(@"销售点(:|：)\d{1,}\s{1,}");
                //match = regex.Match(input);
                //if (!match.Success)
                //{
                //    decodePrintData.ErrorMessage += "没有找到销售点；";
                //    return false;
                //}
                //decodePrintData.L_PrintTicketEntity.P_SaleAddress = Regex.Replace(match.Value, @"\D", "");
                //decodePrintData.L_PrintTicketEntity.P_SaleAddress = machineEntity.M_ZxNumber;

                //期号
                //if (position + 38 > input.Length)
                //{
                //    decodePrintData.ErrorMessage += "没有找到期号；";
                //    return false;
                //}
                //strTemp = input.Substring(position + 18, 20);
                //regex = new Regex(@"\d{" + ticketEntity.L_Expect.Length + "}期");
                //match = regex.Match(strTemp);
                //if (!match.Success)
                //{
                //    decodePrintData.ErrorMessage += "没有找到期号；";
                //    return false;
                //}
                //string expect = Regex.Replace(match.Value, @"\D", "");
                //decodePrintData.L_PrintTicketEntity.P_Expect = expect;
                //if (expect != ticketEntity.L_Expect)
                //{
                //    decodePrintData.ErrorMessage += string.Format("期号不匹配，软件期号：{0} 电子票期号：{1}", ticketEntity.L_Expect,
                //                                                  expect);
                //    return false;
                //}

                //2018.2.23 找不到该字样了
                //int tmpStart = input.IndexOf("中国体育彩票");
                //if(tmpStart<0)
                //{
                //    decodePrintData.ErrorMessage += "没有找到中国体育彩票字样；";
                //    return false;
                //}
                //int tmpEnd = input.IndexOf(ticketCode);
                ////string strPlayCodetype = input.Substring(tmpStart, tmpEnd - tmpStart);//玩法及号码类型字符串
                //string strPlayCodetype = input.Substring(0, tmpEnd);//玩法及号码类型字符串
                //if (strPlayCodetype.IndexOf(PlayFullName) < 0)
                //{
                //    decodePrintData.ErrorMessage += "玩法不匹配；";
                //    return false;
                //}
                //string codetype = "";
                //switch (ticketEntity.L_CodeType)
                //{
                //    case 1001:
                //        codetype = "单场固定";
                //        break;
                //    case 2001:
                //        codetype = "2x1";
                //        break;

                //    case 3001:
                //        codetype = "3x1";
                //        break;
                //    case 3003:
                //        codetype = "3x3";
                //        break;
                //    case 3004:
                //        codetype = "3x4";
                //        break;

                //    case 4001:
                //        codetype = "4x1";
                //        break;
                //    case 4004:
                //        codetype = "4x4";
                //        break;
                //    case 4005:
                //        codetype = "4x5";
                //        break;
                //    case 4006:
                //        codetype = "4x6";
                //        break;
                //    case 4011:
                //        codetype = "4x11";
                //        break;

                //    case 5001:
                //        codetype = "5x1";
                //        break;
                //    case 5005:
                //        codetype = "5x5";
                //        break;
                //    case 5006:
                //        codetype = "5x6";
                //        break;
                //    case 5010:
                //        codetype = "5x10";
                //        break;
                //    case 5016:
                //        codetype = "5x16";
                //        break;
                //    case 5020:
                //        codetype = "5x20";
                //        break;
                //    case 5026:
                //        codetype = "5x26";
                //        break;

                //    case 6001:
                //        codetype = "6x1";
                //        break;
                //    case 6006:
                //        codetype = "6x6";
                //        break;
                //    case 6007:
                //        codetype = "6x7";
                //        break;
                //    case 6015:
                //        codetype = "6x15";
                //        break;
                //    case 6020:
                //        codetype = "6x20";
                //        break;
                //    case 6022:
                //        codetype = "6x22";
                //        break;
                //    case 6035:
                //        codetype = "6x35";
                //        break;
                //    case 6042:
                //        codetype = "6x42";
                //        break;
                //    case 6050:
                //        codetype = "6x50";
                //        break;
                //    case 6057:
                //        codetype = "6x57";
                //        break;

                //    case 7001:
                //        codetype = "7x1";
                //        break;
                //    case 7007:
                //        codetype = "7x7";
                //        break;
                //    case 7008:
                //        codetype = "7x8";
                //        break;
                //    case 7021:
                //        codetype = "7x21";
                //        break;
                //    case 7035:
                //        codetype = "7x35";
                //        break;
                //    case 7120:
                //        codetype = "7x120";
                //        break;

                //    case 8001:
                //        codetype = "8x1";
                //        break;
                //    case 8008:
                //        codetype = "8x8";
                //        break;
                //    case 8009:
                //        codetype = "8x9";
                //        break;
                //    case 8028:
                //        codetype = "8x28";
                //        break;
                //    case 8056:
                //        codetype = "8x56";
                //        break;
                //    case 8070:
                //        codetype = "8x70";
                //        break;
                //    case 8247:
                //        codetype = "8x247";
                //        break;
                //}
                //// if (strPlayCodetype.IndexOf(codetype) < 0)
                //if (input.IndexOf(codetype) < 0)
                //{
                //    decodePrintData.ErrorMessage += "过关方式不匹配；";
                //    return false;
                //}

                //过关方式
                regex = new Regex(@"过关方式\s{1,}(\d{1})x(\d{1,3})\s{1,}");
                match = regex.Match(input);
                if (match.Success)
                {
                    int ccNum = Int32.Parse(match.Groups[1].Value);
                    int zhushu=  Int32.Parse(match.Groups[2].Value);
                    decodePrintData.L_PrintTicketEntity.L_CodeType = ccNum * 1000 + zhushu;                  
                }
                else  
                {
                    regex = new Regex(@"单场固定");
                    match = regex.Match(input);
                    if (match.Success)
                    {
                        decodePrintData.L_PrintTicketEntity.L_CodeType = 1001;
                    }
                    else   //自由过关
                    {
                        regex = new Regex(@"\d{1}场-([\d,]{1,})关");
                        match = regex.Match(input);
                        if (match.Success)
                        {
                            string ggstr = string.Format("99{0}",match.Groups[1].Value.Replace(",",""));

                            decodePrintData.L_PrintTicketEntity.L_CodeType = Int32.Parse(ggstr);
                        }
                    }
                }
                


                //金额
                regex = new Regex(@"(\d{1,2})倍\s{1,}合计\s{1,}(\d{1,5})\s{0,}\D{1}");
                match = regex.Match(input);
                if (!match.Success)
                {
                    decodePrintData.ErrorMessage += "没有找到倍数、金额";
                    decodePrintData.DecodeSuccess = false;
                    return false;
                }
                decodePrintData.L_PrintTicketEntity.P_Mutiple = Int32.Parse(match.Groups[1].Value);
                decodePrintData.L_PrintTicketEntity.P_Money = decimal.Parse(match.Groups[2].Value);


                //出票时间
                try
                {
                    regex = new Regex(RegexSucTime);
                    match = regex.Match(input);
                    decodePrintData.L_PrintTicketEntity.P_SucTime = DateTime.Parse(match.Value);
                }
                catch (Exception exception)
                {
                    //decodePrintData.ErrorMessage += string.Concat("获取出票时间不正确。错误信息：",exception.Message);
                    //decodePrintData.DecodeSuccess = false;
                    //return;

                    decodePrintData.L_PrintTicketEntity.P_SucTime = DateTime.Now;      //2019.12.1  中心返回的票信息中不含出票时间，第一次出现这种情况
                }
                //本票最高可能固定奖金
                //if (ticketEntity.L_RateType == (int)JcRateType.Fixed)
                //{
                //固定赔率
                try
                {
                    regex = new Regex(RegexMaxGetMoney);
                    match = regex.Match(input);
                    string strTemp = match.Value.Replace("本票最高可能固定奖金:", "").Replace("元", "").Replace(",", "");
                    decodePrintData.L_PrintTicketEntity.L_MaxGetMoney = decimal.Parse(strTemp);
                }
                catch (Exception exception)
                {
                    decodePrintData.ErrorMessage += string.Concat("获取最高可能固定奖金不正确。错误信息：", exception.Message);
                    decodePrintData.DecodeSuccess = false;
                    return false;
                }
                //}

                return true;
            }
            catch (Exception exception)
            {
                decodePrintData.ErrorMessage += exception.Message;
                return false;
            }

        }

        /// <summary>
        /// 字符间插入字符串
        /// </summary>
        /// <param name="source">12345</param>
        /// <param name="chr">,</param>
        /// <returns>1,2,3,4,5</returns>
        protected string InsertChar(string source, string chr)
        {
            int length = source.Length;
            if (length > 1)
            {
                for (int i = length - 1; i > 0; i--)
                {
                    source = source.Insert(i, chr);
                }
            }

            return source;
        }

        /// <summary>
        /// 解析电子票中的场次
        /// </summary>
        /// <param name="Istr"></param>
        /// <returns></returns>
        protected string DealChangChi(string Istr)
        {
            string tmpstr = "";
            tmpstr = Istr.Replace("周", "");
            switch (tmpstr.Substring(0, 1))
            {
                case "一":
                    tmpstr = "1" + tmpstr.Substring(1, 3);
                    break;
                case "二":
                    tmpstr = "2" + tmpstr.Substring(1, 3);
                    break;
                case "三":
                    tmpstr = "3" + tmpstr.Substring(1, 3);
                    break;
                case "四":
                    tmpstr = "4" + tmpstr.Substring(1, 3);
                    break;
                case "五":
                    tmpstr = "5" + tmpstr.Substring(1, 3);
                    break;
                case "六":
                    tmpstr = "6" + tmpstr.Substring(1, 3);
                    break;
                case "日":
                    tmpstr = "7" + tmpstr.Substring(1, 3);
                    break;
                default:
                    tmpstr = tmpstr.Substring(1);
                    break;

            }
            return tmpstr;
        }
        #region SortDictionary
        /// <summary>
        /// 电子票上的赔率明细字符串 如：2#4.700,1#1.080 排序输出为 1#1.080,2#4.700  使其与用户投注号码的排序相同
        /// </summary>
        /// <param name="sort"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        protected string SortRateText(string sort, string input)
        {
            return input;   //不排序了，投注号码是什么顺序，就是什么顺序

            StringBuilder stringBuilder = new StringBuilder();
            Dictionary<string, string> tzTextDictionary = new Dictionary<string, string>();
            string[] tmparr = sort.Split(',');
            for (int i = 0; i < tmparr.Length; i++)
            {
                tzTextDictionary.Add(tmparr[i], "");
            }

            string[] tmpArr = input.Split(SplitStr.ToCharArray());
            foreach (string oneCode in tmpArr)
            {
                string[] arr = oneCode.Split('#');
                if (tzTextDictionary.ContainsKey(arr[0]))
                {
                    tzTextDictionary[arr[0]] = oneCode;
                }
            }
            foreach (string value in tzTextDictionary.Values)
            {
                if (value != "")
                {
                    stringBuilder.Append(value + SplitStr);
                }
            }
            string result = stringBuilder.ToString().TrimEnd(SplitStr.ToCharArray());
            return result;
        }
        /// <summary>
        /// 电子票上的投注号码字符串 如：2,1  排序输出为 1,2  使其与用户投注号码的排序相同
        /// </summary>
        /// <param name="sort"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        protected string SortCodeText(string sort, string input)
        {
            StringBuilder stringBuilder = new StringBuilder();
            Dictionary<string, string> tzTextDictionary = new Dictionary<string, string>();
            string[] tmparr = sort.Split(',');
            for (int i = 0; i < tmparr.Length; i++)
            {
                tzTextDictionary.Add(tmparr[i], "");
            }

            string[] tmpArr = input.Split(SplitStr.ToCharArray());
            foreach (string oneCode in tmpArr)
            {
                if (tzTextDictionary.ContainsKey(oneCode))
                {
                    tzTextDictionary[oneCode] = oneCode;
                }
            }
            foreach (string value in tzTextDictionary.Values)
            {
                if (value != "")
                {
                    stringBuilder.Append(value + SplitStr);
                }
            }
            string result = stringBuilder.ToString().TrimEnd(SplitStr.ToCharArray());
            return result;
        }

        #endregion

        /// <summary>
        ///赔率的严格检测
        /// </summary>
        ///<returns></returns>
        protected bool RateStrictCheck(string tmpRateDetail, string code)
        {
            bool rbool = false;
            try
            {
                string tmpCode = SplitRateDetailToCode(tmpRateDetail);

                if (tmpCode == code)
                {
                    rbool = true;
                }
            }
            catch
            {
                rbool = false;
            }
            return rbool;
        }
        protected string SplitRateDetailToCode(string tmpRateDetail)
        {
            List<string> tmpList = new List<string>();
            if (tmpRateDetail == "")
            {
                return "";
            }

            string[] tmpArr = tmpRateDetail.Split(CodeSplitStr.ToCharArray());
            string tmpStr = "";
            foreach (string tmpStr1 in tmpArr)
            {
                // 场次是否有4位
                string[] arronecode = tmpStr1.Split(MatchCodeSplitStr.ToCharArray());
                string changCi = arronecode[0];
                string[] arrPlayAndCode = arronecode[1].Split('[');
                tmpStr = arrPlayAndCode[1].Replace("]", "");
                //tmpStr = arronecode[1].Replace("[","").Replace("]",""); // [1#2.345,3#2.34]
                string[] tmpArr1 = tmpStr.Split(SplitStr.ToCharArray());
                List<string> tmpTZcode = new List<string>();
                foreach (string tmpStr2 in tmpArr1)
                {
                    string[] arrrate = tmpStr2.Split('#');
                    tmpTZcode.Add(arrrate[0]);

                    decimal tmpdecimal; //赔率是否正确
                    bool success = decimal.TryParse(arrrate[1], out tmpdecimal);
                    if (!success)
                    {
                        return "";
                    }
                }
                tmpList.Add(string.Format("{0}→{1}[{2}]", changCi, arrPlayAndCode[0], string.Join(",", tmpTZcode.ToArray())));
                //tmpList.Add(string.Format("{0}→[{1}]", changCi, string.Join(",", tmpTZcode.ToArray())));
            }
            return string.Join(CodeSplitStr, tmpList.ToArray());
        }
        /// <summary>
        /// 获取初始化键码
        /// </summary>
        /// <param name="machineModeConfig"></param>
        /// <returns></returns>
        public List<KeycodeInfo> GetInitKeycodeList(M_MachineModeConfigEntity machineModeConfig)
        {
            List<KeycodeInfo> keycodeList = new List<KeycodeInfo>();

            //防止在竞彩玩法选择界面光标失去焦点  （湖北检测出异常终端，看能否去掉所有多余动作）
            //keycodeList.Add(new KeycodeInfo("f5", machineModeConfig.C_ToInitModeKeysInterval));
            //keycodeList.Add(new KeycodeInfo("esc", machineModeConfig.C_SendKeyInterval));

            if (!string.IsNullOrEmpty(machineModeConfig.C_MacModeKeys))
            {
                foreach (string key in machineModeConfig.C_MacModeKeys.Split(','))
                {
                    //keycodeList.Add(new KeycodeInfo(key, machineModeConfig.C_MacModeKeysInterval));
                    keycodeList.Add(new KeycodeInfo(key, machineModeConfig.C_SendKeyInterval));
                }
            }
            return keycodeList;
        }


        /// <summary>
        /// 获取倍数键码
        /// </summary>
        /// <param name="machineModeConfig"></param>
        /// <returns></returns>
        public List<KeycodeInfo> GetMultipleKeycodeList(int multiple, M_MachineModeConfigEntity machineModeConfig)
        {
            List<KeycodeInfo> keycodeList = new List<KeycodeInfo>();

            keycodeList.Add(new KeycodeInfo(machineModeConfig.C_MultipleKey, machineModeConfig.C_MultipleKeyInterval));
            string strTemp = multiple.ToString();
            for (int i = 0; i < strTemp.Length; i++)
            {
                if (i != strTemp.Length - 1)
                {
                    keycodeList.Add(new KeycodeInfo(strTemp.Substring(i, 1), machineModeConfig.C_SendKeyInterval));
                }
                else
                {
                    keycodeList.Add(new KeycodeInfo(strTemp.Substring(i, 1), machineModeConfig.C_MultipleKeyInterval));
                }
            }
            if (!string.IsNullOrEmpty(machineModeConfig.C_MultipleConfirmKey))
            {
                keycodeList.Add(new KeycodeInfo(machineModeConfig.C_MultipleConfirmKey, machineModeConfig.C_MultipleConfirmKeyInterval));
            }
            return keycodeList;
        }

        /// <summary>
        /// 获取过关方式的键码
        /// </summary>
        /// <param name="codeType"></param>
        /// <param name="machineModeConfig"></param>
        /// <returns></returns>
        protected List<KeycodeInfo> GetGuoGuanKeycodeList(int codeType, M_MachineModeConfigEntity machineModeConfig)
        {
            List<KeycodeInfo> keycodeInfoList = new List<KeycodeInfo>();
            if (codeType != 1001)
            {
                if (!codeType.ToString().StartsWith("99"))
                //if (codeType / 100000 != 99)     //自由过关的codeType格式为 99*****
                {
                    //keycodeInfoList.Add(new KeycodeInfo(TzTypeSelectKey, machineModeConfig.C_PrintConfirmKeyInterval));
                    keycodeInfoList.Add(new KeycodeInfo(TzTypeSelectKey, machineModeConfig.C_SendKeyInterval));
                    switch (codeType)
                    {
                        //2关
                        case 2001:
                            //keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_PrintConfirmKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("2", machineModeConfig.C_SendKeyInterval));
                            break;
                        //3关
                        case 3001:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("2", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 3003:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("3", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 3004:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("4", machineModeConfig.C_SendKeyInterval));
                            break;
                        //4关
                        case 4001:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("2", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 4004:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("3", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 4005:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("4", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 4006:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("5", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 4011:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("6", machineModeConfig.C_SendKeyInterval));
                            break;
                        //5关
                        case 5001:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("2", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 5005:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("3", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 5006:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("4", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 5010:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("5", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 5016:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("6", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 5020:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("7", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 5026:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("8", machineModeConfig.C_SendKeyInterval));
                            break;
                        //6关
                        case 6001:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("2", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 6006:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("3", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 6007:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("4", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 6015:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("5", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 6020:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("6", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 6022:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("7", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 6035:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("8", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 6042:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("9", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 6050:
                            keycodeInfoList.Add(new KeycodeInfo("1", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 6057:
                            keycodeInfoList.Add(new KeycodeInfo("1", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("1", machineModeConfig.C_SendKeyInterval));
                            break;
                        //7关
                        case 7001:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("2", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 7007:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("3", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 7008:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("4", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 7021:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("5", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 7035:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("6", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 7120:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("7", machineModeConfig.C_SendKeyInterval));
                            break;
                        //8关
                        case 8001:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("2", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 8008:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("3", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 8009:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("4", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 8028:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("5", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 8056:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("6", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 8070:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("7", machineModeConfig.C_SendKeyInterval));
                            break;
                        case 8247:
                            keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                            keycodeInfoList.Add(new KeycodeInfo("8", machineModeConfig.C_SendKeyInterval));
                            break;
                    }
                    //keycodeInfoList.Add(new KeycodeInfo(machineModeConfig.C_PrintConfirmKey, machineModeConfig.C_PrintConfirmKeyInterval));
                }
                else
                {
                    keycodeInfoList.Add(new KeycodeInfo(TzTypeSelectKey_freepass, machineModeConfig.C_SendKeyInterval));
                    //把默认的数字去掉
                    keycodeInfoList.Add(new KeycodeInfo(TzTypeDeleteKey_freepass, machineModeConfig.C_SendKeyInterval));

                    foreach (int codetype in getCodeTypeList(codeType))
                    {
                        int gg = codetype / 1000;
                        keycodeInfoList.Add(new KeycodeInfo(gg.ToString(), machineModeConfig.C_SendKeyInterval));
                    }

                    keycodeInfoList.Add(new KeycodeInfo(TzTypeSelectKey_freepass, machineModeConfig.C_SendKeyInterval));

                }
            }
            else   //2021.3.22 彩票机拿过来，有可能默认的过关方式是无，所以单关票也还是选一下过关方式，就不依赖彩票机设置了
            {
                keycodeInfoList.Add(new KeycodeInfo(TzTypeSelectKey, machineModeConfig.C_SendKeyInterval));
                keycodeInfoList.Add(new KeycodeInfo("0", machineModeConfig.C_SendKeyInterval));
                keycodeInfoList.Add(new KeycodeInfo("1", machineModeConfig.C_SendKeyInterval));
            }


            return keycodeInfoList;
        }

        /// <summary>
        /// 自由过关玩法，根据codetype得到过关类型列表,自由过关，L_CodeType的格式是 99XXXXX
        /// 2020.8.10 改规则：99开头，后面的字符串每一位代表N串1，比如 99234 表示2串1，3串1，4串1
        /// </summary>
        /// <param name="codetype"></param>
        /// <returns></returns>
        public static List<int> getCodeTypeList(int codetype)
        {
            List<int> list = new List<int>();

            string codetypeStr = codetype.ToString().Substring(2);

            for (int position = 0; position < codetypeStr.Length; position++)
            {
                int n = Int32.Parse(codetypeStr[position].ToString());
                list.Add(n * 1000 + 1);
            }



            //string codetypeStr = Convert.ToString(codetype % 100000, 2);   //转换成二进制

            //if (codetypeStr.Length < 2)
            //    return list;

            //for (int position = 2; position <= codetypeStr.Length; position++)
            //{
            //    if (codetypeStr[codetypeStr.Length - position] == '1')
            //    {
            //        list.Add(position * 1000 + 1);
            //    }
            //}

            return list;
        }

    }
}
