/*
 * 由SharpDevelop创建。
 * 用户： Administrator
 * 日期: 2018-10-19
 * 时间: 9:17
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */

using System;
using System.ComponentModel;
using System.Text;
using GameMessage;
using Newtonsoft.Json;
#if Server
using MrWu.Debug;
#endif

namespace GameData
{
    /// <summary>
    /// 
    /// </summary>
    public class playerData
    {
        /// <summary>
        /// 可携带的最大游戏币数量  -读取检查  - 写库(所有写库)检查  -牛牛打完检查  -上桌检查
        /// </summary>
        public const int MaxMoney = 2000000000;

        #if Server
        /// <summary>
        /// 读取的时候是否超过上限!
        /// </summary>
        [JsonIgnore]
        public int isUpper = 0;
        #endif

        /// <summary>
        /// 内存id
        /// </summary>
        [DefaultValue(-1)] public int id = -1;

        /// <summary>
        /// userid
        /// </summary>
        public int userid;

        /// <summary>
        /// 性别 0男 1女
        /// </summary>
        public int sex;

        /// <summary>
        /// 地区
        /// </summary>
        public string area;

        /// <summary>
        /// 用户名
        /// </summary>
        public string UserName;
        
        /// <summary>
        /// 昵称
        /// </summary>
        public string nickname;

        /// <summary>
        /// 游戏币
        /// </summary>
        public long money;

        /// <summary>
        /// 金币
        /// </summary>
        public int gold;

        /// <summary>
        /// 奖券
        /// </summary>
        public int coupon;

        /// <summary>
        /// 玩家的ip地址 --发包可忽略
        /// </summary>
        public string ip;

        /// <summary>
        /// 经验
        /// </summary>
        public int exp;

        /// <summary>
        /// 玩家状态
        /// </summary>
        /// <see cref="Game.Data.PlayerState"/>
        public int state;

        /// <summary>
        /// 玩家是否离线 0在线 1离线
        /// </summary>
        public int outline;

        /// <summary>
        /// 房卡
        /// </summary>
        public int fangka;

        /// <summary>
        /// 用户类型 0普通玩家    110微信绑定用户 100微信登陆用户 200QQ 300苹果 400华为
        /// </summary>
        public int userType;

        /// <summary>
        /// 玩家所在游戏
        /// </summary>
        [DefaultValue(-1)] public int gameid = -1;

        /// <summary>
        /// 玩家的所在的城堡
        /// </summary>
        [DefaultValue(-1)] public int roomid = -1;

        /// <summary>
        /// 玩家所在的厅id
        /// </summary>
        [DefaultValue(-1)] public int tingid = -1;

        /// <summary>
        /// 桌子id
        /// </summary>
        [DefaultValue(-1)] public int deskid = -1;

        /// <summary>
        /// 玩家的桌位号
        /// </summary>
        [DefaultValue(-1)] public int seat = -1;

        /// <summary>
        /// 准备倒计时 小于0将被扔出桌子 -暂时只有客户端用 -朋友房中, 未开战满人后 30秒不准备就踢出桌子!
        /// </summary>
        //[DefaultValue(-1)]
        public int readCtDown = -1;

        /// <summary>
        /// 头像
        /// </summary>
        public string avatar;

        /// <summary>
        /// 是否挂机
        /// </summary>
        public bool isGuaji;

        /// <summary>
        /// 是否托管 不能设置值，设置要使用方法
        /// </summary>
        //[JsonIgnore]    //忽略此属性
        public bool isDePosit
        {
            get;
            #if Server
            private
            #endif
            set;
        }

        /// <summary>
        /// 是否网络不稳定
        /// </summary>
        [JsonIgnore] public bool isastable;

        /// <summary>
        /// 桌上状态
        /// </summary>
        [JsonIgnore] public DeskPlayerState dskState;

        /// <summary>
        /// 朋友房房间号
        /// </summary>
        public int friendRoomNum;

        /// <summary>
        /// 朋友房唯一码
        /// </summary>
        [JsonIgnore] public string friendWeiYiMa;

        /// <summary>
        /// 玩家性格
        /// </summary>
        public int mettle = 0;

        /// <summary>
        /// 是否是代理
        /// </summary>
        public int daili = 0;

        /// <summary>
        /// 玩家魅力
        /// </summary>
        public int Charm = 0;

        /// <summary>
        /// 防沉迷用户认证标识
        /// </summary>
        public string pi;

        /// <summary>
        /// 2023年4月20日15:09:09 改成钻石，新的充值货币
        /// </summary>
        public int MatchRoll = 0;

        /// <summary>
        /// 注册时间
        /// </summary>
        public DateTime RegTime;

        /// <summary>
        /// 推广标识
        /// </summary>
        [JsonIgnore] public string platTag = null;

        /// <summary>
        /// 头像类型
        /// </summary>
        public int HeadType = 0;

        /// <summary>
        /// 玩家的设备信息
        /// </summary>
        public DeviceInfo device = new DeviceInfo();

        /// <summary>
        /// 是否参加比赛
        /// </summary>
        public bool IsJoinMatch;

        /// <summary>
        /// 平台类型
        /// </summary>
        ///<see cref="Game.Pack.PlatEnum"/>
        public int PlatEnum;

        /// <summary>
        /// 玩家在俱乐部哪个   ------联赛使用
        /// </summary>
        //[JsonIgnore]
        public int ClubId;

#if Server
        /// <summary>
        /// 玩家俱乐部的分数
        /// </summary>
        public double ClubScore = int.MaxValue;
#else
        /// <summary>
        /// 玩家俱乐部的分数
        /// </summary>
        public double ClubScore;
#endif

        /// <summary>
        /// 比赛倍率
        /// </summary>
        public int RaceBeiLv;

