using Assets.Scripts.Apis.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assets.Scripts.Apis
{
    public class UserApi : ApiBase
    {
        public async Task<JsonResult<UserResultModel>> Login(string phone, string pwd, string countryCode)
        {
            var param = new
            {
                Phone = phone,
                Pwd = pwd,
                countryCode
            };

            JsonResult<UserResultModel> ret = null;
            try
            {
                ret = await PostAsync<JsonResult<UserResultModel>>("NoAuth/v1/Login", param);
            }
            catch (Exception ex)
            {
                return new JsonResult<UserResultModel> { result = false, errMsg = ex.Message };
            }
            //ApiBase.cookies = response.Cookies;
            //var ret = response.DynamicBody;
            if (ret.result)
            {
                //var str = Newtonsoft.Json.JsonConvert.SerializeObject(ret.data);
                var user = new UserResultModel();
                user.Id = ret.data.Id;
                user.Nickname = ret.data.Nickname;
                user.Phone = ((string)ret.data.Phone).Trim();
                user.Country = ret.data.Country;
                user.Province = ret.data.Province;
                user.City = ret.data.City;
                user.Area = ret.data.Area;
                user.Pass = ret.data.Pass;
                user.Sex = ret.data.Sex;
                user.Unit = ret.data.Unit;
                user.Weight = ret.data.Weight;
                user.FTP = ret.data.FTP;
                user.WheelDiameter = ret.data.WheelDiameter;
                user.Email = user.Email ?? "";
                user.Pass = user.Pass ?? "";
                user.OtherPlatforms = user.OtherPlatforms ?? "";
                user.WxHeadImg = ret.data.WxHeadImg ?? "";
                //user.LoginTime = ret.data.LoginTime;
                user.LastLoginTime = ret.data.LastLoginTime;
                user.AutoPause = ret.data.AutoPause;
                user.MaxHeartRate = ret.data.MaxHeartRate;
                user.CanEditRoom = ret.data.CanEditRoom;
                user.BicycleWeight = ret.data.BicycleWeight;
                user.Contact = ret.data.Contact;
                user.ContactPhone = ret.data.ContactPhone;
                user.ContactAddress = ret.data.ContactAddress;
                user.Sensitivity = ret.data.Sensitivity;
                if (ret.data.ConnectedToStrava != null)
                {
                    user.ConnectedToStrava = ret.data.ConnectedToStrava;
                }
                //todo: check local database, insert new user

                //PubCommData.CurrentUser = user;

                return new JsonResult<UserResultModel>() { result = true, data = user };
            }
            else
            {
                return new JsonResult<UserResultModel>() { result = false, data = null, errMsg = ret.errMsg };
            }
        }

        /// <summary>
        /// 获取个人中心的统计信息
        /// </summary>
        /// <returns></returns>
        public JsonResult<SummaryResultModel> GetSummary()
        {
            var result = Get<JsonResult<SummaryResultModel>>("User/GetSummary");

            return result;
        }

        public DateTime GetNow()
        {
            var result = Get("NoAuth/GetNow");

            return DateTime.Parse(result);
        }
    }
}
