package com.gh.common.prioritychain

import android.annotation.SuppressLint
import com.gh.common.util.DirectUtils
import com.gh.gamecenter.BuildConfig
import com.gh.gamecenter.common.entity.LaunchRedirect
import com.gh.gamecenter.common.entity.LaunchRedirectWrapper
import com.gh.gamecenter.common.pagelevel.PageLevelManager
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.common.utils.singleToMain
import com.gh.gamecenter.core.AppExecutor
import com.gh.gamecenter.core.utils.CurrentActivityHolder
import com.gh.gamecenter.retrofit.RetrofitManager
import com.gh.gamecenter.wrapper.MainWrapperRepository
import com.halo.assistant.HaloApp

/**
 * 初次启动跳转
 */
class LaunchRedirectHandler(priority: Int) : PriorityChainHandler(priority) {

    private var launchData: LaunchRedirect? = null

    @SuppressLint("CheckResult")
    fun doPreProcess() {
//        if (true) {
        if (HaloApp.getInstance().isBrandNewInstall) {
            RetrofitManager.getInstance().newApi
                .getLaunchRedirect(BuildConfig.VERSION_NAME, HaloApp.getInstance().channel)
                .compose(singleToMain())
                .subscribe(object : BiResponse<LaunchRedirectWrapper>() {
                    override fun onSuccess(data: LaunchRedirectWrapper) {
                        launchData = data.launchRedirect

                        if (launchData == null) {
                            updateStatus(STATUS_INVALID)
                            processNext()
                            return
                        }

                        // 尝试提前设置 tab default 避免首页数据加载完成，tab 选中会闪烁
                        if (launchData?.type == "bottom_tab") {
                            MainWrapperRepository.getInstance().sendSelectTabEvent(launchData!!)
                        }

                        if (getStatus() == STATUS_PENDING) {
                            updateStatus(STATUS_VALID)
                            process()
                        } else {
                            updateStatus(STATUS_VALID)
                        }
                    }

                    override fun onFailure(exception: Exception) {
                        super.onFailure(exception)
                        updateStatus(STATUS_INVALID)
                        processNext()
                    }
                })
        } else {
            updateStatus(STATUS_INVALID)
            processNext()
        }
    }

    override fun onProcess(): Boolean {
        val currentActivity = CurrentActivityHolder.getCurrentActivity()

        if (GlobalPriorityChainHelper.isThisActivityValid(currentActivity) && launchData?.type != "bottom_tab") {
            if (getStatus() == STATUS_VALID) {
                // 设置下一个页面为顶级页面
                PageLevelManager.setNextPageAsSupremePageLevel()

                DirectUtils.directToLinkPage(currentActivity!!, launchData!!, "首次启动跳转", "")
                // 跳转页面不管回调，延迟 500ms 后执行下一个 handler
                AppExecutor.uiExecutor.executeWithDelay({
                    processNext()
                }, 500L)

                return true
            } else if (getStatus() == STATUS_INVALID) {
                processNext()
            }
        }

        return false
    }

}