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

namespace ChuPiao.AutoPrint.Machine.TjJingCaiV1.MacLotPlayMode
{
    /// <summary>
    /// 竞彩篮球胜负
    /// </summary>
    public class MacLotPlayModeJlSfService : MacLotPlayModeService
    {
        public MacLotPlayModeJlSfService()
        {
            PlayFullName = "竞彩篮球胜负";
        }
        #region Overrides of MacLotPlayModeService

        /// <summary>
        /// 机器投注模式配置信息
        /// </summary>
        /// <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];

            //初始化，每次都需要进入玩法的投注内容输入界面
            keycodeList.AddRange(GetInitKeycodeList(machineModeConfig));

            //投注号码
            List<JcCodeEntity> codeList = DealPrintCode(ticketEntity.L_Code);
            foreach (JcCodeEntity jcCodeEntity in codeList)
            {
                //进入场次选择界面
                //keycodeList.Add(new KeycodeInfo(WeekInitKey, machineModeConfig.C_SendKeyInterval));
                //选择场次
                for (int i = 0; i < jcCodeEntity.ChangCi.Length; i++)
                {
                    keycodeList.Add(new KeycodeInfo(jcCodeEntity.ChangCi.Substring(i, 1), machineModeConfig.C_SendKeyInterval));
                }
                //选择投注号码
                foreach (string s in jcCodeEntity.OneCodeList)
                {
                    keycodeList.Add(new KeycodeInfo(s, machineModeConfig.C_SendKeyInterval));
                }
                //投注号码确认
                keycodeList.Add(new KeycodeInfo(ResultEnterKey, machineModeConfig.C_SendKeyInterval));
            }

            //倍投
            if (ticketEntity.L_Mutiple > 1)
            {
                keycodeList.AddRange(GetMultipleKeycodeList(ticketEntity.L_Mutiple, machineModeConfig));
            }

            //过关方式
            keycodeList.AddRange(GetGuoGuanKeycodeList(ticketEntity.L_CodeType, machineModeConfig));

            //打印确认
            keycodeList.Add(new KeycodeInfo(machineModeConfig.C_PrintConfirmKey, machineModeConfig.C_PrintConfirmKeyInterval));

            //是否输入密码
            keycodeList.AddRange(GetPasswordKeycodeList(ticketEntity, machineEntity, machineModeConfig));

            return keycodeList;
        }
        /// <summary>
        /// 拆分投注号码
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        protected List<JcCodeEntity> DealPrintCode(string code)
        {
            List<JcCodeEntity> codeList = new List<JcCodeEntity>();
            string[] changCiCodeArr = code.Split(CodeSplitStr.ToCharArray()); //表示有多少场
            foreach (string changCiCode in changCiCodeArr)
            {
                JcCodeEntity jcCodeEntity = new JcCodeEntity();

                string[] arr = changCiCode.Split(MatchCodeSplitStr.ToCharArray());

                jcCodeEntity.ChangCi = arr[0];
                string s = arr[1].Replace("[", "").Replace("]", "");
                string[] oneMatchCodeArr = s.Split(SplitStr.ToCharArray());
                jcCodeEntity.OneCodeList.AddRange(oneMatchCodeArr);
                codeList.Add(jcCodeEntity);
            }
            return codeList;
        }

        /// <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 = DecodeHeadJc(ticketEntity, machineEntity, decodePrintData);
            if (!suc)
            {
                decodePrintData.ErrorMessage = string.Format("头部解析错误：{0}", decodePrintData.ErrorMessage);
                decodePrintData.DecodeSuccess = false;
                return;
            }

            Regex regex;
            Match match;

            #region 投注号码及赔率
            List<string> dCodeList = new List<string>();
            List<string> dRateList = new List<string>();
            //金额
            int startposition = input.IndexOf(decodePrintData.L_PrintTicketEntity.P_TicketCode) + 60;
            int endposition = input.IndexOf("本票最高可能固定奖金", startposition);
            strTemp = input.Substring(startposition, endposition - startposition);//包含投注内容的字符串

