package com.gh.common.prioritychain

import androidx.fragment.app.FragmentActivity
import com.gh.common.dialog.PrivacyPolicyDialogFragment
import com.gh.gamecenter.core.utils.CurrentActivityHolder
import com.gh.gamecenter.entity.DialogEntity

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

    private var mPrivacyPolicyEntity: DialogEntity.PrivacyPolicyEntity? = null

    /**
     * 提前预处理显示弹窗的内容
     */
    fun doPreProcess(privacyPolicyEntity: DialogEntity.PrivacyPolicyEntity?) {
        mPrivacyPolicyEntity = privacyPolicyEntity

        if (getStatus() == STATUS_PENDING) {
            if (privacyPolicyEntity == null) {
                processNext()
            } else {
                updateStatus(STATUS_VALID)
                process()
            }
        } else {
            if (privacyPolicyEntity == null) {
                updateStatus(STATUS_INVALID)
            } else {
                updateStatus(STATUS_VALID)
            }
        }
    }

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

        if (GlobalPriorityChainHelper.isThisActivityValid(currentActivity)) {
            when (getStatus()) {
                STATUS_VALID -> {
                    PrivacyPolicyDialogFragment.show(currentActivity as FragmentActivity, mPrivacyPolicyEntity) { _: Boolean? ->
                        processNext()
                    }

                    return true
                }

                STATUS_INVALID -> {
                    processNext()
                }
            }
        }

        return false
    }

}