package com.gh.ndownload.suspendwindow.window

import android.animation.Animator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Rect
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import com.gh.ndownload.suspendwindow.view.NDownloadSuspendCloseLayout

/**
 * 关闭图标窗口
 */
class NDownloadSuspendCloseWindow(context: Context, suspend: Boolean = true) :
    NDownloadSuspendWindow<NDownloadSuspendCloseLayout>(context, suspend) {

    private val popupAnimator = ValueAnimator
        .ofFloat(0F, 1F)
        .setDuration(300L)

    private val scaleAnimator = ValueAnimator
        .ofFloat(1F, 1.12F)
        .setDuration(100L)

    private var scaleUp: Boolean = false

    private val hiddenListener by lazy {
        object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) = Unit
            override fun onAnimationCancel(animation: Animator) = Unit
            override fun onAnimationRepeat(animation: Animator) = Unit
            override fun onAnimationEnd(animation: Animator) {
                root.visibility = View.GONE
            }
        }
    }

    private val shownListener by lazy {
        object : Animator.AnimatorListener {
            override fun onAnimationStart(animation: Animator) {
                root.visibility = View.VISIBLE
            }

            override fun onAnimationCancel(animation: Animator) = Unit
            override fun onAnimationRepeat(animation: Animator) = Unit
            override fun onAnimationEnd(animation: Animator) = Unit
        }
    }

    @SuppressLint("InflateParams")
    override fun onCreateView(context: Context): NDownloadSuspendCloseLayout =
        NDownloadSuspendCloseLayout(context).also {
            it.visibility = View.GONE
        }

    override fun onDetach() {
        popupAnimator.removeListener(hiddenListener)
        popupAnimator.removeListener(shownListener)
        popupAnimator.cancel()
        root.visibility = View.GONE
    }

    override fun onViewCreated(view: NDownloadSuspendCloseLayout) {
        view.apply {
            popupAnimator.addUpdateListener { value ->
                translationY = (value.animatedValue as Float) * height
            }
        }
        view.close.apply {
            scaleAnimator.addUpdateListener {
                scaleX = (it.animatedValue as Float)
                scaleY = (it.animatedValue as Float)
            }
        }
    }

    override fun onLayoutParamsCreated(layoutParams: WindowManager.LayoutParams) {
        layoutParams.apply {
            gravity = Gravity.BOTTOM
            width = WindowManager.LayoutParams.MATCH_PARENT
            height = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                200F,
                context.resources.displayMetrics
            ).toInt()
        }
    }

    fun show() {
        popupAnimator.removeListener(hiddenListener)
        popupAnimator.addListener(shownListener)
        popupAnimator.reverse()
    }

    fun hide() {
        popupAnimator.removeListener(shownListener)
        popupAnimator.addListener(hiddenListener)
        popupAnimator.start()
    }

    fun scaleUp() {
        if (scaleUp) return

        scaleUp = true
        scaleAnimator.start()
    }

    fun scaleDown() {
        if (!scaleUp) return

        scaleAnimator.reverse()
        scaleUp = false
    }

    fun checkTouchPoint(x: Float, y: Float): Boolean {
        val rect = Rect()
        root.close.getDrawingRect(rect)
        val location = IntArray(2)
        root.close.getLocationOnScreen(location)
        rect.left = location[0]
        rect.top = location[1]
        rect.right += location[0]
        rect.bottom += location[1]
        return rect.contains(x.toInt(), y.toInt())
    }
}