using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MessagePack;

namespace GameMessage.THGuoZha
{
    /// <summary>
    /// 泰和过炸的网络包
    /// </summary>
    [MessagePackObject]
    public struct TaiHeGuoZhaGamePack
    {
        /// <summary>
        /// 游戏内存
        /// </summary>
        [Key(0)]
        public LogicInfo Info;

        /// <summary>
        /// 玩家手牌
        /// </summary>
        [Key(1)]
        public List<OneHandCard>[] CardPack;

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

        public void Init()
        {
            Info = new LogicInfo();
            CardPack = null;
            Rule = new Rule();
        }

        public void Init(int ren)
        {
            Info = new LogicInfo();
            Rule = new Rule();
            Info.Init(ren);
            Info.GameNum = (byte)ren;
            CardPack = new List<OneHandCard>[ren];
            for (int i = 0; i < ren; i++)
            {
                CardPack[i] = new List<OneHandCard>();
            }
        }
    }

    /// <summary>
    /// 一手牌
    /// </summary>
    [MessagePackObject]
    public struct OneHandCard
    {
        /// <summary>
        /// 花色集合
        /// </summary>
        [Key(0)]
        public List<byte> Flower;

        /// <summary>
        /// 大小
        /// </summary>
        [Key(1)]
        public byte GameNum;

        /// <summary>
        /// 这一手牌的牌Id集合
        /// </summary>
        [Key(2)]
        public List<ushort> Ids;
    }

    /// <summary>
    /// 打出的牌
    /// </summary>
    [MessagePackObject]
    public struct PayOutCard
    {
        /// <summary>
        /// 牌的位置从1开始
        /// </summary>
        [Key(0)]
        public byte Pos;

        /// <summary>
        /// 大小
        /// </summary>
        [Key(1)]
        public byte GameNum;

        /// <summary>
        /// 这一手牌的牌Id集合
        /// </summary>
        [Key(2)]
        public ushort[] Ids;

        /// <summary>
        /// 花色集合--不是大小王的牌就是记录花色，如果是大小王就记录大小王本身的大小(Num)
        /// </summary>
        [Key(3)]
        public byte[] Flower;

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

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

        public bool IsOk()
        {
            return Pos > 0 && GameNum > 0 && Ids != null && Ids.Length > 0 && Flower != null && Flower.Length > 0;
        }
    }

    /// <summary>
    /// 游戏逻辑内存
    /// </summary>
    [MessagePackObject]
    public struct LogicInfo
    {
        /// <summary>
        /// 游戏流程 1游戏开始 2游戏发牌 3打牌 4结算
        /// </summary>
        [Key(0)]
        public byte GameZT;

        /// <summary>
        /// 玩家人数2-5 
        /// </summary>
        [Key(1)]
        public byte GameNum;

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

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

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

        /// <summary>
        /// 当前出牌内容
        /// </summary>
        [Key(5)]
        public PayOutCard ActivePlayCard;

        /// <summary>
        /// 一轮的分数
        /// </summary>
        [Key(6)]
        public ushort LunScore;

        /// <summary>
        /// 记录打完牌的位置 下标从0开始记录头游位置 里面的位置是从1开始的 
        /// </summary>
        [Key(7)]
        public byte[] OverPos;

        /// <summary>
        /// 玩家奖的数量
        /// </summary>
        [Key(8)]
        public ushort[] JiangScore;

        /// <summary>
        /// 玩家捡分
        /// </summary>
        [Key(9)]
        public short[] JianScore;

        /// <summary>
        /// 玩家剩余牌
        /// </summary>
        [Key(10)]
        public byte[] ShengYuCard;

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

        /// <summary>
        /// 轮人数
        /// </summary>
        [Key(12)]
        public byte LunPlayCount;

        /// <summary>
        /// 本局得分
        /// </summary>
        [Key(13)]
        public short[] Score;

        /// <summary>
        /// 玩家奖分，结算界面显示
        /// </summary>
        [Key(14)]
        public short[] JiangFen;

        [Key(15)]
        public byte BuYaoCount;


