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

namespace GameFix.Poker
{
    public class CardStyle42Helper : CardStyleBaseHelper
    {
        public override int CardStyle => DataCardConst.CardStyle42;
        public override int CardCnt => 8;
        
        internal override bool CheckStyleNormal(CardBaseGroup group, out CardStyleInfo cardStyleInfo)
        {
            cardStyleInfo = null;
            if (group.Cards.Count == CardCnt)
            {
                int mainValue = 0, withValue = 0, withNum = 0;
                for (int i = 0; i < group.CardGroupList.Count; i++)
                {
                    if (group.CardGroupList[i].Count == 4)
                    {
                        mainValue = i;
                    }
                    
                    if (group.CardGroupList[i].Count == 2)
                    {
                        withValue = i;
                        withNum++;
                    }
                }

                if (mainValue > 0 && withNum == 2)
                {
                    cardStyleInfo = new CardStyleInfo(CardStyle, mainValue, group.Cards.Count);
                    return true;
                }
            }
            return false;
        }

        internal override bool CheckStyleWithLZ(CardBaseGroup group, out CardStyleInfo cardStyleInfo)
        {
            cardStyleInfo = null;
            if (group.Cards.Count == CardCnt)
            {
                if (IsAllSame(group.NoLZCards)) return false; // 直接组成炸弹
                
                int lzCount = group.LZCards.Count;
                int fourCV = 0;
                int pairCnt = 0;
                List<int> lackValues = new List<int>(group.LZCards.Count);
                for (int i = group.CardGroupList.Count - 1; i >= 0; i--)
                {
                    int cardV = i;
                    int count = group.CardGroupList[i].Count;
                    int needCnt = 0;
                    if ((4 - count) <= lzCount)
                    {
                        // 找到4张相同的牌
                        needCnt = 4 - count;
                        fourCV = cardV;
                    } else if ((2 - count) <= lzCount && (2 - count) >= 0)
                    {
                        // 两对
                        needCnt = 2 - count;
                        pairCnt++;
                    }
                    lzCount -= needCnt;
                    lackValues.AddRange(Enumerable.Repeat(cardV, needCnt));
                }

                if (fourCV != 0 && lzCount == 0 && pairCnt == 2)
                {
                    cardStyleInfo = new CardStyleInfo(CardStyle, fourCV, CardCnt);
                    cardStyleInfo.CardLackValues = lackValues.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);
            if (!group.CardGroupDic.ContainsKey(4)) return groupList;
            //所有得四张牌组
            foreach (var fourCardGroup in group.CardGroupDic[4])
            {
                if (!IsSameGroupGreater(fourCardGroup, eatStyleInfo)) continue;
                
                // 找两个单张 （不拆牌）
                if (group.CardGroupDic.ContainsKey(2) && group.CardGroupDic[2].Count >= 2)
                {
                    var cardGroup42 = new CardGroupList(fourCardGroup);
                    cardGroup42.AddRange(group.CardGroupDic[2][0]);
                    cardGroup42.AddRange(group.CardGroupDic[2][1]);
                    groupList.Add(cardGroup42);
                }
            }

            return groupList;
        }

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