package com.gh.common.prioritychain

import com.gh.common.iinterface.ISmartRefresh

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

    private var mSmartRefreshFragment: ISmartRefresh? = null

    /**
     * 提前预处理显示弹窗的内容
     */
    fun doPreProcess(smartRefreshFragment: ISmartRefresh?, shouldShow: Boolean) {
        mSmartRefreshFragment = smartRefreshFragment

        if (getStatus() == STATUS_PENDING) {
            if (shouldShow && smartRefreshFragment != null) {
                updateStatus(STATUS_VALID)
                process()
            } else {
                processNext()
            }
        } else {
            if (shouldShow && smartRefreshFragment != null) {
                updateStatus(STATUS_VALID)
            } else {
                updateStatus(STATUS_INVALID)
            }
        }
    }

    override fun onProcess(): Boolean {
        when (getStatus()) {
            STATUS_VALID -> {
                mSmartRefreshFragment?.popupPullDownPush {
                    processNext()
                }

                return true
            }
            STATUS_INVALID -> {
                processNext()
            }
        }

        return false
    }

}