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

namespace ChuPiao.AutoPrint.Machine.TjJingCaiV1.MacLotPlayMode
{
    /// <summary>
    /// 彩种玩法投注模式（键码发送、电子票解析）
    /// </summary>
    public class MacLotPlayModePsZ6DtService : TjJingCaiV1.MacLotPlayMode.MacLotPlayModeService
    {
        public string RegexCode = @"胆码:(\s\d){1,9}([^\d]+)拖码:(\s\d){1,9}";//(\s\d){1,9}
        public string RegexMoney = @"\d{1,5}元";

        public MacLotPlayModePsZ6DtService()
        {
            base.SplitStr = "|";
        }

        #region 键码发送

        /// <summary>
        /// 获得投注需要发送的键码列表
        /// </summary>
        /// <param name="ticketEntity"></param>
        /// <param name="machineEntity"></param>
        /// <returns></returns>
        public override List<KeycodeInfo> GetSendKeycodeList(L_LotTicketEntity ticketEntity, M_MachineEntity machineEntity)
        {
            List<KeycodeInfo> keycodeList = new List<KeycodeInfo>();
            M_MachineModeConfigEntity machineModeConfig = machineEntity.MachineModeConfigs[ticketEntity.MacModeId];
            //初始化
            if(machineModeConfig.C_IsEveryInit)
            {
                if (!string.IsNullOrEmpty(machineModeConfig.C_MacModeKeys))
                {
                    foreach (string key in machineModeConfig.C_MacModeKeys.Split(','))
                    {
                        keycodeList.Add(new KeycodeInfo(key, machineModeConfig.C_MacModeKeysInterval));
                    }
                }
            }
            //投注号码
            List<string> codeList = SplitString(ticketEntity.L_Code, SplitStr);
            foreach (string oneCode in codeList)
            {
                string strTemp = oneCode.Replace(",","");
                for (int i = 0; i < strTemp.Length; i++)
                {
                    keycodeList.Add(new KeycodeInfo(strTemp.Substring(i, 1), machineModeConfig.C_SendKeyInterval));
                }
                //下个场次
                keycodeList.Add(new KeycodeInfo(machineModeConfig.C_NextKey, machineModeConfig.C_NextKeyInterval));
            }
            //删除最后一个NextKey
            int delIndex = keycodeList.Count - 1;
            keycodeList.RemoveAt(delIndex);

            //倍投);
            if(ticketEntity.L_Mutiple>1)
            {
                keycodeList.Add(new KeycodeInfo(machineModeConfig.C_MultipleKey, machineModeConfig.C_MultipleKeyInterval));
                string strTemp = ticketEntity.L_Mutiple.ToString();
                for (int i = 0; i < strTemp.Length; i++)
                {
                    keycodeList.Add(new KeycodeInfo( strTemp.Substring(i, 1), machineModeConfig.C_SendKeyInterval));
                }
                if(!string.IsNullOrEmpty(machineModeConfig.C_MultipleConfirmKey))
                {
                    keycodeList.Add(new KeycodeInfo( machineModeConfig.C_MultipleConfirmKey, machineModeConfig.C_MultipleConfirmKeyInterval));
                }
            }
            //打印确认
            keycodeList.Add(new KeycodeInfo( machineModeConfig.C_PrintConfirmKey, machineModeConfig.C_PrintConfirmKeyInterval));

            //是否输入密码
            //TODO 配置
            //if (ticketEntity.L_Money >= 500)
            //{
            //    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));
            //    }
            //}
            //是否输入密码
            keycodeList.AddRange(GetPasswordKeycodeList(ticketEntity, machineEntity, machineModeConfig));


            return keycodeList;
        }
        #endregion

        #region 电子票解析

        /// <summary>
        /// 解析电子票
        /// </summary>
        /// <param name="machineEntity"></param>
        /// <param name="decodePrintData"></param>
        /// <param name="ticketEntity"></param>
        public override void DecodeData(L_LotTicketEntity ticketEntity, M_MachineEntity machineEntity, DecodePrintData decodePrintData)
        {
            M_MachineModeConfigEntity machineModeConfig = machineEntity.MachineModeConfigs[ticketEntity.MacModeId];

            //抓取的返回
            decodePrintData.L_PrintTicketEntity.P_PrnText = TicketTextUtil.CompressAndEncrypt(decodePrintData.InputData.CaptureDataBytes);
            decodePrintData.L_PrintTicketEntity.P_SucTime = DateTime.Now;

            string input = decodePrintData.InputData.CaptureDataString;
            //input = input.Replace("大乐透12035期开奖01+04+30+32+35 08+12", "");
            string strTemp;
            //票号、密码、期号解析
            bool suc = DecodeHead(ticketEntity,decodePrintData);
            if(!suc)
            {
                decodePrintData.ErrorMessage = string.Format("头部解析错误：{0}", decodePrintData.ErrorMessage);
                decodePrintData.DecodeSuccess = false;
                return;
            }
            int position;
            Regex regex;
            Match match;

            //金额
            //int position = input.IndexOf(decodePrintData.L_PrintTicketEntity.P_TicketCode) + 70;
            //Regex regex=new Regex(RegexMoney);
            //strTemp = input.Substring(position, 20);
            //Match match = regex.Match(strTemp);
            //if(!match.Success)
            //{
            //    decodePrintData.ErrorMessage += "没有找到金额";
            //    decodePrintData.DecodeSuccess = false;
            //    return;
            //}
            //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);
            //    decodePrintData.DecodeSuccess = false;
            //    return;
            //}
            //投注号码
            //position = input.IndexOf("组选6胆拖");
            position = input.IndexOf("合计");
            //strTemp = Regex.Replace(input.Substring(position, 150), @"\n", "");
            strTemp = Regex.Replace(input.Substring(position), @"\n", "");

            regex=new Regex(RegexCode);
            //MatchCollection matchCollection = regex.Matches(strTemp);
            match = regex.Match(strTemp);
            decodePrintData.L_PrintTicketEntity.P_Code = ticketEntity.L_Code;
            if (machineModeConfig.C_IsCheckCode)
            {
                //进行号码检测
                string code = string.Empty;
                //foreach (Match oneMatch in matchCollection)
                //{
                //    code += oneMatch.Value+SplitStr;
                //}
                code = match.Value;
                string s1 = Regex.Replace(ticketEntity.L_Code, @"\D", "");
                string s2 = Regex.Replace(code, @"\D", "");
                if (s1 != s2)
                {
                    decodePrintData.ErrorMessage += string.Format("投注号码不匹配，软件号码：{0} 电子票号码：{1}", s1,
                                                          s2);
                    //把跟投注号码不匹配的电子票号设成"01,02,03,04,05"这种格式，以便入库

                    decodePrintData.L_PrintTicketEntity.P_Code = code;
                    decodePrintData.DecodeSuccess = false;
                    return;
                }
            }
            decodePrintData.L_PrintTicketEntity.P_SingleZhuShu = ticketEntity.L_SingleZhuShu;
            decodePrintData.DecodeSuccess = true;

        }
        #endregion
    }
}
