package com.gh.common.prioritychain

import android.app.Activity
import android.os.Handler
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import com.gh.gamecenter.common.constant.Constants
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.view.BugFixedPopupWindow
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.databinding.PopupBottomTabGuideBinding
import com.gh.gamecenter.entity.BottomTab

class BottomTabGuideHandler(priority: Int): PriorityChainHandler(priority) {
    private var mActivity: Activity? = null
    private var mHandler: Handler? = null
    private var mViewPager: ViewGroup? = null
    private var mGuide: BottomTab.Guide? = null
    private var mTriangleTransX = 0F
    private var mGuideTransX = 0F
    private var mRestWidth = 0F
    private var mIsCenterPosition = false
    private var mIsRightPosition = false

    private var mPopupWindow: PopupWindow? = null

    fun doPreProcess(
        shouldShow: Boolean,
        activity: Activity? = null,
        handler: Handler? = null,
        viewPager: ViewGroup? = null,
        guide: BottomTab.Guide? = null,
        triangleTransX: Float = 0F,
        guideTransX: Float = 0F,
        restWidth: Float = 0F,
        isCenterPosition: Boolean = false,
        isRightPosition: Boolean = false
    ) {
        mActivity = activity
        mHandler = handler
        mViewPager = viewPager
        mGuide = guide
        mTriangleTransX = triangleTransX
        mGuideTransX = guideTransX
        mRestWidth = restWidth
        mIsCenterPosition = isCenterPosition
        mIsRightPosition = isRightPosition

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

    override fun onProcess(): Boolean {
        when (getStatus()) {
            STATUS_VALID -> {
                if (mGuide == null || mActivity == null || mViewPager == null) {
                    processNext()
                    return false
                }

                val guideSet = HashSet(SPUtils.getStringSet(Constants.SP_BOTTOM_TAB_GUIDE_SET))
                if (!guideSet.contains(mGuide?.bottomTabId + mGuide?.text)) {
                    mPopupWindow = showBottomTabPopupWindow(
                        mActivity!!,
                        mHandler,
                        mViewPager!!,
                        mGuide!!,
                        mTriangleTransX,
                        mGuideTransX,
                        mRestWidth,
                        mIsCenterPosition,
                        mIsRightPosition
                    ) {
                        processNext()
                    }
                    guideSet.add(mGuide?.bottomTabId + mGuide?.text)
                    SPUtils.setStringSet(Constants.SP_BOTTOM_TAB_GUIDE_SET, guideSet)
                    return true
                } else {
                    processNext()
                }
            }

            STATUS_INVALID -> {
                processNext()
            }
        }

        return false
    }

    fun dismiss() {
        if (mPopupWindow?.isShowing == true) {
            mPopupWindow?.dismiss()
        }
    }

    companion object {
        const val KEY_BOTTOM_TAB_GUIDE_AUTO_DISMISS = 100
        private const val AUTO_DISMISS_DELAY_TIME = 5000L

        fun showBottomTabPopupWindow(
            activity: Activity,
            handler: Handler?,
            viewPager: ViewGroup,
            guide: BottomTab.Guide,
            triangleTransX: Float,
            guideTransX: Float,
            restWidth: Float,
            isCenterPosition: Boolean,
            isRightPosition: Boolean,
            dismissCallback: (() -> Unit)?
        ): PopupWindow {
            val binding = PopupBottomTabGuideBinding.inflate(LayoutInflater.from(activity))
            binding.guideTv.text = guide.text
            binding.guideTriangleIv.translationX = triangleTransX
            binding.guideTv.translationX = guideTransX
            binding.guideIconIv.translationX = guideTransX
            binding.guideTv.updateLayoutParams<ConstraintLayout.LayoutParams> {
                horizontalBias = if (isCenterPosition) 0.5F else if (isRightPosition) 1F else 0F
            }

            val popupWindow = BugFixedPopupWindow(
                binding.root,
                ConstraintLayout.LayoutParams.MATCH_PARENT,
                ConstraintLayout.LayoutParams.WRAP_CONTENT
            )
            popupWindow.contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
            val measureWidth = popupWindow.contentView.measuredWidth
            val measureHeight = popupWindow.contentView.measuredHeight
            if (measureWidth > restWidth) {
                binding.guideTv.translationX = 0F
                binding.guideIconIv.translationX = 0F
            }

            return popupWindow.apply {
                isFocusable = false
                isOutsideTouchable = false
                animationStyle = com.gh.gamecenter.common.R.style.popup_window_ease_in_and_out_anim_style
                setOnDismissListener {
                    dismissCallback?.invoke()
                }
                handler?.post {
                    if (!activity.isFinishing) {
                        showAsDropDown(viewPager, 0, 8F.dip2px() - measureHeight)
                    }
                    handler.sendEmptyMessageDelayed(KEY_BOTTOM_TAB_GUIDE_AUTO_DISMISS, AUTO_DISMISS_DELAY_TIME)
                }
            }
        }
    }
}