using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using MessagePack;

namespace GameMessage.DCTaoShang
{
    [MessagePackObject]
    public struct DaZhaCardPack
    {
        /// <summary>
        /// 4个玩家手牌
        /// </summary>
        [Key(0)]
        public PlayCard[] PlayCard;
        public void Init()
        {
            PlayCard = new PlayCard[4];
        }
    }
    
    [MessagePackObject]
    public struct PlayCard
    {
        /// <summary>
        /// 1个玩家的手牌
        /// </summary>
        [Key(0)]
        public TCardInfo[] CardInfos;
        /// <summary>
        /// 初始化玩家牌
        /// </summary>
        public void Init(int len)
        {
            CardInfos = new TCardInfo[len];
        }
    }
    
    [MessagePackObject]
    public struct TCardInfo : ICardInfoType1
    {
        /// <summary>
        /// 绝对ID
        /// </summary>
        [Key(0)]
        public byte ID { get; set; }

        /// <summary>
        /// 牌面花色 1黑桃 2红桃 3梅花 4方块 5大小王
        /// </summary>
        [Key(1)] public byte Flower;

        /// <summary>
        /// 牌面大小
        /// 1-13 黑桃A-K
        /// 14-26 红桃A-K
        /// 27-39 梅花A-K
        /// 40-52 方块A-K
        /// 53小王 54大王
        /// </summary>
        [Key(2)] public byte Num;

        /// <summary>
        /// 游戏里大小 3-14（3~A） 16是2  98小王 100大王
        /// </summary>
        [Key(3)]
        public byte GameNum { get; set; }

        /// <summary>
        /// 游戏分值
        /// </summary>
        [Key(4)] public byte Score;

        /// <summary>
        /// 当前在谁手里 1-4
        /// </summary>
        [Key(5)] public byte GameOwer;

        /// <summary>
        /// 状态 0打下去了 1没有打下去
        /// </summary>
        [Key(6)] public byte GameState;

        public override string ToString()
        {
            return
                $"ID:{ID} 花色:{(Flower == 1 ? "黑桃" : Flower == 2 ? "红桃" : Flower == 3 ? "梅花" : Flower == 4 ? "方块" : "王")} 大小:{Num}_{GameNum} 分:{Score} 状态:{(GameState == 1 ? "在手上" : "打下去了")}";
        }

        public bool IsActive()
        {
            return ID > 0 && Flower > 0 && GameNum > 0;
        }

        public override bool Equals(object obj)
        {
            if (!(obj is TCardInfo))
            {
                return false;
            }

            TCardInfo other = (TCardInfo)obj;
            return /*ID == other.ID &&*/
                Flower == other.Flower &&
                Num == other.Num &&
                GameNum == other.GameNum &&
                Score == other.Score &&
                GameOwer == other.GameOwer &&
                GameState == other.GameState;
        }

        public override int GetHashCode()
        {
            int hash = 17;
            //hash = hash * 23 + ID.GetHashCode();
            hash = hash * 22 + Flower.GetHashCode();
            hash = hash * 33 + Num.GetHashCode();
            hash = hash * 24 + GameNum.GetHashCode();
            hash = hash * 11 + Score.GetHashCode();
            hash = hash * 12 + GameOwer.GetHashCode();
            hash = hash * 15 + GameState.GetHashCode();
            return hash;
        }
    }
    
    [MessagePackObject]
    public struct TaoShangGamePack
    {
        /// <summary>
        /// 逻辑内存包
        /// </summary>
        [Key(0)]
        public TaoShangLogicInfo Info;

        /// <summary>
        /// 牌的内存包
        /// </summary>
        [Key(1)]
        public DaZhaCardPack CardPack;

        /// <summary>
        /// 规则
        /// </summary>
        [Key(2)]
        public Rule Rule;

        public void Init()
        {
            Info.Init();
            CardPack.Init();
        }
    }

    [MessagePackObject]
    public struct TaoShangLogicInfo
    {
        /// <summary>
        /// 游戏流程 1游戏开始 2游戏发牌 3打牌 4结算
        /// </summary>
        [Key(0)]
        public byte GameZT;
        /// <summary>
        /// 控制权是哪个位置1-4
        /// </summary>
        [Key(1)]
        public byte WhoPlay;

        /// <summary>
        /// 最大的出牌内容
        /// </summary>
        [Key(2)]
        public PlayOutCard MaxPlayCard;

        /// <summary>
        /// 当前出牌内容
        /// 牌型有单张、对子、连对（3对以上）、三张（不带牌）、顺子、炸弹
        /// </summary>
        [Key(3)]
        public PlayOutCard ActivePlayCard;

        /// <summary>
        /// 第一个出牌位置
        /// </summary>
        [Key(4)]
        public byte FistPlay;

        /// <summary>
        /// 叫牌的牌Id,叫牌的时候就赋值进去
        /// </summary>
        [Key(5)]
        public byte[] JiaoCard;

        /// <summary>
        /// 记录打完牌的位置 0号位置是头游 1号位置是二游 2-三游 3-四游
        /// </summary>
        [Key(6)]
        public byte[] OverPos;

