package com.gh.gamecenter

import android.app.Activity
import android.content.Context
import android.net.Uri
import android.os.Bundle
import com.therouter.router.Navigator
import com.therouter.router.interceptor.NavigationCallback
import com.therouter.TheRouter
import com.gh.common.util.EntranceUtils
import com.gh.gamecenter.common.constant.EntranceConsts
import com.gh.gamecenter.common.utils.DialogHelper
import com.gh.gamecenter.common.utils.dropSchemeAndHost
import com.halo.assistant.HaloApp
import com.lightgame.utils.Utils

class SkipCompatActivity : SkipActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        val uri = intent.data
        if (handleSkip(this, uri)) {
            intent.setData(null)
        }

        super.onCreate(savedInstanceState)

        finish()
    }

    companion object {
        const val TAG = "SkipCompat"

        const val HOST = "app"

        const val SCHEME = "ghzhushou"
        const val MARKET = "market"

        fun handleSkip(context: Context, uri: Uri?): Boolean {
            if (uri == null) return false

            // Handle skip
            if (uri.scheme == SCHEME) {
                Utils.log(TAG, "handleSkip: $uri")

                val host = uri.host
                if (host == HOST) {
                    TheRouter
                        .build(uri.dropSchemeAndHost())
                        .navigation(context, object : NavigationCallback() {
                            override fun onFound(postcard: Navigator) {
                                Utils.log(TAG, "navigate to postcard ${postcard.originalUrl} onFound" )
                            }

                            override fun onLost(postcard: Navigator, requestCode: Int) {
                                Utils.log(TAG, "navigate to postcard ${postcard.originalUrl} onLost" )

                                if (HaloApp.getInstance().isAlreadyUpAndRunning) {
                                    // 在光环应用内时，显示不支持弹窗
                                    // 显示不支持弹窗
                                    DialogHelper.showUpgradeDialog(context)
                                } else {
                                    // 在光环应用外跳转时，重定向至首页
                                    EntranceUtils.jumpActivity(context, Bundle())
                                }
                            }

                            override fun onArrival(postcard: Navigator) {
                                Utils.log(TAG, "navigate to postcard ${postcard.originalUrl} onArrival" )
                            }

                            override fun onActivityCreated(postcard: Navigator, activity: Activity) {
                                Utils.log(TAG, "navigate to postcard ${postcard.originalUrl} onActivityCreated" )
                            }
                        })

                    return true
                }
            } else if (uri.scheme == MARKET) {
                val host = uri.host
                var id = ""
                try {
                    id = uri.getQueryParameter("id") ?: ""
                } catch (e: Exception) {
                    e.printStackTrace()
                }

                if (host != null) {
                    if ("details" == host) {
                        val bundle = Bundle()
                        bundle.putString(
                            EntranceConsts.KEY_TO,
                            EntranceConsts.KEY_MARKET_DETAILS
                        )
                        bundle.putString(EntranceConsts.KEY_DATA, id)
                        EntranceUtils.jumpActivity(context, bundle)
                    } else {
                        EntranceUtils.jumpActivity(context, Bundle()) // 跳转至首页
                    }
                    return true
                }
            }

            return false
        }
    }

}