using System.Text;
using Guru.LitJson;
using UnityEngine;
using UnityEngine.Networking;

namespace Guru
{
	public class RefreshFirebaseTokenRequest : RequestBase
	{
		protected override string RequestURL => IPMConfig.IPM_URL + "auth/api/v1/renewals/firebase";
		
		protected override UnityWebRequest CreateRequest()
		{
			var request = new UnityWebRequest(RequestURL, "POST");
			//request.uploadHandler = new UploadHandlerRaw();
			request.downloadHandler = new DownloadHandlerBuffer();
			Debug.Log($"[SDK] --- RefreshFirebaseTokenRequest :: contentTYpe: {IPMConfig.Header_Value_ContentType}    AppID: {IPMConfig.IPM_X_APP_ID}    UID: {IPMConfig.IPM_UID}    AccessToken: {IPMConfig.IPM_TOKEN}");
			request.SetRequestHeader(IPMConfig.Header_Param_ContentType, IPMConfig.Header_Value_ContentType);
			request.SetRequestHeader(IPMConfig.Header_Param_APPID, IPMConfig.IPM_X_APP_ID);
			request.SetRequestHeader(IPMConfig.Header_Param_UID, IPMConfig.IPM_UID);
			request.SetRequestHeader(IPMConfig.Header_Param_ACCESS_TOKEN, IPMConfig.IPM_TOKEN);
			return request;
		}

		protected override void RequestSuccessCallBack(string response)
		{
			ResponseData<TokenResponse> responseData =
				JsonUtility.FromJson<ResponseData<TokenResponse>>(response);
			
			Debug.Log(response);
			Debug.Log(responseData.data.ToString());
			IPMConfig.IPM_FIREBASE_TOKEN = responseData.data.firebaseToken;
			IPMConfig.IPM_FIREBASE_TOKEN_TIME = TimeUtil.GetCurrentTimeStampSecond();
			
			this.Log("@@@ Send OK!");
		}
	}
}