package com.ama.core.common.widget


import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
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.TextView
import com.ama.core.common.R
import com.ama.core.common.databinding.LayoutPopIconMenuViewBinding
import com.ama.core.common.util.dp


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

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

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




    private var mBinding: LayoutPopIconMenuViewBinding? = null
    init {
        itemSpacing = 20.dp
        mBinding = LayoutPopIconMenuViewBinding.inflate(LayoutInflater.from(context), this, true)
        mBinding?.run {
            ivTrigger.setImageResource(R.mipmap.home_expand)
            ivTrigger.setOnClickListener { toggleMenu() }
        }
    }


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


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

        mMenuItemList.asReversed().forEachIndexed { index, menuItem ->
            val subContainer = FrameLayout(context).apply {
                setOnClickListener {
                    menuItem.onClick(this)
                }

                layoutParams = FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
                    bottomMargin = itemSpacing
                    gravity = Gravity.CENTER_HORIZONTAL
                    topMargin = 10
                }
                val itemView = PopMenuItemView(context).apply {
                    setData(menuItem)
                }

                mItemViewList.add(itemView)
                addView(itemView)
            }
            mBinding!!.llMenuContainer.addView(subContainer, 0)
        }
    }

    fun setMenuText(menuIndex: Int, textContent: String) {
        mItemViewList.get(menuIndex).setText(textContent)
    }

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

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

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

            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.home_expand)

        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
                    }

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

                    override fun onAnimationRepeat(animation: Animator) {

                    }

                    override fun onAnimationStart(animation: Animator) {

                    }
                })
                start()
            }
        }
    }





}