package com.gh.common.prioritychain

import android.content.Context
import android.view.LayoutInflater
import android.widget.FrameLayout
import com.airbnb.lottie.LottieAnimationView
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.databinding.LayoutCommunityHomeVideoGuideBinding

class CommunityHomeGuideHandler(
    priority: Int,
    private val context: Context,
    private val decorView: FrameLayout?,
    private val videoLottie: LottieAnimationView?
) : PriorityChainHandler(priority) {

    init {
        updateStatus(STATUS_VALID)
    }

    override fun onProcess(): Boolean {
        return if (SPUtils.getBoolean(Constants.SP_SHOW_COMMUNITY_HOME_VIDEO_GUIDE, true)) {
            showHomeVideoGuide(context, decorView, videoLottie)
            processNext()
            true
        } else {
            processNext()
            false
        }
    }

    companion object {
        fun showHomeVideoGuide(context: Context, decorView: FrameLayout?, videoLottie: LottieAnimationView?) {
            val guideLayoutBinding = LayoutCommunityHomeVideoGuideBinding.inflate(
                LayoutInflater.from(context),
                decorView,
                true
            )
            guideLayoutBinding.root.setOnClickListener { view ->
                decorView?.removeView(view)
                SPUtils.setBoolean(Constants.SP_SHOW_COMMUNITY_HOME_VIDEO_GUIDE, false)

                videoLottie?.playAnimation()
                SPUtils.setLong(
                    Constants.SP_COMMUNITY_HOME_VIDEO_LOTTIE_LAST_PLAY_TIME,
                    System.currentTimeMillis()
                )
            }
        }
    }
}