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

namespace GameFix.Poker
{
    public class CardStyle331Helper : CardStyleBaseHelper
    {
        public override int CardStyle => DataCardConst.CardStyle331;
        public override int CardCnt => 8;

        protected virtual int GroupCnt => 4;

        // 特殊情况
        // 333444 666777888999101010JJJ  (理论不存在)
        internal override bool CheckStyleNormal(CardBaseGroup group, out CardStyleInfo cardStyleInfo)
        {
            cardStyleInfo = null;
            if (group.Cards.Count >= CardCnt && group.Cards.Count % GroupCnt == 0)
            {
                if (IsMaxContinuous(group.CardGroupList, 3, out var shunValueList) 
                    && shunValueList.Count * GroupCnt == group.Cards.Count)
                {
                    cardStyleInfo = new CardStyleInfo(CardStyle, shunValueList[0], group.Cards.Count);
                    return true;
                }
            }
            return false;
        }

        internal override bool CheckStyleWithLZ(CardBaseGroup group, out CardStyleInfo cardStyleInfo)
        {
            cardStyleInfo = null;
            if (group.Cards.Count >= CardCnt && group.Cards.Count % GroupCnt == 0)
            {
                if (IsAllSame(group.NoLZCards)) return false; // 直接组成炸弹

                if (IsMaxContinuousByLZ(group.CardGroupList, 3, group.LZCards.Count,
                        out var shunValueList, out var lackValueList))
                {
                    if (shunValueList.Count * GroupCnt == group.Cards.Count)
                    {
                        cardStyleInfo = new CardStyleInfo(CardStyle, shunValueList[0], shunValueList.Count * 4);
                        cardStyleInfo.CardLackValues = lackValueList.ToArray();

                        return true;
                    }
                }
                
            }
            
            return false;
        }

        // 333444555 888
        internal override List<CardGroupList> GetAllEatTipGroup(CardBaseGroup group, CardStyleInfo eatStyleInfo)
        {
            var groupCnt = group.CardGroupList.Count;
            var groupList = new List<CardGroupList>(groupCnt);
            
            var planeHelper = new CardStyle111222Helper();
            // 先找到所有的飞机
            CardStyleInfo tmpEatStyleInfo = null;
            if (eatStyleInfo != null) 
                tmpEatStyleInfo = new CardStyleInfo(
                    eatStyleInfo.CardStyle, 
                    eatStyleInfo.CardValue, 
                    eatStyleInfo.CardCnt / GroupCnt * 3);
            var allPlaneList = planeHelper.GetAllEatTipGroup(group, tmpEatStyleInfo);
            List<Card> wingCards = new List<Card>();
            foreach (var plane in allPlaneList)
            {
                var singleCnt = (plane.Count / 3) * (GroupCnt - 3); // 需要单牌得数量

                // 找到符合条件得所有翅膀
                var wingGroupList = group.CardGroupDic
                    .Where(kv => kv.Key >= 1).SelectMany(kv=>kv.Value).ToList();
                wingCards.Clear();
                foreach (var wingGroup in wingGroupList)
                {
                    if (wingGroup.Count >= 3 && plane.Intersect(wingGroup).Any()) // 翅膀不应该来源于飞机
                        continue;

                    // 将翅膀加入列表中
                    var needCnt = singleCnt - wingCards.Count;
                    needCnt = needCnt > wingGroup.Count ? wingGroup.Count : needCnt;
                    wingCards.AddRange(wingGroup.GetRange(0, needCnt));
                    if (wingCards.Count == singleCnt)
                    {
                        plane.AddRange(wingCards);
                        groupList.Add(new CardGroupList(plane));
                        break;
                    }
                }
            }

            return groupList;
        }

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