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

namespace GameFix.Poker
{
    /// <summary>
    /// 炸弹 （四张以上）
    /// </summary>
    public class CardStyleBombHelper : CardStyleBaseHelper
    {
        public override int CardStyle => DataCardConst.CardStyleBomb;
        public override int CardCnt => 4;

        internal override bool CheckStyleNormal(CardBaseGroup group, out CardStyleInfo cardStyleInfo)
        {
            cardStyleInfo = null;
            if (group.Cards.Count >= CardCnt && IsAllSame(group.Cards))
            {
                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) return false;
            if (group.LZCards.Count == group.Cards.Count && IsAllSame(group.LZCards))
            {
                cardStyleInfo = CardStyleHelper.CreateCardStyleInfo(CardStyle, group.LZCards);
                return true;
            }
            
            if (IsAllSame(group.NoLZCards))
            {
                var cardV = group.NoLZCards[0].Value;
                cardStyleInfo = new CardStyleInfo(CardStyle, cardV, group.Cards.Count);
                cardStyleInfo.CardLackValues = Enumerable.Repeat((int)cardV, group.LZCards.Count).ToArray();
                
                return true;
            }
            
            return false;
        }

        internal override List<CardGroupList> GetAllStyleCards(CardBaseGroup group, bool isSplitCard)
        {
            List<CardGroupList> ret = new List<CardGroupList>();
            var groupCnt = group.CardGroupList.Count;
            for (int i = 0; i < groupCnt; i++)
            {
                var cardGroup = group.CardGroupList[i];
                if (cardGroup.Count >= CardCnt)
                {
                    ret.Add(new CardGroupList(cardGroup));
                }
            }

            return ret;
        }

        internal override List<CardGroupList> GetAllEatTipGroup(CardBaseGroup group, CardStyleInfo eatStyleInfo)
        {
            var groupCnt = group.CardGroupList.Count;
            var groupList = new List<CardGroupList>(groupCnt);
            for (int i = 0; i < groupCnt; i++)
            {
                var cardGroup = group.CardGroupList[i];
                if (cardGroup.Count >= CardCnt)
                {
                    var cardStyleInfo = CardStyleHelper.CreateCardStyleInfo(CardStyle, cardGroup);
                    if (eatStyleInfo == null || group.mStyleComparer.Greater(cardStyleInfo, eatStyleInfo))
                    {
                        groupList.Add(new CardGroupList(cardGroup));
                    }
                }
            }

            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>();
            if (eatStyleInfo == null)
                eatStyleInfo = new CardStyleInfo(CardStyle, 0, CardCnt);
            foreach (var cardGroup in cardGroupList)
            {
                if (cardGroup.Count == 0 || IsJoker(cardGroup)) continue;
                var totalCnt = cardGroup.Count + group.LZCards.Count;
                if (totalCnt < CardCnt) continue;
                
                var tmpStyleInfo = new CardStyleInfo(CardStyle, cardGroup.First().Value, totalCnt);
                int needCnt = 0;
                if (group.mStyleComparer.Greater(tmpStyleInfo, eatStyleInfo))
                {
                    needCnt = tmpStyleInfo.CardCnt - cardGroup.Count;
                    List<Card> tmpList = new List<Card>();
                    tmpList.AddRange(cardGroup);
                    tmpList.AddRange(group.LZCards.GetRange(0, needCnt));
                    groupList.Add(new CardGroupList(tmpList));
                }
            }

            if (groupList.Count == 0)
            { // 癞子是否可以组成炸弹
                var tmpStyleInfo = new CardStyleInfo(CardStyle, group.LZCards.First().Value, group.LZCards.Count);
                if (group.mStyleComparer.Greater(tmpStyleInfo, eatStyleInfo))
                    groupList.Add(new CardGroupList(group.LZCards));
            }

            return groupList;
        }
    }
}