package com.gh.common.prioritychain

import android.os.Bundle
import androidx.fragment.app.FragmentActivity
import com.gh.common.dialog.AccelerateExpirationDialogFragment
import com.gh.gamecenter.common.constant.Constants.SP_ACCELERATOR_MEMBERSHIP_EXPIRED
import com.gh.gamecenter.core.utils.CurrentActivityHolder
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.login.user.UserRepository
import com.halo.assistant.accelerator.repository.AcceleratorDataHolder

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

    fun doPreProcess() {
        UserRepository.getInstance().setAutoLoginListener {
            val hasShow = SPUtils.getBoolean(SP_ACCELERATOR_MEMBERSHIP_EXPIRED)
            val vipEntity = AcceleratorDataHolder.instance.vipEntity
            if (getStatus() == STATUS_PENDING) {
                if (vipEntity == null ||
                    vipEntity.vipStatus ||
                    vipEntity.isNewUser ||
                    vipEntity.isVipRefundUser ||
                    vipEntity.isDurationRefundUser ||
                    hasShow
                ) {
                    processNext()
                } else {
                    updateStatus(STATUS_VALID)
                    process()
                }
            } else {
                if (vipEntity == null ||
                    vipEntity.vipStatus ||
                    vipEntity.isNewUser ||
                    vipEntity.isVipRefundUser ||
                    vipEntity.isDurationRefundUser ||
                    hasShow
                ) {
                    updateStatus(STATUS_INVALID)
                } else {
                    updateStatus(STATUS_VALID)
                }
            }
        }
    }

    override fun onProcess(): Boolean {
        val currentActivity = CurrentActivityHolder.getCurrentActivity()
        if (GlobalPriorityChainHelper.isThisActivityValid(currentActivity)) {
            when (getStatus()) {
                STATUS_VALID -> {
                    val reminder = AccelerateExpirationDialogFragment.ExpirationReminder.TimeRunsOut("")
                    SPUtils.setBoolean(SP_ACCELERATOR_MEMBERSHIP_EXPIRED, true)
                    val dialogFragment = AccelerateExpirationDialogFragment().apply {
                        arguments = Bundle().apply {
                            putParcelable(
                                AccelerateExpirationDialogFragment.KEY_EXPIRATION_REMINDER, reminder
                            )
                        }
                    }
                    dialogFragment.dialog?.setOnDismissListener {
                        processNext()
                    }
                    dialogFragment.show(
                        (currentActivity as FragmentActivity).supportFragmentManager,
                        AccelerateExpirationDialogFragment::class.java.name
                    )

                    return true
                }

                STATUS_INVALID -> {
                    processNext()
                }
            }
        }

        return false
    }
}