using Assets.Cyp.Common;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Main : MonoBehaviour
{
    [SerializeField] Text NickName;
    [SerializeField] Text TotalKM;
    [SerializeField] RawImage Avatar;
    [SerializeField] GameObject BtnRide;
    // Start is called before the first frame update
    void Start()
    {
        if (Global.CurrentUser != null)
        {
            SetCurrentUser();
        }
        GetSummary();
        if (BtnRide != null) 
        {
            BtnRide.GetComponent<Button>().onClick.AddListener(GoRide);
        }
    }
    void GoRide() 
    {
        SceneManager.LoadScene("3-MapList");
        Utils.showToast(gameObject,"去骑行");
    }
    void SetCurrentUser() 
    {
        var user = Global.CurrentUser;
        NickName.text = user.Nickname;
        Utils.DisplayImage(StartCoroutine, Avatar, user.WxHeadImg);
    }
    void GetSummary() 
    {
        var res = Global.userApi.GetSummary();
        if (res.result)
        {
            TotalKM.text = $"{res.data.TotalDistance.ToString("#0.00")}KM";
        }
        else 
        {
            Utils.showToast(gameObject, res.errMsg);
        }
        //var res = await NoAuthApi.GetCurrentUser();
        //if (res.result)
        //{
        //    Global.CurrentUser = res.data;
        //    SetCurrentUser();
        //}
        //else 
        //{
        //    Utils.showToast(gameObject, res.errMsg);
        //}
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}
