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

namespace GameFix.Poker
{
    public static class CardFunc
    {
        /// <summary>
        /// 获取牌值分数 (5 10 K)
        /// </summary>
        public static int GetCard510KScore(List<Card> cards)
        {
            int score = 0;
            for (int i = 0; i < cards.Count; i++)
            {
                if (cards[i].Value == DataCardConst.CardValue5)
                    score += 5;
                if (cards[i].Value == DataCardConst.CardValue10)
                    score += 10;
                if (cards[i].Value == DataCardConst.CardValueK)
                    score += 10;
            }

            return score;
        }
        
        /// <summary>
        /// 获取cards 中所有510k的集合
        /// </summary>
        public static List<List<Card>> Get510KList(List<Card> cards, int lessNumber)
        {
            var result = new List<List<Card>>();
            
            var valueCardDict = cards.GroupBy(x => x.Value)
                .ToDictionary(g => g.Key, g => g.ToList());
            
            valueCardDict.TryGetValue(DataCardConst.CardValue5, out var cards5);
            valueCardDict.TryGetValue(DataCardConst.CardValue10, out var cards10);
            valueCardDict.TryGetValue(DataCardConst.CardValueK, out var cardsK);

            if (cards5?.Count >= lessNumber && cards10?.Count >= lessNumber && cardsK?.Count >= lessNumber)
            {
                var min = Math.Min(Math.Min(cards5.Count, cards10.Count), cardsK.Count);
                
                for (int i = 0; i < min; i++)
                {
                    var list = new List<Card>
                    {
                        cards5[i],
                        cards10[i],
                        cardsK[i]
                    };
                    result.Add(list);
                }
            }

            return result;
        }

        public static int GetJokerCount(List<Card> cards)
        {
            return cards.Count(DataCardConst.IsJoker);
        }
        
        public static bool IsAllJoker(List<Card> cards)
        {
            if (cards.Count == 0) return false;
            if (cards.Count == 1) return true;
            
            for (int i = 1; i < cards.Count; i++)
            {
                if (!DataCardConst.IsJoker(cards[i]))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// 获取最大得同色数量
        /// </summary>
        /// <param name="cards"></param>
        /// <returns></returns>
        public static int GetMaxSameColorCount(List<Card> cards)
        {
            return cards.Select(x => GetSuitColorType(x.Suit))
                .GroupBy(x => x).Select(g => g.Count()).DefaultIfEmpty(0).Max();
        }

        /// <summary>
        /// 获取最大同牌值数量
        /// </summary>
        public static int GetMaxSameValueCount(List<Card> cards)
        {
            return cards.Select(x => x.Value)
                .GroupBy(x => x).Select(g => g.Count()).DefaultIfEmpty(0).Max();
        }

        /// <summary>
        /// 所有颜色是否相同 （红色/黑色）
        /// </summary>
        public static bool IsAllColorSame(List<Card> cards)
        {
            if (cards.Count == 0) return false;
            if (cards.Count == 1) return true;
            
            for (int i = 1; i < cards.Count; i++)
            {
                if (GetSuitColorType(cards[i].Suit) != GetSuitColorType(cards[i-1].Suit))
                {
                    return false;
                }
            }

            return true;
        }

        public static int GetSuitColorType(int suit)
        {
            if (suit == DataCardConst.SuitSpade || suit == DataCardConst.SuitClub)
                return 1;
            if (suit == DataCardConst.SuitHeart || suit == DataCardConst.SuitDiamond)
                return 2;
            return 0;
        }
        
        /// <summary>
        /// 所有花色是否相同
        /// </summary>
        public static bool IsAllSuitSame(List<Card> cards)
        {
            if (cards.Count == 0) return false;
            if (cards.Count == 1) return true;
            
            for (int i = 1; i < cards.Count; i++)
            {
                if (cards[i].Suit != cards[i-1].Suit)
                {
                    return false;
                }
            }

            return true;
        }
        
        /// <summary>
        /// 所有牌值是否相同
        /// </summary>
        public static bool IsAllValueSame(List<Card> cards)
        {
            if (cards.Count == 0) return false;
            if (cards.Count == 1) return true;
            
            for (int i = 1; i < cards.Count; i++)
            {
                if (cards[i].Value != cards[i-1].Value)
                {
                    return false;
                }
            }

            return true; 
        }
    }
}