package com.gh.common.prioritychain

import android.app.Activity
import android.os.Handler
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.gh.common.util.NewFlatLogUtils
import com.gh.gamecenter.GameDetailActivity
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.view.BugFixedPopupWindow
import com.gh.gamecenter.core.utils.DisplayUtils
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.databinding.PopupAccelerateNotificationBinding
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.wrapper.MainWrapperViewModel

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

    private var mActivity: Activity? = null
    private var mHandler: Handler? = null
    private var mGameList: List<GameEntity>? = null
    private var mViewModel: MainWrapperViewModel? = null

    /**
     * 提前预处理显示弹窗的内容
     */
    fun doPreProcess(
        activity: Activity,
        handler: Handler,
        gameEntityList: List<GameEntity>?,
        viewModel: MainWrapperViewModel
    ) {
        mActivity = activity
        mGameList = gameEntityList
        mHandler = handler
        mViewModel = viewModel

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

    override fun onProcess() : Boolean {
        when (getStatus()) {
            STATUS_VALID -> {
                val accelerateSet =
                    HashSet(SPUtils.getStringSet(Constants.SP_ACCELERATE_NOTIFICATION_POP_UP_SET))
                if (!mGameList.isNullOrEmpty() && !accelerateSet.contains(mGameList!![0].messageId)) {
                    showAccelerateNotificationPopupWindow(mActivity!!, mViewModel, mHandler, mGameList!![0]) {
                        processNext()
                    }
                    accelerateSet.add(mGameList!![0].messageId)
                    SPUtils.setStringSet(Constants.SP_ACCELERATE_NOTIFICATION_POP_UP_SET, accelerateSet)
                    return true
                } else {
                    processNext()
                }
            }

            STATUS_INVALID -> {
                processNext()
            }
        }

        return false
    }

    companion object {
        fun showAccelerateNotificationPopupWindow(
            activity: Activity,
            viewModel: MainWrapperViewModel?,
            handler: Handler?,
            gameEntity: GameEntity?,
            dismissCallback: (() -> Unit)?
        ) {
            val binding: PopupAccelerateNotificationBinding =
                PopupAccelerateNotificationBinding.inflate(LayoutInflater.from(activity))
            if (gameEntity != null) {
                binding.gameIconView.displayGameIcon(gameEntity)
                binding.gameNameTv.text = gameEntity.name
                binding.root.setOnClickListener {
                    NewFlatLogUtils.logMessageInformClickPluginVersion(
                        gameEntity.id,
                        gameEntity.name!!
                    )
                    if (gameEntity.messageId.isNotEmpty()) {
                        // 把对应系统消息设为已读
                        viewModel?.postMessageRead(gameEntity.messageId)
                    }
                    GameDetailActivity.startGameDetailActivity(
                        context = activity,
                        gameId = gameEntity.id,
                        entrance = "首页插件上架弹窗",
                        isSkipGameComment = false,
                        scrollToLibao = false,
                        openVideoStreaming = false,
                        openPlatformWindow = true,
                        traceEvent = null
                    )
                }
            }
            BugFixedPopupWindow(
                binding.root,
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.WRAP_CONTENT
            ).apply {
                isTouchable = true
                isFocusable = true
                isOutsideTouchable = true
                animationStyle = com.gh.gamecenter.common.R.style.popup_window_ease_in_and_out_anim_style
                showAtLocation(
                    activity.window.decorView,
                    Gravity.TOP,
                    0,
                    DisplayUtils.getStatusBarHeight(activity.resources) + DisplayUtils.dip2px(42f)
                )
                setOnDismissListener {
                    dismissCallback?.invoke()
                }
                handler?.postDelayed({ dismiss() }, 5000)
            }
        }
    }
}