package com.gh.gamecenter.qqmini

import android.app.Activity
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.ResultReceiver
import com.gh.gamecenter.core.utils.ToastUtils
import com.tencent.qqmini.minigame.opensdk.OpenSdkLoginManager
import com.tencent.qqmini.minigame.opensdk.config.OpenSdkConfig
import com.tencent.qqmini.sdk.MiniSDK
import com.tencent.qqmini.sdk.launcher.model.AccountInfo
import com.tencent.qqmini.sdk.launcher.model.ExtParams
import com.tencent.qqmini.sdk.launcher.model.MiniAppInfo
import com.tencent.tauth.Tencent

/**
 * QQ 游戏管理类
 */
internal object QGameHelper {

    private var isOpenSdkLoginInit = false

    fun initOpenSdkLogin(context: Context, wxAppId: String, qqAppId: String) {
        // 若 Android 系统版本低于 5.0 不用初始化
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP || isOpenSdkLoginInit) {
            return
        }

        isOpenSdkLoginInit = true
        MiniSDK.init(context)
        OpenSdkLoginManager.init(
            OpenSdkConfig.Builder()
                .setUseOauth(false)
                .setQqOpenAppId(qqAppId) // QQ互联平台AppId，若不想接入QQ互联，则不设置
                .setWxOpenAppId(wxAppId) // 微信互联平台AppId，若不想接入微信互联，则不设置
                .build(context)
        )
        Tencent.setIsPermissionGranted(true)
    }

    /**
     * 授权完成后设置用户帐号信息
     */
    fun setLoginInfo(context: Context, userId: String, userName: String, token: String) {
        MiniSDK.setLoginInfo(
            context,
            AccountInfo(userId, userName, token.toByteArray()),
            null
        )
    }

    fun launchGame(
        activity: Activity,
        gameId: String,
        resultCallback: (resultCode: Int, resultData: Bundle?) -> Unit
    ) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            ToastUtils.toast("该游戏仅支持安卓5.0及以上设备")
            return
        }

        val extParam = ExtParams().apply {
            customInfo = "" // 心跳透传字段
            resultReceiver = object : ResultReceiver(Handler(Looper.getMainLooper())) {
                override fun onReceiveResult(resultCode: Int, resultData: Bundle?) {
                    resultCallback.invoke(resultCode, resultData)
                }
            }
        }

        MiniSDK.startMiniAppById(activity, gameId, extParam)
    }

    fun launchGameByLink(
        activity: Activity,
        gameLink: String,
        resultCallback: (resultCode: Int, resultData: Bundle?) -> Unit
    ) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            ToastUtils.toast("该游戏仅支持安卓5.0及以上设备")
            return
        }

        val extParam = ExtParams().apply {
            customInfo = "" // 心跳透传字段
            resultReceiver = object : ResultReceiver(Handler(Looper.getMainLooper())) {
                override fun onReceiveResult(resultCode: Int, resultData: Bundle?) {
                    resultCallback.invoke(resultCode, resultData)
                }
            }
        }

        MiniSDK.startMiniAppByLink(activity, gameLink, extParam)
    }

    fun stopAllMiniApp(context: Context, force: Boolean) {
        MiniSDK.stopAllMiniApp(context, force)
    }

    fun stopMiniApp(context: Context, info: MiniAppInfo, force: Boolean) {
        MiniSDK.stopMiniApp(context, info, force)
    }

}