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

namespace GameFix.Poker
{
    public class CardStyle332Helper : CardStyleBaseHelper
    {
        public override int CardStyle => DataCardConst.CardStyle332;
        public override int CardCnt => 10;
        
        // 特殊牌型
        // 888999 7777
        // 666777888999 33334444
        internal override bool CheckStyleNormal(CardBaseGroup group, out CardStyleInfo cardStyleInfo)
        {
            cardStyleInfo = null;
            if (group.Cards.Count >= CardCnt && group.Cards.Count % 5 == 0)
            {
                if (IsMaxContinuous(group.CardGroupList, 3, out var shunValueList))
                {
                    // 检测剩下的是否都是都是一对
                    int doubleNum = 0;
                    for (int i = 0; i < group.CardGroupList.Count; i++)
                    {
                        var cnt = group.CardGroupList[i].Count;
                        if (shunValueList.Contains(i))
                        { // 超出部分可以组成对子
                            if (cnt <= 3) continue;
                            if (cnt % 2 == 0)
                            { // 拆了这个组合
                                doubleNum += cnt / 2;
                                shunValueList.Remove(i);
                            } else if ((cnt - 3) % 2 == 0)
                            {
                                doubleNum += (cnt - 3) / 2;
                            }
                            continue;
                        }
                        if (group.CardGroupList[i].Count % 2 == 0)
                            doubleNum += group.CardGroupList[i].Count / 2;
                    }

                    if (IsContinuous(shunValueList) 
                        && doubleNum == shunValueList.Count 
                        && shunValueList.Count * 5 == 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 % 5 == 0)
            {
                if (IsAllSame(group.NoLZCards)) return false; // 直接组成炸弹

                if (IsMaxContinuousByLZ(group.CardGroupList, 3, group.LZCards.Count,
                        out var shunValueList, out var lackValueList))
                {
                    // 判断剩下得牌是否都是一对
                    int doubleNum = 0;
                    int lzCount = group.LZCards.Count - lackValueList.Count;
                    for (int i = 0; i < group.CardGroupList.Count; i++)
                    {
                        int count = group.CardGroupList[i].Count;
                        if (shunValueList.Contains(i) || count == 0) continue;
                        if (count == 2 || ((2 - count) <= lzCount && (2 - count) > 0))
                        {
                            doubleNum++;
                            lzCount -= (2 - count);
                            shunValueList.AddRange(Enumerable.Repeat(i, (2 - count)));
                        } else if (count == 4 || ((4 - count) <= lzCount && (4 - count) > 0))
                        {
                            doubleNum += 2;
                            lzCount -= (4 - count);
                            shunValueList.AddRange(Enumerable.Repeat(i, (4 - count)));
                        }
                    }
                    
                    if (doubleNum == shunValueList.Count 
                        && shunValueList.Count * 5 == group.Cards.Count)
                    {
                        cardStyleInfo = new CardStyleInfo(CardStyle, shunValueList[0], shunValueList.Count * 5);
                        cardStyleInfo.CardLackValues = lackValueList.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 planeHelper = new CardStyle111222Helper();
            // 先找到所有的飞机
            CardStyleInfo tmpEatStyleInfo = null;
            if (eatStyleInfo != null) 
                tmpEatStyleInfo = new CardStyleInfo(
                    eatStyleInfo.CardStyle, 
                    eatStyleInfo.CardValue, 
                    eatStyleInfo.CardCnt / 5 * 3);
            var allPlaneList= planeHelper.GetAllEatTipGroup(group, tmpEatStyleInfo);
            List<Card> wingCards = new List<Card>();
            foreach (var plane in allPlaneList)
            {
                var doubleCnt = plane.Count / 3; // 需要对子得数量

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

                    // 将翅膀加入列表中
                    var needCnt = doubleCnt * 2 - wingCards.Count;
                    needCnt = needCnt > wingGroup.Count ? wingGroup.Count : needCnt;
                    needCnt = needCnt % 2 == 0 ? needCnt : needCnt - 1; // 不能为奇数
                    wingCards.AddRange(wingGroup.GetRange(0, needCnt));
                    if (wingCards.Count == doubleCnt * 2)
                    {
                        plane.AddRange(wingCards);
                        groupList.Add(new CardGroupList(plane));
                        break;
                    }
                }
            }

            return groupList;
        }

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