namespace Guru
{
	using System.Text;
	using Firebase.Crashlytics;
	using UnityEngine;
	using UnityEngine.Networking;
	using System.Collections.Generic;
	using Newtonsoft.Json;
	
	/// <summary>
	/// 中台匿名授权用户请求
	/// https://docs.google.com/document/d/1yRE0HQTwaDfBeH7Zd1li1Xr8JIdfqkz3ixqdjD-aH44/edit#heading=h.eispbvmfw5oo
	/// </summary>
	public class AuthUserRequest : RequestBase
	{
		protected override string RequestURL => IPMConfig.IPM_URL + "auth/api/v1/tokens/provider/secret";

		protected override UnityWebRequest CreateRequest()
		{
			var data = new Dictionary<string, object>()
			{
				["secret"] = IPMConfig.IPM_DEVICE_ID,
				["eventConfig"] = EventConfig.Build(),
			};
			var  json = JsonConvert.SerializeObject(data);
			Debug.Log($"[SDK] --- AuthUserRequest json: {json}");
			var request = new UnityWebRequest(RequestURL, "POST");
			request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(json));
			request.downloadHandler = new DownloadHandlerBuffer();
			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_DEVICE_INFO, new DeviceInfoData().ToString());
			return request;
		}

		protected override void RequestSuccessCallBack(string response)
		{
			ResponseData<TokenResponse> responseData =
				JsonUtility.FromJson<ResponseData<TokenResponse>>(response);

			if (responseData == null || responseData.data == null)
				return;
			
			this.Log(response);
			this.Log(responseData.data.ToString());
			
			Analytics.SetFirebaseUserId(responseData.data.uid);
			
			IPMConfig.IPM_UID = responseData.data.uid;
			IPMConfig.IPM_UID_INT = responseData.data.uidInt;
			IPMConfig.IPM_TOKEN = responseData.data.token;
			IPMConfig.IPM_FIREBASE_TOKEN = responseData.data.firebaseToken;
			IPMConfig.IPM_CREATED_TIMESTAMP = responseData.data.createdAtTimestamp;
			IPMConfig.IPM_NEWUSER = responseData.data.newUser;
			IPMConfig.IPM_TOKEN_TIME = TimeUtil.GetCurrentTimeStampSecond();
			IPMConfig.IPM_FIREBASE_TOKEN_TIME = TimeUtil.GetCurrentTimeStampSecond();
			IPMConfig.IPM_UUID = IDHelper.GenUUID(responseData.data.uid);
			DeviceUtil.Save2AppGroup();
		}
		
	}
}