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

namespace GameFix.Poker
{
    public class CardStyle41Helper : CardStyleBaseHelper
    {
        public override int CardStyle => DataCardConst.CardStyle41;
        public override int CardCnt => 6;
        
        internal override bool CheckStyleNormal(CardBaseGroup group, out CardStyleInfo cardStyleInfo)
        {
            cardStyleInfo = null;
            if (group.Cards.Count == CardCnt)
            {
                for (int i = 0; i < group.CardGroupList.Count; i++)
                {
                    if (group.CardGroupList[i].Count == 4)
                    {
                        cardStyleInfo = new CardStyleInfo(CardStyle, i, 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;
                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;
                    }
                    lzCount -= needCnt;
                    lackValues.AddRange(Enumerable.Repeat(cardV, needCnt));
                }

                if (lzCount > 0)
                {
                    // 可能还会剩下癞子 （剩下得癞子当成普通牌） 
                    // 也可以处理成替代剩下得牌
                    lzCount = 0;
                }

                if (fourCV != 0 && lzCount == 0)
                {
                    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(1) && group.CardGroupDic[1].Count >= 2)
                {
                    var cardGroup42 = new CardGroupList(fourCardGroup);
                    cardGroup42.AddRange(group.CardGroupDic[1][0]);
                    cardGroup42.AddRange(group.CardGroupDic[1][1]);
                    groupList.Add(cardGroup42);
                } else if (group.CardGroupDic.ContainsKey(2) && group.CardGroupDic[2].Count >= 1)
                {
                    var cardGroup42 = new CardGroupList(fourCardGroup);
                    cardGroup42.AddRange(group.CardGroupDic[2][0]);
                    groupList.Add(cardGroup42);
                }
            }

            return groupList;
        }

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