using Newtonsoft.Json;
using System;

namespace TsSFCDeivceClient.Model
{
    public class APIResponseData
    {
        /// <summary>
        /// 状态码
        /// </summary>
        public int Code { get; set; } = 200;

        /// <summary>
        /// 返回的数据
        /// </summary>
        public object Data { get; set; }
        /// <summary>
        /// 错误消息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 登录返回的Token，其他方法不返回
        /// </summary>
        public string Token { get; set; }

        /// <summary>
        /// 操作是否成功
        /// </summary>
        public bool IsSuccess
        {
            get
            {
                return Code == 1;
            }
        }

        /// <summary>
        /// 结果转整数型
        /// </summary>
        /// <returns></returns>
        public int ToInt()
        {
            try
            {
                if (Code == 1)
                    return Convert.ToInt32(Data);
                throw new Exception(Message);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 结果转布尔类型
        /// </summary>
        /// <returns></returns>
        public bool ToBool()
        {
            try
            {
                if (Code == 1)
                    return (bool)Data;
                throw new Exception(Message);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 结果转对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T ToDeserializeObject<T>()
        {
            try
            {
                if (Data == null)
                    return default(T);
                return JsonConvert.DeserializeObject<T>(Data + "");
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
