package com.remax.visualnovel.ui.chat.ui.expandableSelector

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.LinearLayout
import android.widget.TextView
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.LayoutExpandSelectViewBinding
import com.remax.visualnovel.databinding.LayoutSettingSoundSubViewBinding
import com.remax.visualnovel.entity.response.ChatSound
import com.remax.visualnovel.utils.spannablex.utils.dp


class ExpandSoundSelectView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private lateinit var mBinding: LayoutExpandSelectViewBinding
    private lateinit var mExpandView : ExpandSoundSubView

    private var isExpanded = false
    private var animationDuration = 300
    private var itemSelectedListener: OnItemSelectedListener? = null

    init {
        initView(context, attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?) {
        mBinding = LayoutExpandSelectViewBinding.inflate(LayoutInflater.from(context), this, true)

        mExpandView = ExpandSoundSubView(context)
        mBinding.itemsContainer.addView(mExpandView, LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT))
        setTitleText(R.string.title_sound_actor)
        setTitleIcon(R.mipmap.setting_sound_icon)
        setupClickListeners()
    }

    private fun setupClickListeners() {
        mBinding.titleLayout.setOnClickListener { toggle() }
    }


    fun setTitleIcon(resId: Int) {
        mBinding.icon.setImageResource(resId)
    }

    fun setTitleText(titleRes: Int) {
        mBinding.titleText.text = context.resources.getString(titleRes)
    }

    fun setItems(newItems: List<ChatSound>) {
        mExpandView.setItems(newItems)
    }


    fun toggle() {
        if (isExpanded) collapse() else expand()
    }

    fun expand() {
        if (isExpanded) return

        isExpanded = true
        mBinding.itemsContainer.visibility = View.VISIBLE
        animateArrow(0f, 90f)
        // param height anim
        val animator = ValueAnimator.ofInt(0, getItemsHeight())
        animator.duration = animationDuration.toLong()
        animator.interpolator = AccelerateDecelerateInterpolator()
        animator.addUpdateListener { animation ->
            val value = animation.animatedValue as Int
            val params = mBinding.itemsContainer.layoutParams
            params.height = value
            mBinding.itemsContainer.layoutParams = params
        }
        animator.start()
    }


    fun collapse() {
        if (!isExpanded) return

        isExpanded = false
        animateArrow(90f, 0f)
        // param height anim
        val animator = ValueAnimator.ofInt(getItemsHeight(), 0)
        animator.duration = animationDuration.toLong()
        animator.interpolator = AccelerateDecelerateInterpolator()
        animator.addUpdateListener { animation ->
            val value = animation.animatedValue as Int
            val params = mBinding.itemsContainer.layoutParams
            params.height = value
            mBinding.itemsContainer.layoutParams = params
        }
        animator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                mBinding.itemsContainer.visibility = View.GONE
            }
        })
        animator.start()
    }

    private fun animateArrow(from: Float, to: Float) {
        val rotation = ObjectAnimator.ofFloat(mBinding.arrow, "rotation", from, to)
        rotation.duration = animationDuration.toLong()
        rotation.interpolator = AccelerateDecelerateInterpolator()
        rotation.start()
    }

    private fun getItemsHeight(): Int {
        var height = 0
        for (i in 0 until mBinding.itemsContainer.childCount) {
            val child = mBinding.itemsContainer.getChildAt(i)
            child.measure(
                MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
            )
            height += child.measuredHeight
        }
        return height
    }

    fun setOnItemSelectedListener(listener: OnItemSelectedListener) {
        this.itemSelectedListener = listener
    }

    interface OnItemSelectedListener {
        fun onItemSelected(position: Int, item: SelectorItem)
    }
}