        /// <summary>
        /// 谁和谁是队友 
        /// 存储队友2个位置，从1开始，如果第二个位置是100以上，说明暗牌那个玩家还没有打下来
        /// </summary>
        [Key(7)]
        public byte[] DuiYouPos;

        /// <summary>
        /// 记录要显示的赏牌。炸弹玩法和打牌过程中如果要显示赏去就要使用它。
        /// </summary>
        [Key(8)]
        public List<PlayOutCardShang> ShangCards;

        /// <summary>
        /// 4个玩家的赏分,打牌过程中和结算都用它，结算会覆盖打牌过程中的值
        /// </summary>
        [Key(9)]
        public byte[] ShangScore;

        /// <summary>
        /// 赏分输赢记录
        /// </summary>
        [Key(10)]
        public sbyte[] ShangTotalScore;

        /// <summary>
        /// 牌局输赢分，炸弹玩法该值没有
        /// </summary>
        [Key(11)]
        public sbyte[] PlayScore;

        /// <summary>
        /// 发牌以后统计玩家王是否有赏分，如果有的话，则出的任何一张王都显示 1就是有王的赏分，0就是没有
        /// </summary>
        [Key(12)]
        public byte[] WangScore;

        /// <summary>
        /// 玩家托管标识 0没有托管 1托管中
        /// </summary>
        [Key(13)]
        public byte[] PlayTuoGauan;

        public void Init()
        {
            MaxPlayCard = new PlayOutCard();
            ActivePlayCard = new PlayOutCard();
            OverPos = new byte[4];
            DuiYouPos = new byte[2];
            JiaoCard = new byte[2];
            ShangCards = new List<PlayOutCardShang>(20);
            ShangCards.Clear();
            ShangScore = new byte[4];
            PlayScore = new sbyte[4];
            WangScore = new byte[4];
            ShangTotalScore = new sbyte[4];
            PlayTuoGauan = new byte[4];
        }
    }

    [MessagePackObject]
    public struct PlayOutCard : IPlayOutCardType1
    {
        /// <summary>
        /// 牌的位置从1开始
        /// </summary>
        [Key(0)]
        public byte Pos { get; set; }

        /// <summary>
        /// 牌的类型
        /// </summary>
        [Key(1)]
        public CardType1 Type { get; set; }

        /// <summary>
        /// 牌大小-运算大小
        /// 正的510K 数值是21，副的510K是20
        /// </summary>
        [Key(2)]
        public byte GameNum { get; set; }

        /// <summary>
        /// 出的牌绝对Id集合
        /// </summary>
        [Key(3)]
        public byte[] Ids { get; set; }

        public void Init(int len)
        {
            Ids = new byte[len];
        }

        public override string ToString()
        {
            return $"pos:{Pos} type:{Type} num:{GameNum} ids:{(Ids == null || Ids.Length <= 0 ? "不要" :($"len:{Ids.Length}--{string.Join(",", Ids)}"))}";
        }
    }
    
    [MessagePackObject]
    public struct PlayOutCardShang
    {
        /// <summary>
        /// 牌的位置从1开始
        /// </summary>
        [Key(0)]
        public byte Pos;

        /// <summary>
        /// 牌的类型
        /// </summary>
        [Key(1)]
        public CardType1 Type;

        /// <summary>
        /// 牌大小-运算大小
        /// 正的510K 数值是21，副的510K是20
        /// </summary>
        [Key(2)]
        public byte GameNum;

        /// <summary>
        /// 出的牌绝对Id集合
        /// </summary>
        [Key(3)]
        public TCardInfo[] Cards;

        public void Init(int len)
        {
            Cards = new TCardInfo[len];
        }

        public override string ToString()
        {
            return $"pos:{Pos} type:{Type} num:{GameNum} ids:{(Cards == null || Cards.Length <= 0 ? "不要" : string.Join(",", Cards))}";
        }
    }

    [MessagePackObject]
    public struct Rule
    {
        /// <summary>
        /// 首局是否随机坐庄 true是 false不是
        /// </summary>
        [Key(0)]
        public bool ZhuangRandom;

        /// <summary>
        /// 是否支持单龙 true支持 false不支持
        /// </summary>
        [Key(1)]
        public bool IsLong;

        /// <summary>
        /// 是否显示赏区 true显示 false不显示
        /// </summary>
        [Key(2)]
        public bool IsShowShang;

        /// <summary>
        /// 托管的时间 0不托管 1超过1分钟托管 3超过3分钟托管 5超过5分钟托管
        /// </summary>
        [Key(3)]
        public int TuoGuanNum;

        /// <summary>
        /// 是否是炸弹玩法 true是 false不是
        /// </summary>
        [Key(4)]
        public bool IsBom;

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            if (ZhuangRandom)
            {
                sb.Append("随机坐庄 ");
            }
            if (IsLong)
            {
                sb.Append("支持单龙 ");
            }
            if (IsShowShang)
            {
                sb.Append("显示赏区 ");
            }
            if (TuoGuanNum > 0)
            {
                sb.Append(TuoGuanNum + "分钟托管 ");
            }
            if (IsBom)
            {
                sb.Clear();
                sb.Append("炸弹玩法 ");
            }
            return sb.ToString();
        }
    }

   
}
