package com.gh.gamecenter.qqmini

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.therouter.router.Route
import com.gh.gamecenter.common.constant.RouteConsts
import com.gh.gamecenter.core.provider.IQGameProvider
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.qqmini.minigame.opensdk.wx.WXEntryActivityHelper
import com.tencent.qqmini.sdk.launcher.AppLoaderFactory
import com.tencent.qqmini.sdk.launcher.model.ShareData

@com.therouter.inject.ServiceProvider
class QGameProviderImpl : IQGameProvider {

    override fun initOpenSdkLogin(context: Context, wxAppId: String, qqAppId: String) {
        QGameHelper.initOpenSdkLogin(context, wxAppId, qqAppId)
    }

    override fun setLoginInfo(context: Context, userId: String, userName: String, token: String) {
        QGameHelper.setLoginInfo(context, userId, userName, token)
    }

    override fun launchGame(
        activity: Activity,
        qqGameId: String,
        resultCallback: (resultCode: Int, resultData: Bundle?) -> Unit
    ) {
        QGameHelper.launchGame(activity, qqGameId, resultCallback)
    }

    override fun launchGameByLink(
        activity: Activity,
        qqGameLink: String,
        resultCallback: (resultCode: Int, resultData: Bundle?) -> Unit
    ) {
        QGameHelper.launchGameByLink(activity, qqGameLink, resultCallback)
    }

    override fun onWechatLogin(context: Context, content: String, isCanceled: Boolean, message: String) {
        WXEntryActivityHelper.notifyWxLoginResult(context, content, isCanceled, message)
    }

    /**
     * 微信分享结果回调
     * @param resultType 结果 0 代表失败， -1 代表取消， 1 代表成功
     */
    override fun onWechatShare(context: Context, resultType: Int) {
        val tempShareData = WXEntryActivityHelper.sShareData
        WXEntryActivityHelper.sShareData = null

        if (tempShareData != null) {
            when (resultType) {
                -1 -> tempShareData.notifyShareResult(context, ShareData.ShareResult.CANCEL)
                0 -> tempShareData.notifyShareResult(context, ShareData.ShareResult.FAIL)
                1 -> tempShareData.notifyShareResult(context, ShareData.ShareResult.SUCCESS)
            }
        }
    }

    override fun stopAllMiniApp(force: Boolean) {
        AppLoaderFactory.g().context?.let {
            QGameHelper.stopAllMiniApp(it, force)
        }
    }

}