package cn.yyxx.eyuangame.core.network

import android.content.Context
import cn.yyxx.eyuangame.base.entity.SdkChargeInfo
import cn.yyxx.eyuangame.base.entity.SdkRoleInfo
import cn.yyxx.eyuangame.base.utils.Logger
import cn.yyxx.eyuangame.core.entity.LoginType
import cn.yyxx.eyuangame.core.entity.SdkBackLoginInfo
import cn.yyxx.eyuangame.core.internal.IFileRequestCallback
import cn.yyxx.eyuangame.core.internal.IRequestCallback
import cn.yyxx.support.volley.source.VolleyError
import org.json.JSONException
import org.json.JSONObject

/**
 * @author #Suyghur.
 * Created on 2021/06/21
 */
class SdkRequest {

    fun initSdk(context: Context, callback: IRequestCallback) {
        val jsonObject = JSONObject()
        try {
            jsonObject.put("route_path", Host.BASIC_ROUTE_INIT_SDK)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        VolleyRequest.post(context, jsonObject, callback)
    }

    fun userLoginVerify(context: Context, jsonObject: JSONObject, callback: IRequestCallback) {
        try {
            if (jsonObject.getInt("login_type") == LoginType.TYPE_ACCOUNT_LOGIN) {
                jsonObject.put("route_path", Host.BASIC_ROUTE_LOGIN)
            } else {
                jsonObject.put("route_path", Host.BASIC_ROUTE_THIRD_PART_LOGIN)
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        VolleyRequest.post(context, jsonObject, callback)
    }

    fun registerUser(context: Context, jsonObject: JSONObject, callback: IRequestCallback) {
        try {
            jsonObject.put("route_path", Host.BASIC_ROUTE_REGISTER)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        VolleyRequest.post(context, jsonObject, callback)
    }


    fun bindAccount(context: Context, jsonObject: JSONObject, callback: IRequestCallback) {
        try {
            jsonObject.put("route_path", Host.BASIC_ROUTE_BIND_ACCOUNT)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        VolleyRequest.post(context, jsonObject, callback)
    }

    fun bindPhone(context: Context, jsonObject: JSONObject, callback: IRequestCallback) {
        try {
            jsonObject.put("route_path", Host.BASIC_ROUTE_BIND_PHONE)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        VolleyRequest.post(context, jsonObject, callback)
    }

    fun getCaptcha(context: Context, jsonObject: JSONObject, callback: IRequestCallback) {
        try {
            jsonObject.put("route_path", Host.BASIC_ROUTE_GET_CAPTCHA)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        VolleyRequest.post(context, jsonObject, callback)
    }

    fun forgetUserPwd(context: Context, jsonObject: JSONObject, callback: IRequestCallback) {
        try {
            jsonObject.put("route_path", Host.BASIC_ROUTE_FORGET_PWD)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        VolleyRequest.post(context, jsonObject, callback)
    }

    fun modifyUserPwd(context: Context, jsonObject: JSONObject, callback: IRequestCallback) {
        try {
            jsonObject.put("route_path", Host.BASIC_ROUTE_MODIFY_PWD)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        VolleyRequest.post(context, jsonObject, callback)
    }


    fun createOrder(context: Context, chargeInfo: SdkChargeInfo, callback: IRequestCallback) {
        val jsonObject = JSONObject()
        try {
            jsonObject.put("route_path", Host.BASIC_ROUTE_CREATE_ORDER)
            jsonObject.put("login_type", SdkBackLoginInfo.instance.loginType)
            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("cp_pay_time", chargeInfo.cpPayTime)
            jsonObject.put("cp_pay_sign", chargeInfo.cpPaySign)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        VolleyRequest.post(context, jsonObject, callback)
    }


    fun notifyOrder(context: Context, orderId: String, originalJson: String, callback: IRequestCallback) {
        val jsonObject = JSONObject()
        try {
            jsonObject.put("route_path", Host.BASIC_ROUTE_NOTIFY_ORDER)
            jsonObject.put("order_id", orderId)
            jsonObject.put("iap_notify_data", originalJson)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        VolleyRequest.post(context, jsonObject, callback)
    }

    fun submitRoleData(context: Context, timing: Int, roleInfo: SdkRoleInfo, callback: IRequestCallback) {
        val jsonObject = JSONObject()
        try {
            jsonObject.put("route_path", Host.BASIC_ROUTE_SUBMIT_ROLE_INFO)
            jsonObject.put("report_type", timing)
            jsonObject.put("uid", roleInfo.userId)
            jsonObject.put("role_id", roleInfo.roleId)
            jsonObject.put("role_name", roleInfo.roleName)
            jsonObject.put("role_level", roleInfo.roleLevel)
            jsonObject.put("role_ctime", roleInfo.roleCTime)
            jsonObject.put("server_id", roleInfo.serverId)
            jsonObject.put("server_name", roleInfo.serverName)
            jsonObject.put("vip_level", roleInfo.vipLevel)
            jsonObject.put("balance", roleInfo.balance)
            jsonObject.put("total_purchase", roleInfo.totalPurchase)
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        VolleyRequest.post(context, jsonObject, callback)
    }

    fun downloadImageFile(context: Context, url: String) {
        VolleyRequest.downloadImageFile(context, url, object : IFileRequestCallback {
            override fun onResponse(result: String) {
                Logger.d("downloadImageFile onResponse $result")
            }

            override fun onErrorResponse(error: VolleyError) {
                Logger.e("downloadImageFile onErrorResponse")
            }
        })
    }

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