        public void Init(int renshu)
        {
            OverPos = new byte[renshu];
            JiangScore = new ushort[renshu];
            JianScore = new short[renshu];
            Score = new short[renshu];
            JiangFen = new short[renshu];
            ShengYuCard = new byte[renshu];
            PlayTuoGauan = new byte[renshu];
            LunPlayCount = (byte)renshu;
        }
    }

    [MessagePackObject]
    public class CommandPack
    {
        [Key(0)]
        public byte Pos;
        
        [Key(1)]
        public int Idx;

        public static CommandPack Create(byte pos,int idx)
        {
            CommandPack pack = new CommandPack();
            pack.Pos = pos;
            pack.Idx = idx;
            return pack;
        }
    }

    /// <summary>
    /// 泰和过炸规则
    /// </summary>
    [MessagePackObject]
    public struct Rule
    {
        /// <summary>
        /// 是否是冲关模式 false是普通模式，true是冲关模式
        /// </summary>
        [Key(0)]
        public bool IsChongGuan;

        /// <summary>
        /// 0不加王 1加4王 2加8王 3加12王  --只有普通模式才有效果，冲关模式是加8王
        /// </summary>
        [Key(1)]
        public byte AddWang;

        /// <summary>
        /// 0不加奖 1加10奖 2加15奖  --只有普通模式才有效果，冲关模式不加奖
        /// </summary>
        [Key(2)]
        public byte AddJiang;

        /// <summary>
        /// 是否显示手牌（也就是剩余牌） true显示 false不显示
        /// </summary>
        [Key(3)]
        public bool IsShowCard;

        /// <summary>
        /// 开局方式 0手动开局 1延时开局 2自动开局  --这个可以不做，统一系统默认的准备时间开局
        /// 0玩家手动点击准备
        /// 1延时15秒系统帮玩家点击准备
        /// 2延时5秒系统帮玩家点击准备
        /// </summary>
        [Key(4)]
        public byte OpenType;

        /// <summary>
        /// 模式 0不选 1-5秒自动出牌 2-10秒自动出牌 3-15秒自动出牌 4-极速模式（全托，不能取消托管，自动准备）
        /// 1/2/3 时间到了就自动帮出牌，
        /// </summary>
        [Key(5)]
        public byte Type;

        /// <summary>
        /// 开启延时托管 true开启 false未开启   --true开了延时托管就是玩家在第一次托管的情况下，第二次就自动帮玩家托管，玩家可以点击取消托管。
        /// false没有开启的话就不出现托管
        /// type模式里面，如果选择不是1/2/3 就不能勾选开启，如果选择了才可以勾选开启
        /// </summary>
        [Key(6)]
        public bool YanShiTuoGuan;

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            if (IsChongGuan)
            {//冲关模式
                sb.Append("冲关模式, ");
            }
            else
            {//普通模式
                sb.Append("普通模式, ");
                if (AddJiang > 0)
                {
                    sb.Append($"{(AddJiang == 1 ? "无奖加10奖" : "无奖加15奖")}, ");
                }
                switch (AddWang)
                {
                    case 0:
                        sb.Append("不加王, ");
                        break;
                    case 1:
                        sb.Append("加4王, ");
                        break;
                    case 2:
                        sb.Append("加8王, ");
                        break;
                    case 3:
                        sb.Append("加12王, ");
                        break;
                }
            }
            if (Type > 0)
            {
                switch (Type)
                {
                    case 1:
                        sb.Append("5秒自动出牌, ");
                        break;
                    case 2:
                        sb.Append("10秒自动出牌, ");
                        break;
                    case 3:
                        sb.Append("15秒自动出牌, ");
                        break;
                    case 4:
                        sb.Append("极速玩法, ");
                        break;
                }
                if (Type >= 1 && Type <= 3)
                {
                    sb.Append($"{(YanShiTuoGuan ? "开启延时托管" : "关闭延时托管")}, ");
                }
            }
            //switch (OpenType)
            //{
            //    case 0:
            //        sb.Append("手动开局, ");
            //        break;
            //    case 1:
            //        sb.Append("延时开局, ");
            //        break;
            //    case 2:
            //        sb.Append("自动开局, ");
            //        break;
            //}
            sb.Append($"{(IsShowCard ? "显示手牌" : "不显示手牌")}, ");
            return sb.ToString();
        }
    }



    
}
