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

namespace GameFix.Poker
{
    public class CardStyle2Helper : CardStyleBaseHelper
    {
        public override int CardStyle => DataCardConst.CardStyle2;
        public override int CardCnt => 2;

        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 (IsContainJoker(group.NoLZCards)) 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, CardCnt);
                cardStyleInfo.CardLackValues = Enumerable.Repeat((int)cardV, 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);
            for (int i = 0; i < groupCnt; i++)
            {
                var cardGroup = group.CardGroupList[i];
                if (cardGroup.Count >= CardCnt && cardGroup.Count < 4 && IsSameGroupGreater(cardGroup, eatStyleInfo))
                {
                    groupList.Add(new CardGroupList(cardGroup));
                }
            }
            
            // 从两张牌到三张排序，优先不拆组合牌
            if (groupList.Count > 0)
            {
                groupList = groupList.OrderBy(x=>x.Count).ToList();
                groupList.ForEach(x => x.RemoveRange(2, x.Count - 2));
            }

            return groupList;
        }

        internal override List<CardGroupList> GetAllEatTipGroupWithLZ(CardBaseGroup group, CardStyleInfo eatStyleInfo)
        {
            var cardGroupList = group.CardGroupDic.Where(kv => kv.Key >= 1).SelectMany(kv => kv.Value).ToList();
            var groupList = new List<CardGroupList>();
            foreach (var cardGroup in cardGroupList)
            {
                if (cardGroup.Count > 0 
                    && cardGroup.First().Value > eatStyleInfo.CardValue
                    && !IsJoker(cardGroup))
                {
                    List<Card> tmpList = new List<Card>();
                    tmpList.Add(cardGroup.First());
                    tmpList.Add(group.LZCards.First());
                    groupList.Add(new CardGroupList(tmpList));
                }
            }
            
            if (groupList.Count == 0 
                && group.LZCards.Count >= CardCnt 
                && IsAllSame(group.LZCards)
                && group.LZCards.First().Value > eatStyleInfo.CardValue)
            {
                var cardList = group.LZCards.GetRange(0, CardCnt);
                groupList.Add(new CardGroupList(cardList));
            }

            return groupList;
        }
    }
}