using System.Collections.Generic;
using GameMessage;

namespace GameFix.Poker
{
    // 默认降序比较器
    public class CardDefaultComparer :IComparer<Card>
    {
        /// <summary>
        /// 是否升序
        /// </summary>
        bool mIsAscending;
        public CardDefaultComparer(bool isAscending = false)
        {
            mIsAscending = isAscending;
        }
        
        public int Compare(Card x, Card y)
        {
            if (x == null && y == null) return 0;
            if (x == null) return 1;    // 空值放后面
            if (y == null) return -1;
            if (x.CardType == DataCardConst.CardTypeLZ && y.CardType != DataCardConst.CardTypeLZ) return -1;
            if (y.CardType == DataCardConst.CardTypeLZ && x.CardType != DataCardConst.CardTypeLZ) return 1;

            return mIsAscending ? -y.SortNum.CompareTo(x.SortNum) : y.SortNum.CompareTo(x.SortNum);
        }
    }

    public class CardStyleComparer : IComparer<Card>
    {
        private CardStyleInfo mCardStyleInfo;
        public CardStyleComparer(CardStyleInfo cardStyleInfo)
        {
            mCardStyleInfo = cardStyleInfo;
        }
        
        public int Compare(Card x, Card y)
        {
            if (x == null && y == null) return 0;
            if (x == null) return 1;    // 空值放后面
            if (y == null) return -1;
            if (x.Value < mCardStyleInfo.CardValue && y.Value >= mCardStyleInfo.CardValue) return 1;
            if (x.Value >= mCardStyleInfo.CardValue && y.Value < mCardStyleInfo.CardValue) return 1;
            
            return x.SortNum.CompareTo(y.SortNum);
        }
    }

    public class CardIdComparer : IComparer<Card>
    {
        public int Compare(Card x, Card y)
        {
            if (ReferenceEquals(x, y)) return 0;
            if (y is null) return 1;
            if (x is null) return -1;
            return x.Id.CompareTo(y.Id);
        }
    }
    
    /// <summary>
    /// 相同牌值放一起
    /// </summary>
    public class CardColorSuitComparer : IComparer<Card>
    {
        private List<int> mColorSuitList = new List<int>()
            { DataCardConst.SuitSpade, DataCardConst.SuitClub, DataCardConst.SuitHeart, DataCardConst.SuitDiamond };

        public int Compare(Card x, Card y)
        {
            if (ReferenceEquals(x, y)) return 0;
            if (y is null) return 1;
            if (x is null) return -1;

            // 先按牌值排序（降序）
            int result = y.Value.CompareTo(x.Value);
            if (result != 0) return result;

            // 相同牌值按花色排序
            int xIndex = mColorSuitList.IndexOf(x.Suit);
            int yIndex = mColorSuitList.IndexOf(y.Suit);
            if (xIndex == -1) xIndex = int.MaxValue;
            if (yIndex == -1) yIndex = int.MaxValue;

            result = xIndex.CompareTo(yIndex);
            return result != 0 ? result : y.SortNum.CompareTo(x.SortNum);
        }
    }

    public class CardValueEqualityComparer : IEqualityComparer<Card>
    {
        public bool Equals(Card x, Card y)
        {
            if (ReferenceEquals(x, y)) return true;
            if (x is null) return false;
            if (y is null) return false;
            if (x.GetType() != y.GetType()) return false;
            return x.Value == y.Value && x.Suit == y.Suit;
        }

        public int GetHashCode(Card obj)
        {
            unchecked
            {
                var hashCode = obj.Value.GetHashCode();
                hashCode = (hashCode * 397) ^ obj.Suit.GetHashCode();
                return hashCode;
            }
        }
    }
}