package com.gh.gamecenter.home.custom.floatview

import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.annotation.IntDef
import androidx.core.view.ViewCompat
import androidx.core.view.ViewPropertyAnimatorListener
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.gh.gamecenter.common.utils.dip2px
import com.gh.gamecenter.common.utils.visibleIf

/**
 * 自定义页面左下角悬浮窗管理类
 *
 */
class CustomFloatViewAnimationHelper : DefaultLifecycleObserver {

    private var floatView: View? = null

    private var floatState: Int = VIEW_STATE_EXPAND

    private var isScrolling = false

    private val handler = Handler(Looper.getMainLooper())

    fun show(floatView: View) {
        this.floatView = floatView
        floatView.visibleIf(true)
    }

    fun dismiss() {
        floatView?.visibleIf(false)
    }

    fun resume() {
        floatView?.visibleIf(true)
    }

    // 页面滚动状态变化
    fun onScrollChanged(isScrolling: Boolean) {
        this.isScrolling = isScrolling
        handler.removeCallbacksAndMessages(null)
        if (isScrolling) {
            if (floatState == VIEW_STATE_EXPAND) {
                // 如果悬浮窗处于展开状态，则立即执行收起动画
                collapse()
            }
        } else {
            if (floatState == VIEW_STATE_COLLAPSE || floatState == VIEW_STATE_COLLAPSING) {
                expandDelay()
            }
        }
    }


    private fun expandDelay() {
        handler.postDelayed({
            expand()
        }, EXPAND_DELAY)
    }

    private fun expand() {
        floatView?.startAnimation(true)
    }

    private fun collapse() {
        floatView?.startAnimation(false)
    }

    private fun View.startAnimation(isExpand: Boolean) {
        val startState = if (isExpand) VIEW_STATE_EXPANDING else VIEW_STATE_COLLAPSING
        val endState = if (isExpand) VIEW_STATE_EXPAND else VIEW_STATE_COLLAPSE
        val alpha = if (isExpand) 1F else 0.6F
        val translationX = if (isExpand) 0F else (width + 8F.dip2px()) / 2F

        ViewCompat.animate(this)
            .setListener(object : ViewPropertyAnimatorListener {
                override fun onAnimationStart(view: View) {
                    floatState = startState
                }

                override fun onAnimationEnd(view: View) {
                    floatState = endState
                    if (isExpand && isScrolling) {
                        // 执行展开动画之后，发现页面仍在滚动，则立马执行收起动画
                        collapse()
                    }
                }

                override fun onAnimationCancel(view: View) = Unit
            })
            .alpha(alpha)
            .translationX(translationX)
            .setDuration(SHRINK_AND_EXPAND_ANIMATION_DURATION)
            .start()
    }

    override fun onDestroy(owner: LifecycleOwner) {
        super.onDestroy(owner)
        handler.removeCallbacksAndMessages(null)
        floatView?.animate()?.cancel()
        floatView = null
    }

    companion object {

        // 外界停止滚动
        const val SCROLL_STATE_IDLE = 0

        // 外界正在滚动
        const val SCROLL_STATE_SCROLLING = 1

        @IntDef(SCROLL_STATE_IDLE, SCROLL_STATE_SCROLLING)
        @Retention(AnnotationRetention.SOURCE)
        annotation class ScrollState

        // 悬浮的View当前的状态
        // 展开状态
        private const val VIEW_STATE_EXPAND = 0

        // 正在执行动画  展开 ->收起  或  收起 -> 展开
        private const val VIEW_STATE_EXPANDING = 1

        // 正在执行动画  收起 -> expand -> collapsing
        private const val VIEW_STATE_COLLAPSING = 2

        //  收起状态
        private const val VIEW_STATE_COLLAPSE = 3


        private const val SHRINK_AND_EXPAND_ANIMATION_DURATION = 300L
        private const val EXPAND_DELAY = 1000L

    }
}