        /// <summary>
        /// 更新设备的信息
        /// </summary>
        /// <param name="info"></param>
        /// <returns>返回更新设备信息等级 0表示无关  1表示低级  2表示中等 3表示高级</returns>
        public void UpdateDeviceInfo(DeviceInfo info)
        {
            device.plat = info.plat;
            device.wmsty = info.wmsty;
            device.loginsty = info.loginsty;
            device.wifimac = info.wifimac;
            device.wifissd = info.wifissd;
            device.ip = info.ip;
            device.phone_imei = info.phone_imei;
            device.weidu = info.weidu;
            device.jingdu = info.jingdu;
            device.city = info.city;
            device.province = info.province;
            device.district = info.district;
            device.address = info.address;
        }

        /// <summary>
        /// 更新玩家状态_自动运算
        /// </summary>
        public virtual void UpdatePlayerState()
        {
            if (state == (int)PlayerState.Ready)
            {
                //准备
                dskState = DeskPlayerState.Ready;
            }
            else if (state == (int)PlayerState.Desk)
            {
                //在桌上
                dskState = DeskPlayerState.Desk;
            }
            else if (state == (int)PlayerState.War)
            {
                if (outline == 1)
                {
                    //离线
                    dskState = DeskPlayerState.OutLine;
                }
                // else if (isDePosit)
                // {
                //     //托管
                //     dskState = DeskPlayerState.DePosit;
                // }
                else if (isastable)
                {
                    //网络不稳定
                    dskState = DeskPlayerState.astable;
                }
                else
                    dskState = DeskPlayerState.War;
            }
            else
            {
                //没在桌上
                dskState = DeskPlayerState.None;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public playerData()
        {
            id = -1;
            gameid = -1;
            roomid = -1;
            tingid = -1;
            deskid = -1;
            seat = -1;
            readCtDown = -1;
            dskState = DeskPlayerState.None;

            setMettle();
        }

        /// <summary>
        /// 设置玩家性格
        /// </summary>
        private void setMettle()
        {
            Random random = new Random();
            int r = random.Next(0, 100);
            int a, b, c, d;
            if (r < 18)
                a = 1;
            else if (r < 36)
                a = 2;
            else if (r < 68)
                a = 3;
            else
                a = 4;

            r = random.Next(0, 100);
            if (r <= 80)
                b = 10;
            else if (r <= 90)
                b = 20;
            else
                b = 30;

            c = (random.Next(2) + 1) * 100;

            r = random.Next(0, 100);
            if (r < 80)
                d = 1000;
            else
                d = 2000;

            mettle = a + b + c + d;
        }

        /// <summary>
        /// 获取状态字符串信息
        /// </summary>
        /// <returns></returns>
        public string StateToString()
        {
            switch (state)
            {
                case 0:
                    return "不在游戏中";
                case 1:
                    return "进入游戏";
                case 2:
                    return "在桌上";
                case 3:
                    return "准备好了";
                case 4:
                    return "开战中";
            }

            return "Error State : " + state;
        }

        /// <summary>
        /// 获取用户类型字符串信息
        /// </summary>
        /// <returns></returns>
        public string UserTypeToString()
        {
            switch (userType)
            {
                case 0:
                    return "普通用户";
                case 100:
                    return "微信登录用户";
                case 110:
                    return "微信绑定用户";
                case 200:
                    return "QQ登录用户";
                case 300:
                    return "苹果登录用户";
                case 400:
                    return "华为登录用户";
            }

            return "Error UserType : " + userType;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sbl = new StringBuilder();

            sbl.Append("id:" + id + "\r\n");
            sbl.Append("userid:" + userid + "\r\n");
            sbl.Append("sex:" + sex + "\r\n");
            sbl.Append("地区:" + area + "\r\n");
            sbl.Append("昵称:" + nickname + "\r\n");
            sbl.Append("游戏币:" + money + "\r\n");
            sbl.Append("金币:" + gold + "\r\n");
            sbl.Append("奖券:" + coupon + "\r\n");
            sbl.Append("经验:" + exp + "\r\n");
            sbl.Append("是否离线:" + outline + Environment.NewLine);
            sbl.Append("玩家状态:" + StateToString() + "\r\n");
            sbl.Append("房卡:" + fangka + "\r\n");
            sbl.Append("魅力:" + Charm + "\r\n");
            sbl.Append("玩家所在游戏:" + gameid + "\r\n");
            sbl.Append("玩家的房间号:" + roomid + "\r\n");
            sbl.Append("玩家所在的厅:" + tingid + "\r\n");
            sbl.Append("玩家所在的桌子:" + deskid + "\r\n");
            sbl.Append("玩家的座位号:" + seat + "\r\n");
            //sbl.Append("剩余准备时间:" + readCtDown + "\r\n");
            sbl.Append("用户类型:" + UserTypeToString() + "\r\n");
            sbl.Append("代理:" + daili + "\r\n");
            sbl.Append("钻石：" + MatchRoll + "\r\n");
            sbl.Append("pi:" + pi + "\r\n");
            sbl.Append("注册时间:" + RegTime + "\r\n");
            sbl.Append("是否比赛中:" + IsJoinMatch + "\r\n");
            return sbl.ToString();
        }

        /// <summary>
        /// 玩家托管状态变化事件
        /// </summary>
        [JsonIgnore]
        public Action<playerData> DepositChange;
        
        #if Server
        public void SetDeposit(bool value)
        {
            if (isDePosit != value)
            {
                isDePosit = value;
                try
                {
                    DepositChange?.Invoke(this);
                }
                catch (Exception e)
                {
                    Debug.Error($"托管状态改变出错:{e.Message}");
                }
            }
        }
        #endif
    }
}