package cn.yyxx.eyuancomm.impl.channel.vivo

import android.app.Activity
import android.app.Application
import android.content.Context
import cn.yyxx.eyuancomm.base.entity.SdkChargeInfo
import cn.yyxx.eyuancomm.base.internal.feature.IApplication
import cn.yyxx.eyuancomm.base.internal.feature.IFeature
import com.vivo.unionpay.sdk.open.VivoConstants
import com.vivo.unionpay.sdk.open.VivoPayInfo
import com.vivo.unionpay.sdk.open.VivoRoleInfo
import com.vivo.unionpay.sdk.open.VivoUnionSDK

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

    private var appId = ""
    private var uid = ""

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

    override fun initApplication(application: Application) {
        this.appId = ""
        VivoUnionSDK.initSdk(application, appId)
    }

    override fun initialize(activity: Activity, isLandscape: Boolean, callback: (Int, String) -> Unit) {
        callback(0, "")
    }

    override fun login(activity: Activity, callback: (Int, String) -> Unit) {
        VivoUnionSDK.login(activity) { code, token, uid ->
            this.uid = uid
            if (code == VivoConstants.VIVO_LOGIN_SUCCESS) {
                // 用户登录成功
                callback(code, "")
            } else {
                // 用户取消或登录失败
                callback(code, "Vivo用户取消或登录失败")
            }
        }
    }

    override fun logout(activity: Activity, callback: (Int, String) -> Unit) {
    }

    override fun charge(activity: Activity, chargeInfo: SdkChargeInfo, isIab: Boolean, callback: (Int, String) -> Unit) {
        val params = HashMap<String, String>()
        val roleInfo = VivoRoleInfo()
        val payInfo = VivoPayInfo.Builder()
            .setAppId(appId)
            .setProductId(chargeInfo.productId)
            .setTransNo(chargeInfo.orderId)
            .setExpireTime("")
            .setExtInfo("透传参数")
            .setUid(uid)
            .setNotifyUrl("")
            .setSign("")
            .setSignType("RSA")
            .setVivoRoleInfo(roleInfo)
            .build()
        VivoUnionSDK.pay(activity, payInfo) { code ->
            if (code == VivoConstants.VIVO_PAY_SUCCESS) {
                callback(0, "")
            } else {
                callback(-1, "")
            }
        }
    }

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

    override fun openExitView(activity: Activity, callback: (Int, String) -> Unit) {
    }

    override fun onDestroy(activity: Activity) {
    }

    override fun getUserId(): String = uid

    override fun getChannelVersion(): String = "1.6.3.0"

    override fun getChannelName(): String = "vivo"


}