package cn.yyxx.eyuancomm.comm.network

import android.content.Context
import cn.yyxx.eyuancomm.base.entity.ResultInfo
import cn.yyxx.eyuancomm.base.entity.SdkChargeInfo
import cn.yyxx.eyuancomm.base.network.VolleyRequest
import org.json.JSONException
import org.json.JSONObject

/**
 * @author #Suyghur.
 * Created on 2022/01/25
 */
class SdkRequest {

    companion object {
        val instance: SdkRequest by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            SdkRequest()
        }
    }

    fun initSdk(context: Context, extObject: String, onResponse: (ResultInfo) -> Unit) {
        val jsonObject = JSONObject()
        jsonObject.put("route_path", Host.BASIC_ROUTE_INIT_SDK)
        jsonObject.put("channel_init_ext", extObject)
        VolleyRequest.post(context, Host.HOST, jsonObject, true, onResponse)
    }

    fun userVerify(context: Context, extObject: String, onResponse: (ResultInfo) -> Unit) {
        val jsonObject = JSONObject()
        jsonObject.put("route_path", Host.BASIC_ROUTE_USER_VERIFY)
        jsonObject.put("channel_ext", extObject)
        VolleyRequest.post(context, Host.HOST, jsonObject, true, onResponse)
    }

    fun createOrder(context: Context, extObject: String, chargeInfo: SdkChargeInfo, onResponse: (ResultInfo) -> Unit) {
        val jsonObject = JSONObject()
        try {
            jsonObject.put("route_path", Host.BASIC_ROUTE_CREATE_ORDER)
            jsonObject.put("uid", chargeInfo.userId)
            jsonObject.put("role_id", chargeInfo.roleId)
            jsonObject.put("role_name", chargeInfo.roleName)
            jsonObject.put("role_level", chargeInfo.roleLevel)
            jsonObject.put("role_ctime", chargeInfo.roleCTime)
            jsonObject.put("server_id", chargeInfo.serverId)
            jsonObject.put("server_name", chargeInfo.serverName)
            jsonObject.put("amount", chargeInfo.amount)
            jsonObject.put("product_id", chargeInfo.productId)
            jsonObject.put("product_name", chargeInfo.productName)
            jsonObject.put("product_desc", chargeInfo.productDesc)
            jsonObject.put("cp_order_id", chargeInfo.cpOrderId)
            jsonObject.put("cp_ext", chargeInfo.cpExt)
            jsonObject.put("channel_ext", extObject)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        VolleyRequest.post(context, Host.HOST, jsonObject, true, onResponse)
    }
}