package cn.yyxx.eyuancomm.impl.channel.oppo

import android.app.Activity
import android.app.Application
import android.content.Context
import android.text.TextUtils
import cn.yyxx.eyuancomm.base.entity.SdkChargeInfo
import cn.yyxx.eyuancomm.base.ext.getMetaDataValue
import cn.yyxx.eyuancomm.base.internal.feature.IApplication
import cn.yyxx.eyuancomm.base.internal.feature.IFeature
import cn.yyxx.eyuancomm.base.utils.Logger
import com.nearme.game.sdk.GameCenterSDK
import com.nearme.game.sdk.callback.ApiCallback
import com.nearme.game.sdk.common.model.biz.PayInfo
import com.nearme.game.sdk.common.model.biz.ReqUserInfoParam
import org.json.JSONObject

/**
 * @author #Suyghur.
 * Created on 2022/01/24
 */
class ChannelSdkOppo : IFeature, IApplication {

    override fun attachBaseContext(application: Application, context: Context) {
    }

    override fun initApplication(application: Application) {
        val appSecret = application.getMetaDataValue("app_secret")?.toString()
        GameCenterSDK.init(appSecret, application)
    }

    override fun initialize(activity: Activity, isLandscape: Boolean, callback: (Int, String) -> Unit) {
        callback(0, "Sdk初始化成功")
    }

    override fun login(activity: Activity, callback: (Int, String) -> Unit) {
        GameCenterSDK.getInstance().doLogin(activity, object : ApiCallback {
            override fun onSuccess(result: String) {
                // 登录成功
                GameCenterSDK.getInstance().doGetTokenAndSsoid(object : ApiCallback {
                    override fun onSuccess(result: String) {
                        Logger.d("doGetTokenAndSsoid: $result")
                        if (TextUtils.isEmpty(result)) {
                            Logger.e("解析渠道Sdk登录信息失败")
                            callback(-1, "解析渠道Sdk登录信息失败")
                            return
                        }
                        try {
                            val jsonObject = JSONObject(result)
                            val token = jsonObject.getString("token")
                            val ssoid = jsonObject.getString("ssoid")
                            Logger.d("token: $token")
                            Logger.d("ssoid: $ssoid")
                            getOppoUserInfo(token, ssoid, callback)
                        } catch (e: Exception) {
                            e.printStackTrace()
                            Logger.e("渠道Sdk登录失败")
                            callback(-1, "渠道Sdk登录失败")
                        }
                    }

                    override fun onFailure(result: String?, code: Int) {
                        Logger.e("解析渠道Sdk登录信息失败，code: $code, msg: $result")
                        callback(-1, "解析渠道Sdk登录信息失败，code: $code, msg: $result")
                    }
                })
            }

            override fun onFailure(result: String, code: Int) {
                // 登录失败
                Logger.e("渠道Sdk登录失败，code: $code, msg: $result")
                callback(-1, "渠道Sdk登录失败，code: $code, msg: $result")
            }

        })
    }

    private fun getOppoUserInfo(token: String, ssoid: String, callback: (Int, String) -> Unit) {
        GameCenterSDK.getInstance().doGetUserInfo(ReqUserInfoParam(token, ssoid), object : ApiCallback {
            override fun onSuccess(result: String) {
                // 获取oppo用户信息成功
                Logger.d("获取oppo用户信息成功，result: $result")
                with(JSONObject()) {
                    put("token", token)
                    put("ssoid", ssoid)
                    callback(0, this.toString())
                }
            }

            override fun onFailure(result: String, code: Int) {
                // 获取oppo用户信息失败
                Logger.e("获取oppo用户信息失败， code: $code, msg: $result")
                callback(-1, "获取oppo用户信息失败，code: $code, msg: $result")
            }
        })
    }

    override fun logout(activity: Activity, callback: (Int, String) -> Unit) {
        callback(0, "用户登出成功")
    }

    override fun charge(activity: Activity, chargeInfo: SdkChargeInfo, isIab: Boolean, callback: (Int, String) -> Unit) {
        with(PayInfo(chargeInfo.orderId, "callback_info", chargeInfo.amount)) {
            productDesc = chargeInfo.productDesc
            productName = chargeInfo.productName
            type = PayInfo.TYPE_NOARMAL_PAY
            currency = "USD"
            callbackUrl = "http://xxxx"
            GameCenterSDK.getInstance().doPay(activity, this, object : ApiCallback {
                override fun onSuccess(result: String) {
                    // 客户端Sdk支付成功
                    Logger.d("aaaaa")
                }

                override fun onFailure(result: String, code: Int) {
                    // 客户端Sdk支付失败
                    Logger.d("oppo支付失败，code: $code, msg: $result")
                    callback(-1, "oppo支付失败，code: $code, msg: $result")
                }
            })
        }
    }

    override fun hasExitView(activity: Activity): Boolean = true

    override fun openExitView(activity: Activity, callback: (Int, String) -> Unit) {
        GameCenterSDK.getInstance().onExit(activity) {
            callback(0, "Sdk退出成功")
        }
    }

    override fun onDestroy(activity: Activity) {
    }

    override fun getUserId(): String = ""

    override fun getChannelVersion(): String = "20210623"

    override fun getChannelName(): String = "oppo"
}