package cn.yyxx.eyuangame.core.network

import android.content.Context
import android.text.TextUtils
import cn.yyxx.eyuangame.Version
import cn.yyxx.eyuangame.base.entity.SdkChargeInfo
import cn.yyxx.eyuangame.base.entity.SdkRoleInfo
import cn.yyxx.eyuangame.base.utils.ParamsUtils
import cn.yyxx.eyuangame.core.entity.LoginType
import cn.yyxx.eyuangame.core.internal.IRequestCallback
import cn.yyxx.support.AppUtils
import cn.yyxx.support.DensityUtils
import cn.yyxx.support.device.DeviceInfoUtils
import cn.yyxx.support.gaid.GAIDUtils
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()
//        jsonObject.put("common", getCommon(context))
        jsonObject.put("route_path", Host.BASIC_ROUTE_INIT_SDK)
        VolleyRequest.post(context, Host.HOST, jsonObject, callback)
    }

    fun userLoginVerify(context: Context, jsonObject: JSONObject, callback: IRequestCallback) {
        try {
            jsonObject.put("common", getCommon(context))
            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: Exception) {
            e.printStackTrace()
        }
        VolleyRequest.post(context, Host.HOST, jsonObject, callback)
    }

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

    fun forgetUserPwd(context: Context, jsonObject: JSONObject, callback: IRequestCallback) {

    }

    fun modifyUserPwd(context: Context, jsonObject: JSONObject, callback: IRequestCallback) {

    }

    fun bindPhone(context: Context, jsonObject: JSONObject, callback: IRequestCallback) {

    }

    fun bindAccount(context: Context, jsonObject: JSONObject, callback: IRequestCallback) {

    }

    fun createOrder(context: Context, chargeInfo: SdkChargeInfo, callback: IRequestCallback) {

    }

    fun submitRoleData(context: Context, roleInfo: SdkRoleInfo, callback: IRequestCallback) {

    }

    private fun getCommon(context: Context): JSONObject {
        val common = JSONObject()
        try {
            common.put("gcp_code", ParamsUtils.getGcpCode(context))
            common.put("game_code", ParamsUtils.getGameCode(context))
            common.put("package_name", context.packageName)
            common.put("server_version", Version.SERVER_VERSION)
            common.put("client_version", Version.VERSION_NAME)
            common.put("game_version", AppUtils.getVersionName(context))
            common.put("screen", DensityUtils.getResolution(context))
            common.put("simulator", DeviceInfoUtils.isEmulator(context))
            if (TextUtils.isEmpty(GAIDUtils.getGoogleAdid())) {
                common.put("device_id", DeviceInfoUtils.getAndroidDeviceId(context))
                common.put("id_type", 1)
            } else {
                common.put("device_id", GAIDUtils.getGoogleAdid())
                common.put("id_type", 0)
            }
            common.put("network", DeviceInfoUtils.getNetworkType(context))
            common.put("os", "android")
            common.put("os_version", DeviceInfoUtils.getDeviceSoftwareVersion())
            common.put("mobile_model", DeviceInfoUtils.getDeviceModel())
            common.put("mobile_mfrs", DeviceInfoUtils.getDeviceManufacturer())
            common.put("mobile_brand", DeviceInfoUtils.getDeviceBrand())
        } catch (e: JSONException) {
            e.printStackTrace()
        }
        return common
    }


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