            string[] tmpArr = strTemp.Split('\n');//投注内容临时数组
            regex = new Regex(@"周(一|二|三|四|五|六|日)\d{3}");
            for (int i = 0; i < tmpArr.Length; i++)
            {
                strTemp = tmpArr[i].Trim();
                match = regex.Match(strTemp);
                if (match.Success)
                {
                    strTemp = match.Value;
                    string strChangCi = DealChangChi(strTemp);//场次字符串 如：第1关  周四002
                    i++;//场次的下一行为球队名称 如：主队:莫斯科火车头 Vs 客队:毕尔巴鄂竞技  
                    i++;//球队名称的下一行为投注内容与赔率 如：胜@2.540元
                    string tmpCodeStr = "";
                    //while (!new Regex(@"M\0!\0a\0").IsMatch(tmpArr[i]) && !new Regex(@"选项固定奖金额").IsMatch(tmpArr[i]))
                    //while (!new Regex(@"第\d{1,}关").IsMatch(tmpArr[i]) && !new Regex(@"选项固定奖金额").IsMatch(tmpArr[i]))   //2017-05-27 关改成场了
                    while (!new Regex(@"第\d{1,}(关|场)").IsMatch(tmpArr[i]) && !new Regex(@"选项固定奖金额").IsMatch(tmpArr[i]))
                    {
                        //投注内容可能有多行
                        if (tmpArr[i].IndexOf("@") > 0 || new Regex(@"\d{0,}.\d{1,}元").IsMatch(tmpArr[i]))     //有单独行   3.650元
                        {
                            tmpCodeStr += tmpArr[i].Trim();
                        }
                        i++;

                        if (i >= tmpArr.Length)
                        {
                            break;
                        }
                    }
                    i--;
                    //if (ticketEntity.L_RateType == (int)JcRateType.Fixed)
                    //{
                        dRateList.Add(string.Format("{0}{1}[{2}]", strChangCi, MatchCodeSplitStr, DealRateCode(tmpCodeStr.Trim())));
                    //}
                    //else
                    //{
                    //    dCodeList.Add(string.Format("{0}{1}[{2}]", strChangCi, MatchCodeSplitStr, DealResultCode(tmpCodeStr.Trim())));
                    //}
                }
            }
            //解析组合过关的赔率
            decodePrintData.L_PrintTicketEntity.L_CodeRate = string.Join(CodeSplitStr, dRateList.ToArray());

            //投注内容
            decodePrintData.L_PrintTicketEntity.P_Code = SplitRateDetailToCode(decodePrintData.L_PrintTicketEntity.L_CodeRate);
            #endregion

            decodePrintData.DecodeSuccess = true;
        }

        /// <summary>
        /// 处理投注结果
        /// </summary>
        /// <param name="Istr"></param>
        /// <returns></returns>
        private string DealResultCode(string Istr)
        {
            string tmpstr = "";

            for (int i = 0; i < Istr.Length; i++)
            {
                switch (Istr.Substring(i, 1))
                {
                    case "+":
                        tmpstr += SplitStr;
                        break;
                    case "胜":
                        tmpstr += "1";
                        break;
                    case "负":
                        tmpstr += "2";
                        break;
                    default:
                        break;
                }
            }

            //电子票上的投注号码字符串 如：2,1  排序输出为 1,2  使其与用户投注号码的排序相同
            tmpstr = SortCodeText(CODE_SORT_DIECTIONRY, tmpstr);

            return tmpstr;

        }
        /// <summary>
        /// 处理赔率明细
        /// </summary>
        /// <param name="Istr"></param>
        /// <returns></returns>
        private string DealRateCode(string Istr)
        {
            Istr = Regex.Replace(Istr, @"[^胜负元@.+\d]{1}", "");

            string tmpstr = "";
            string OneStr = "";
            for (int i = 0; i < Istr.Length; i++)
            {
                OneStr = Istr.Substring(i, 1);
                switch (OneStr)
                {
                    case "+":
                        tmpstr += SplitStr;
                        break;
                    case "@":
                        tmpstr += "#";
                        break;
                    case "胜":
                        tmpstr += "1";
                        break;
                    case "负":
                        tmpstr += "2";
                        break;
                    case "元":
                        break;
                    default:
                        tmpstr += OneStr;
                        break;
                }
            }

            //电子票上的赔率明细字符串 如：2#4.700,1#1.080 排序输出为 1#1.080,2#4.700  使其与用户投注号码的排序相同
            tmpstr = SortRateText(CODE_SORT_DIECTIONRY, tmpstr);

            return tmpstr;
        }
        //号码排序字典 1 表示胜  2 表示负
        private const string CODE_SORT_DIECTIONRY = "1,2";
        #endregion
    }
}
