package com.gh.common.prioritychain

import androidx.fragment.app.FragmentActivity
import com.gh.gamecenter.core.utils.CurrentActivityHolder
import com.gh.gamecenter.update.UpdateHelper

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

    fun doPreProcess() {
        UpdateHelper.getUpdate(false) {
            if (getStatus() == STATUS_PENDING) {
                if (UpdateHelper.isUpdateValid(false)) {
                    updateStatus(STATUS_VALID)
                    process()
                } else {
                    updateStatus(STATUS_INVALID)
                    processNext()
                }
            } else {
                if (UpdateHelper.isUpdateValid(false)) {
                    updateStatus(STATUS_VALID)
                } else {
                    updateStatus(STATUS_INVALID)
                }
            }
        }
    }

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

        if (GlobalPriorityChainHelper.isThisActivityValid(currentActivity)) {
            if (getStatus() == STATUS_VALID) {
                UpdateHelper.showUpdateDialog(currentActivity as FragmentActivity) {
                    processNext()
                }

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

        return false
    }

}