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

namespace Guru
{
    public class GoogleOrderRequest : RequestBase
    {
        public int orderType;
        public string productId;
        public string subscriptionId;
        public string token;
        public string packageName;
        public int level;

        public GoogleOrderRequest(){}
        
        public GoogleOrderRequest(int orderType, string productId, string subscriptionId, 
            string token, int level)
        {
            this.orderType = orderType;
            this.packageName = Application.identifier;
            this.productId = productId;
            this.subscriptionId = subscriptionId;
            this.token = token;
            this.level = level;
        }

        protected override string RequestURL => IPMConfig.IPM_URL + "order/api/v1/orders/android";
        protected override UnityWebRequest CreateRequest()
        {
            GoogleOrderData googleOrderData = new GoogleOrderData(orderType, productId, token, level);
            this.Log($"send orderData:{googleOrderData}"); 
            var request = new UnityWebRequest(RequestURL, "POST");
            request.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(JsonMapper.ToJson(googleOrderData)));
            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_UID, IPMConfig.IPM_UID);
            request.SetRequestHeader(IPMConfig.Header_Param_ACCESS_TOKEN, IPMConfig.IPM_TOKEN);
            return request;
        }

        protected override void RequestSuccessCallBack(string response)
        {
            ResponseData<OrderResponse> responseData = JsonUtility.FromJson<ResponseData<OrderResponse>>(response);
            if (responseData != null && responseData.data != null)
            {
                // Analytics.Tch001IAPRev(responseData.data.usdPrice);
                double usdPrice = responseData.data.usdPrice;
                Analytics.Tch001IAPRev(usdPrice);
                Analytics.Tch02IAPRev(usdPrice);
                AdjustService.TrackIAPPurchase(usdPrice, productId);
                Analytics.IAPPurchase(usdPrice, productId);
                return;
            }

            if (!string.IsNullOrEmpty(response))
            {
                if (response.Contains("500") || response.Contains("Internal Server Error"))
                {
                    Debug.LogError(
                        $"[IAP] GoogleOrderRequest failed: contact with guru backend server team. Make sure they'v connect order report to this project!");
                }
                else
                {
                    Debug.LogError($"[IAP] GoogleOrderRequest failed: {response}");
                }
            }
            else
            {
                Debug.LogError($"[IAP] GoogleOrderRequest failed: response is null or empty");
            }
        }
    }
}