package com.gh.common.prioritychain

import androidx.fragment.app.FragmentActivity
import com.gh.common.dialog.ReserveDialog
import com.gh.gamecenter.core.utils.CurrentActivityHolder
import com.gh.gamecenter.feature.entity.ReserveOnlineEntity

class ReserveDialogHandler(priority: Int) : PriorityChainHandler(priority) {

    private var mReserveData: ReserveOnlineEntity? = null

    /**
     * 提前预处理显示弹窗的内容
     */
    fun doPreProcess(reserveData: ReserveOnlineEntity?) {
        mReserveData = reserveData

        if (getStatus() == STATUS_PENDING) {
            if (reserveData?.games.isNullOrEmpty()) {
                processNext()
            } else {
                updateStatus(STATUS_VALID)
                process()
            }
        } else {
            if (reserveData?.games.isNullOrEmpty()) {
                updateStatus(STATUS_INVALID)
            } else {
                updateStatus(STATUS_VALID)
            }
        }
    }

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

        if (GlobalPriorityChainHelper.isThisActivityValid(currentActivity)) {
            when (getStatus()) {
                STATUS_VALID -> {
                    val reserveDialog = ReserveDialog.getInstance(mReserveData ?:ReserveOnlineEntity())
                    reserveDialog.setOnDismissListener {
                        processNext()
                    }
                    reserveDialog.show((currentActivity as FragmentActivity).supportFragmentManager, "reserveDialog")

                    return true
                }

                STATUS_INVALID -> {
                    processNext()
                }
            }
        }

        return false
    }

}