package com.remax.visualnovel.ui.chat


import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import com.dylanc.viewbinding.nonreflection.inflate
import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.OvershootInterpolator
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.LayoutPopIconMenuViewBinding
import com.remax.visualnovel.utils.spannablex.utils.dp


class PopMenuIconView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

    data class MenuItem(
        val iconResId: Int,
        val onClick: (View) -> Unit
    )

    private val mMenuItemList = mutableListOf<MenuItem>()
    private var isMenuShowing = false
    private var itemSpacing = 0
    private var itemSize = 0



    private var mBinding: LayoutPopIconMenuViewBinding? = null
    init {
        itemSize = 30.dp
        itemSpacing = 20.dp
        mBinding = inflate(LayoutPopIconMenuViewBinding::inflate)
        mBinding?.run {
            ivTrigger.setImageResource(R.mipmap.chat_up)
            ivTrigger.setOnClickListener { toggleMenu() }
        }
    }


    fun setMenuList(menuList : List<MenuItem>) {
        mMenuItemList.clear()
        mMenuItemList.addAll(menuList)
        updateMenuItems()
    }


    private fun updateMenuItems() {
        mBinding!!.llMenuContainer.removeAllViews()

        mMenuItemList.forEachIndexed { index, menuItem ->
            ImageView(context).apply {
                setImageResource(menuItem.iconResId)

                layoutParams = LinearLayout.LayoutParams(itemSize, itemSize).apply {
                    if (/*index < mMenuItemList.size - 1*/true) {
                        bottomMargin = itemSpacing
                    }
                }

                setOnClickListener {
                    menuItem.onClick(this)
                    dismissMenu()
                }

                mBinding!!.llMenuContainer.addView(this, 0)
            }
        }
    }

    fun toggleMenu() {
        if (isMenuShowing) dismissMenu() else showMenu()
    }

    private fun showMenu() {
        if (isMenuShowing) return
        isMenuShowing = true

        mBinding?.run {
            divider.visibility = VISIBLE
            llMenuContainer.visibility = VISIBLE
            ivTrigger.setImageResource(R.mipmap.chat_down)

            val maxWidthSpec = MeasureSpec.makeMeasureSpec(Int.MAX_VALUE/2, MeasureSpec.AT_MOST)
            val heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
            llMenuContainer.measure(maxWidthSpec, heightSpec)
            val measureHeight = llMenuContainer.measuredHeight

            val valueYAnim = ValueAnimator.ofInt(0, measureHeight).apply {
                addUpdateListener {
                    val layoutParam = llMenuContainer.layoutParams
                    layoutParam.height = it.animatedValue as Int
                    llMenuContainer.layoutParams = layoutParam
                }
            }

            AnimatorSet().apply {
                duration = 300
                interpolator = OvershootInterpolator(2f)
                playTogether(valueYAnim)
                start()
            }
        }
    }


    private fun dismissMenu() {
        if (!isMenuShowing) return
        isMenuShowing = false
        mBinding!!.ivTrigger.setImageResource(R.mipmap.chat_up)

        mBinding?.run {
            val valueYAnim = ValueAnimator.ofInt(llMenuContainer.measuredHeight, 0).apply {
                addUpdateListener {
                    var layoutParam = llMenuContainer.layoutParams
                    layoutParam.height = it.animatedValue as Int
                    llMenuContainer.layoutParams = layoutParam
                }
                start()
            }

            AnimatorSet().apply {
                duration = 300
                interpolator = AccelerateInterpolator()
                playTogether(valueYAnim)
                addListener(object: Animator.AnimatorListener{
                    override fun onAnimationCancel(animation: Animator) {
                        llMenuContainer.visibility = GONE
                        divider.visibility = GONE
                    }

                    override fun onAnimationEnd(animation: Animator) {
                        llMenuContainer.visibility = GONE
                        divider.visibility = GONE
                    }

                    override fun onAnimationRepeat(animation: Animator) {

                    }

                    override fun onAnimationStart(animation: Animator) {

                    }
                })
                start()
            }
        }
    }





}