using System.Collections.Generic;
using GameMessage;

namespace GameFix.Poker
{
    /// <summary>
    /// 飞机 带 4张
    /// 最后一手牌可以少带或者不带
    /// </summary>
    public class CardStyle3311SpecialHelper : CardStyleBaseHelper
    {
        public override int CardStyle => DataCardConst.CardStyle3311;
        public override int CardCnt => 10;
        private readonly CardStyle3311Helper _cardStyle3311 = new();
        
        internal override bool CheckStyleNormal(CardBaseGroup group, out CardStyleInfo cardStyleInfo)
        {
            cardStyleInfo = null;
            // 手牌对象才检测
            if (group is not CardHandGroup) return false;
            
            // 只要是飞机就可以带剩下得牌
            // 先过滤是否看有三个得牌
            var cardList = new List<CardGroupList>();
            foreach (var cardGroup in group.SelfCardGroupDic)
            {
                if (cardGroup.Key >= 3)
                    cardList.AddRange(cardGroup.Value);
            }
            if (cardList.Count >= 2)
            {
                if (group.SelfCards.Count == group.Cards.Count)
                {
                    // 剩下得牌都出完
                    if (IsMaxContinuous(group.CardGroupList, 3, out var shunValueList) 
                        && shunValueList.Count >= 2
                        && shunValueList.Count * 5 >= group.Cards.Count)
                    {
                        cardStyleInfo = new CardStyleInfo(CardStyle, shunValueList[0], group.Cards.Count);
                        return true;
                    }
                    return false;
                }
                
                return _cardStyle3311.CheckStyleNormal(group, out cardStyleInfo);
            }

            return false;
        }

        internal override List<CardGroupList> GetAllEatTipGroup(CardBaseGroup group, CardStyleInfo eatStyleInfo)
        {
            // 一定要检测手牌
            var ret = new List<CardGroupList>();
            if (group is not CardHandGroup handGroup) return ret;
            if (group.SelfCardGroupDic.TryGetValue(3, out var cardList)
                && cardList.Count >= 2)
            {
                // 手上得牌是否可以成为飞机
                var cardStyle = handGroup.GetCardStyleInfo(group.SelfCards);
                if (cardStyle.CardStyle == CardStyle)
                {
                    ret.Add(new CardGroupList(group.SelfCards));
                    return ret;
                }
                return _cardStyle3311.GetAllEatTipGroup(group, eatStyleInfo);
            }
            return ret;
        }
    }
}