using MessagePack;

namespace GameMessage.PaoDeKuaiF
{
    /// <summary>
    /// 跑得快主包
    /// </summary>
    [MessagePackObject]
    public struct PdkGamePack
    {
        /// <summary>
        /// 牌的内存包
        /// </summary>
        [Key(0)]
        public PdkCardPack CardPack;

        /// <summary>
        /// 跑得快逻辑内存
        /// </summary>
        [Key(1)]
        public PdkLogicInfo Info;
        
        /// <summary>
        /// 初始化包
        /// </summary>
        /// <param name="len"></param>
        public void Init(int len)
        {
            CardPack.Init(len);
            Info.Init(len);

        }
    }

    /// <summary>
    /// 跑得快逻辑包
    /// </summary>
    [MessagePackObject]
    public struct PdkLogicInfo
    {
        /// <summary>
        /// 游戏流程 1游戏开始 2游戏发牌 3包庄 4打牌 5结算
        /// </summary>
        [Key(0)]
        public byte GameZT;

        /// <summary>
        /// 最大的出牌内容
        /// </summary>
        [Key(1)]
        public PlayOutCardPdkF MaxPlayCard;
        /// <summary>
        /// 当前出牌内容
        /// </summary>
        [Key(2)]
        public PlayOutCardPdkF ActivePlayCard;

        /// <summary>
        /// 控制权是哪个位置1-3
        /// </summary>
        [Key(3)]
        public byte WhoPlay;

        /// <summary>
        /// 第一先出的玩家1-3
        /// </summary>
        [Key(4)]
        public byte FistPlay;

        /// <summary>
        /// 存储玩家的包庄 1包庄 2不包庄
        /// </summary>
        [Key(5)]
        public byte[] BaoZhuang;

        /// <summary>
        /// 游戏人数
        /// </summary>
        [Key(6)]
        public byte PlayNum;

        /// <summary>
        /// 记录打完牌的位置因为只要有人出完牌牌局就结束
        /// </summary>
        [Key(7)]
        public byte OverPos;

        /// <summary>
        /// 玩家出的炸弹数量
        /// </summary>
        [Key(8)]
        public int[] ZhaDans;

        /// <summary>
        /// 玩家剩余牌张数
        /// </summary>
        [Key(9)]
        public byte[] ShenYuCard;

        /// <summary>
        /// 玩家最后的输赢分
        /// </summary>
        [Key(10)]
        public int[] Score;

        /// <summary>
        /// 包庄人的位置从1开始
        /// </summary>
        [Key(11)]
        public byte BaoPos;

        /// <summary>
        /// 红桃十玩家的位置
        /// </summary>
        [Key(12)]
        public byte HongTaoShiPos;
        /// <summary>
        /// 玩家托管标识 0没有托管 1托管中
        /// </summary>
        [Key(13)]
        public byte[] PlayTuoGauan;

        public void Init(int len)
        {
            ZhaDans = new int[len];
            BaoZhuang = new byte[len];
            ShenYuCard = new byte[len];
            Score = new int[len];
            PlayTuoGauan = new byte[len];
        }
    }

    [MessagePackObject]
    public struct PlayOutCardPdkF : 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 PlayCardPdkF
    {
        /// <summary>
        /// 1个玩家的手牌
        /// </summary>
        [Key(0)]
        public TCardInfoPdkF[] CardInfos;
        /// <summary>
        /// 初始化玩家牌
        /// </summary>
        public void Init(int len)
        {
            CardInfos = new TCardInfoPdkF[len];
        }
    }
    
    [MessagePackObject]
    public struct TCardInfoPdkF : 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 TCardInfoPdkF))
            {
                return false;
            }

            TCardInfoPdkF other = (TCardInfoPdkF)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;
        }
    }
    
    /// <summary>
    /// 跑得快玩家牌包
    /// </summary>
    [MessagePackObject]
    public struct PdkCardPack
    {
        /// <summary>
        /// 玩家手牌
        /// </summary>
        [Key(0)]
        public PlayCardPdkF[] Cards;

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