package com.adealink.weparty.deeplink

import android.app.Activity
import android.content.Intent
import android.net.Uri
import androidx.core.net.toUri
import com.adealink.frame.log.Log
import com.adealink.frame.router.constant.TAG_ROUTER
import com.adealink.frame.router.manager.IDeepLinkConfig
import com.adealink.frame.util.AppUtil
import com.adealink.weparty.AppModule
import com.adealink.weparty.BuildConfig
import com.adealink.weparty.MainActivity
import com.adealink.weparty.module.account.Account
import com.adealink.weparty.module.account.AccountModule

class DeepLinkConfig : IDeepLinkConfig {
    override fun isLogin(): Boolean {
        return AccountModule.isLogin()
    }

    override fun getLoginRouterPath(): String {
        return Account.Login.PATH
    }

    override fun getHomeRouterPath(): String {
        return AppModule.Main.PATH
    }

    override fun getRoomRouterPath(): String {
        // TODO: 进房路由
        //return Room.Room.PATH
        return ""
    }

    override fun getSplashRouterPath(): String {
        return AppModule.Main.PATH
    }

    override fun isHomeActivityExists(): Boolean {
        return AppUtil.runningActivities.find {
            it.contains(MainActivity::class.java.name)
        } != null
    }

    override fun reportPushClick(intent: Intent) {
//        val pushMessage = pushService.getPushMessageFromIntent(intent) ?: return
//        PushStatEvent(PushStatEvent.Action.CLICK).apply {
//            pushId to "${pushMessage.pushId}"
//            type to "${pushMessage.messageType}"
//            deeplink to "${pushMessage.deeplink}"
//        }.send()
    }

    override fun getDynamicLinkRealUriFromIntent(intent: Intent, callback: (uri: Uri?) -> Unit) {
        // 根据可能出现的uri情况，暂无需再解析Firebase Dynamic Link，这移除了shareManager.handleDynamicLinkIntent方法；
        val link: Uri? = intent.data
        val scheme = link?.scheme ?: ""
        val host = link?.host ?: ""
        var realUri: Uri? = null
        //Applink
        if ((scheme.startsWith("http") && host == BuildConfig.HTTP_DEEP_LINK_HOST)
            || (scheme.startsWith("http") && host == BuildConfig.HTTP_DEBUG_DEEP_LINK_HOST)
        ) {
            val path = link?.path ?: ""
            realUri = "${BuildConfig.DEEP_LINK_SCHEME}://${BuildConfig.DEEP_LINK_HOST}$path".toUri()
                .buildUpon().apply {
                    link?.queryParameterNames?.forEach { parameterName ->
                        appendQueryParameter(parameterName, link.getQueryParameter(parameterName))
                    }
                }.build()
            Log.d(TAG_ROUTER, "getDynamicLinkRealUriFromIntent, link:$link")
            Log.d(TAG_ROUTER, "  => , realUri:$realUri")
//            link?.getQueryParameter(PARAM_LINK)?.let {
//                realUri = Uri.parse(URLDecoder.decode(it, "UTF-8"))
//            }
        } else {
            realUri = intent.data
        }
        callback(realUri)
    }

    override fun getPushMessageType(intent: Intent): Long? {
//        val pushMessage = pushService.getPushMessageFromIntent(intent) ?: return null
//        return pushMessage.messageType
        return null
    }

    override fun getDispatchPathKey(): String {
        return "key_dispatch_path"
    }

    override fun getAuthRedirectUrl(): String {
        return ""
    }

    override fun getFromWebUrl(): String {
        return ""
    }

    override fun getCurrentTopActivity(): Activity? {
        return AppUtil.currentActivity
    }
}