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

namespace GameFix.Poker
{
    /// <summary>
    /// 手牌组相关逻辑
    /// </summary>
    public class CardGroup : CardBaseGroup
    {
        public CardGroup(IEnumerable<Card> cards, Deck deck) : base(cards, deck)
        {

        }

        /// <summary>
        /// 拆解牌组
        /// </summary>
        public List<CardGroupList> Decompose()
        {
            List<CardGroupList> ret = new List<CardGroupList>();
            var cardGroup = mDeck.CreateCardGroup(mCards);
            foreach (var helper in mCardStyleBaseHelpers)
            {
                var styleCards = helper.GetAllStyleCards(cardGroup, false);
                ret.AddRange(styleCards);
                cardGroup.RemoveCards(styleCards);
            }

            if (cardGroup.mCards.Count > 0)
            { // 还有未知牌型没有拆解完？ （理论不存在）
                ret.Add(new CardGroupList(cardGroup.mCards));
            }
            
            return ret;
        }
        
        
        /// <summary>
        /// 获取吃牌提示
        /// </summary>
        public List<CardGroupList> GetEatTip(CardStyleInfo targetStyle)
        {
            List<CardGroupList> ret = new List<CardGroupList>();
            if (targetStyle == null)
            {
                // 没有提供对方牌型，先判断自己手牌是否符合一种牌型可以一次性出完
                var styleInfo = GetCardStyleInfo();
                bool isCanOutImmediate = false;
                if (styleInfo.CardStyle !=  DataCardConst.CardStyle0)
                {
                    var cardGroupList = new CardGroupList(mCards);
                    cardGroupList.CardStyleInfo = styleInfo;
                    ret.Add(cardGroupList);
                    isCanOutImmediate = true;
                }

                if (!isCanOutImmediate)
                {
                    foreach (var helper in mCardStyleBaseHelpers)
                    {
                        ret.AddRange(helper.GetAllEatTipGroup(this, null));
                        if (ret.Count > 0)
                            break;
                    }
                }
            }
            else
            {
                foreach (var helper in mCardStyleBaseHelpers)
                {
                    if (mStyleComparer.Greater(helper.CardStyle, targetStyle.CardStyle))
                    {
                        ret.AddRange(helper.GetAllEatTipGroup(this, targetStyle));
                    }
                }

                // 没找到再尝试有用癞子去匹配
                if (ret.Count == 0 && mLZCards.Count > 0)
                {
                    foreach (var helper in mCardStyleBaseHelpers)
                    {
                        if (mStyleComparer.Greater(helper.CardStyle, targetStyle.CardStyle))
                        {
                            ret.AddRange(helper.GetAllEatTipGroupWithLZ(this, targetStyle));
                        }
                    }
                }
            }

            // 牌类型赋值
            foreach (var cardList in ret)
            {
                if (cardList.CardStyleInfo != null) continue;
                var group = mDeck.CreateCardGroup(cardList);
                cardList.CardStyleInfo = group.GetCardStyleInfo();
            }

            return ret;
        }

        public List<CardGroupList> GetShunTip(CardStyleInfo targetStyle, byte[] tipHelpers)
        {
            List<CardGroupList> ret = new List<CardGroupList>();
            for (int i = 0; i < tipHelpers.Length; i++)
            {
                var helper = GetHelper(tipHelpers[i]);
                var tipCardList = helper?.GetAllEatTipGroup(this, targetStyle);
                if (tipCardList != null && tipCardList.Count > 0)
                {
                    ret.AddRange(tipCardList.OrderByDescending(x=>x.Count));
                    return ret;
                }
            }
            
            return ret;
        }

        public List<CardGroupList> GetEatTip(int styleId)
        {
            List<CardGroupList> ret = new List<CardGroupList>();
            var helper = GetHelper(styleId);
            if (helper != null)
            {
                ret.AddRange(helper.GetAllEatTipGroup(this, null));

                if (ret.Count == 0 && mLZCards.Count > 0)
                {
                    ret.AddRange(helper.GetAllEatTipGroupWithLZ(this, null));
                }
            }

            return ret;
        }
        
        /// <summary>
        /// 获取当前牌组得牌型
        /// </summary>
        public CardStyleInfo GetCardStyleInfo()
        {
            if (this.mLZCards.Count > 0)
            {
                foreach (var helper in mCardStyleBaseHelpers)
                {
                    if (helper.CheckStyleWithLZ(this, out var chekStyleInfo))
                    {
                        return chekStyleInfo;
                    }
                }
            }
            else
            {
                foreach (var helper in mCardStyleBaseHelpers)
                {
                    if (helper.CheckStyleNormal(this, out var chekStyleInfo))
                    {
                        return chekStyleInfo;
                    }
                }
            }
            CardStyleInfo cardStyleInfo = new CardStyleInfo();
            cardStyleInfo.CardStyle = DataCardConst.CardStyle0;   
            return cardStyleInfo;
        }
        
        /// <summary>
        /// 获取当前牌组得所有符合条件牌型
        /// </summary>
        public List<CardStyleInfo> GetCardStyleInfos()
        {
            List<CardStyleInfo> cardStyleInfos = new List<CardStyleInfo>();
            if (this.mLZCards.Count > 0)
            {
                foreach (var helper in mCardStyleBaseHelpers)
                {
                    if (helper.CheckStyleWithLZ(this, out var chekStyleInfo))
                    {
                        cardStyleInfos.Add(chekStyleInfo);
                    }
                }
            }
            else
            {
                foreach (var helper in mCardStyleBaseHelpers)
                {
                    if (helper.CheckStyleNormal(this, out var chekStyleInfo))
                    {
                        cardStyleInfos.Add(chekStyleInfo);
                    }
                }
            }
            return cardStyleInfos;
        }

        public bool CheckCardStyleInfoValidate(CardStyleInfo cardStyleInfo, out CardStyleInfo chkStyleInfo)
        {
            chkStyleInfo = new CardStyleInfo();
            foreach (var helper in mCardStyleBaseHelpers)
            {
                if (helper.CardStyle == cardStyleInfo.CardStyle)
                {
                    // 先检测是否符合普通牌型
                    if (helper.CheckStyleNormal(this, out chkStyleInfo))
                    {
                        return true;
                    }

                    if (this.mLZCards.Count > 0)
                    {
                        return helper.CheckStyleWithLZ(this, out chkStyleInfo);
                    }
                }
            }

            return false;
        }
    }
}