using System.Collections.Generic;
using System.Linq;
using GameMessage;

namespace GameFix.Poker
{
    public class CardStyle111222Helper : CardStyleBaseHelper
    {
        public override int CardStyle => DataCardConst.CardStyle111222;
        public override int CardCnt => 6;
        
        internal override bool CheckStyleNormal(CardBaseGroup group, out CardStyleInfo cardStyleInfo)
        {
            cardStyleInfo = null;
            if (group.Cards.Count >= CardCnt && group.Cards.Count % 3 == 0)
            {
                if (IsContinuous(group.CardGroupList, 3, out var shunCnt) 
                    && shunCnt.Count * 3 == group.Cards.Count)
                {
                    cardStyleInfo = CardStyleHelper.CreateCardStyleInfo(CardStyle, group.Cards);
                    return true;
                }
            }
            return false;
        }

        internal override bool CheckStyleWithLZ(CardBaseGroup group, out CardStyleInfo cardStyleInfo)
        {
            cardStyleInfo = null;
            if (group.Cards.Count >= CardCnt && group.Cards.Count % 3 == 0)
            {
                if (IsAllSame(group.NoLZCards)) return false;

                if (IsMaxContinuousByLZ(group.CardGroupList, 3, group.LZCards.Count,
                        out var shunValueList, out var lackValueList))
                {
                    if (shunValueList.Count * 3 == group.Cards.Count)
                    {
                        cardStyleInfo = new CardStyleInfo(CardStyle, shunValueList[0], shunValueList.Count * 3);
                        cardStyleInfo.CardLackValues = lackValueList.ToArray();
                        return true;
                    }
                }

            }
            
            return false;
        }

        internal override List<CardGroupList> GetAllEatTipGroup(CardBaseGroup group, CardStyleInfo eatStyleInfo)
        {
            var groupCnt = group.CardGroupList.Count;
            var groupList = new List<CardGroupList>(groupCnt);

            var minValue = eatStyleInfo != null ? eatStyleInfo.CardValue + 1 : 3;
            var continuousCnt = eatStyleInfo?.CardCnt ?? -1;
            List<Card> continuousList = new List<Card>();
            for (int i = minValue; i < group.CardGroupList.Count; i++)
            {
                continuousList.Clear();
                for (int j = 0; j < group.CardGroupList.Count; j++)
                {
                    if (i + j >= group.CardGroupList.Count)
                        break;
                    if (group.CardGroupList[i + j].Count <= 2)
                        break;
                    
                    continuousList.AddRange(group.CardGroupList[i + j].GetRange(0, 3));

                    if (continuousList.Count >= CardCnt && continuousList.Count % 3 == 0)
                    {
                        if (continuousCnt == -1 || continuousCnt == continuousList.Count)
                        {
                            groupList.Add(new CardGroupList(continuousList));
                        }
                    }
                }
            }

            return groupList;
        }

        internal override List<CardGroupList> GetAllEatTipGroupWithLZ(CardBaseGroup group, CardStyleInfo eatStyleInfo)
        {// 这里不处理直接找炸弹
            return new List<CardGroupList>();
        }
    }
}