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

namespace GameFix.Poker
{
    public class CardStyle31Helper : CardStyleBaseHelper
    {
        public override int CardStyle => DataCardConst.CardStyle31;
        public override int CardCnt => 4;

        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 == 3)
                    {
                        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 maxCV = 0;
                for (int i = 0; i < group.CardGroupList.Count; i++)
                {
                    if (group.CardGroupList[i].Count + group.LZCards.Count == 3 && i>maxCV)
                    {
                        maxCV = i;
                    }
                }

                if (maxCV != 0)
                {
                    cardStyleInfo = new CardStyleInfo(CardStyle, maxCV, CardCnt);
                    cardStyleInfo.CardLackValues = Enumerable.Repeat(maxCV, group.LZCards.Count).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 sortGroupList = group.CardGroupList.OrderBy((x) => x.Count).ToList();
            for (int i = 0; i < groupCnt; i++)
            {
                var cardGroup = group.CardGroupList[i];
                if (cardGroup.Count == 3 && IsSameGroupGreater(cardGroup, eatStyleInfo))
                {
                    // 从其他组中找牌
                    var newCardGroup = new CardGroupList(cardGroup);
                    for (int j = 0; j < groupCnt; j++)
                    {
                        if (sortGroupList[j] != cardGroup && sortGroupList[j].Count >= 1)
                        {
                            int needCnt = CardCnt - newCardGroup.Count;
                            needCnt = needCnt > sortGroupList[j].Count ? sortGroupList[j].Count : needCnt;
                            newCardGroup.AddRange(sortGroupList[j].GetRange(0, needCnt));

                            if (newCardGroup.Count == CardCnt)
                            {
                                groupList.Add(newCardGroup);
                                break;
                            }
                        }
                    }
                }
            }

            return groupList;
        }

        internal override List<CardGroupList> GetAllEatTipGroupWithLZ(CardBaseGroup group, CardStyleInfo eatStyleInfo)
        {
            var cardGroupList = group.CardGroupDic.Where(kv => kv.Key >= 1)
                .SelectMany(kv => kv.Value).OrderByDescending(x=>x.Count).ToList();
            var groupList = new List<CardGroupList>();
            foreach (var cardGroup in cardGroupList)
            {
                if (cardGroup.Count > 0 
                    && cardGroup.First().Value > eatStyleInfo.CardValue
                    && !IsJoker(cardGroup))
                {
                    var needCnt = CardCnt - cardGroup.Count - 1;
                    if (needCnt <= group.LZCards.Count)
                    {
                        List<Card> tmpList = new List<Card>();
                        tmpList.AddRange(cardGroup);
                        tmpList.AddRange(group.LZCards.GetRange(0, needCnt));
                        for (int i = cardGroupList.Count - 1; i >= 0; i--)
                        {
                            if (cardGroupList[i] != cardGroup)
                            {
                                int wingNeedCnt = CardCnt - tmpList.Count;
                                wingNeedCnt = wingNeedCnt > cardGroupList[i].Count ? cardGroupList[i].Count : wingNeedCnt;
                                tmpList.AddRange(cardGroupList[i].GetRange(0, wingNeedCnt));
                                break;
                            }
                        }

                        if (tmpList.Count == CardCnt)
                        {
                            groupList.Add(new CardGroupList(tmpList));
                        }
                    }
                }
            }

            return groupList;
        }
    }
}