package com.gh.gamecenter.common.view.cropbox

import android.animation.ValueAnimator
import android.graphics.Color
import android.os.Handler
import android.os.Looper
import android.view.animation.AccelerateDecelerateInterpolator

class CropBoxAnimator(private val block: (Int) -> Unit) {

    private var currentState: AnimatorState = AnimatorState.Default

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


    private val valueAnimator by lazy {
        val startColor = Color.parseColor("#CC000000")
        val endColor = Color.parseColor("#33000000")
        ValueAnimator.ofArgb(startColor, endColor)
            .setDuration(ANIMATOR_DURATION)
            .apply {
                interpolator = AccelerateDecelerateInterpolator()
                addUpdateListener { animator ->
                    val animatedColor = animator.animatedValue as Int
                    block(animatedColor)
                    val time = animator.currentPlayTime
                    if (time >= ANIMATOR_DURATION) {
                        if (currentState is AnimatorState.Playing) {
                            currentState = AnimatorState.Dragging
                        }
                        if (currentState is AnimatorState.Reversing) {
                            currentState = AnimatorState.Default
                        }
                    }
                }

            }
    }

    private val task = Runnable {
        // 延时结束，执行反向渐变动画
        currentState = AnimatorState.Reversing
        valueAnimator.reverse()
    }


    fun changeDragState(isDragging: Boolean) {
        when (currentState) {
            is AnimatorState.Default -> {
                // 执行正向渐变动画
                if (isDragging) {
                    currentState = AnimatorState.Playing
                    valueAnimator.start()
                }
            }

            is AnimatorState.Playing -> {
                if (!isDragging) {
                    currentState = AnimatorState.Delay
                    handler.removeCallbacksAndMessages(null)
                    handler.postDelayed(task, DELAY_DURATION)
                }
            }

            is AnimatorState.Dragging -> {
                if (!isDragging) {
                    currentState = AnimatorState.Delay
                    handler.removeCallbacksAndMessages(null)
                    handler.postDelayed(task, DELAY_DURATION)
                }
            }

            is AnimatorState.Delay -> {
                if (isDragging) {
                    currentState = AnimatorState.Dragging
                    handler.removeCallbacksAndMessages(null)
                }
            }

            is AnimatorState.Reversing -> {
                if (isDragging) {
                    currentState = AnimatorState.Playing
                    valueAnimator.reverse()
                }
            }

        }
    }

    companion object {
        private const val ANIMATOR_DURATION = 200L
        private const val DELAY_DURATION = 500L
    }

    sealed class AnimatorState {
        object Default : AnimatorState()
        object Playing : AnimatorState()
        object Dragging : AnimatorState()
        object Delay : AnimatorState()
        object Reversing : AnimatorState()
    }
}