package com.lukouguoji.gjc.holder

import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.Transformation
import androidx.recyclerview.widget.LinearLayoutManager
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.ItemIntExpAssembleUldBinding
import com.lukouguoji.module_base.base.BaseViewHolder
import com.lukouguoji.module_base.base.CommonAdapter
import com.lukouguoji.module_base.bean.GjcUldUseBean
import com.lukouguoji.module_base.bean.GjcWarehouse

/**
 * 国际出港组装-ULD列表ViewHolder
 * 支持展开/收起运单明细
 */
class IntExpAssembleViewHolder(view: View) :
    BaseViewHolder<GjcUldUseBean, ItemIntExpAssembleUldBinding>(view) {

    private var waybillAdapter: CommonAdapter? = null

    override fun onBind(item: Any?, position: Int) {
        val bean = getItemBean(item) ?: return
        binding.bean = bean
        binding.position = position

        // 初始化运单明细子列表
        setupWaybillRecyclerView(bean)

        // 更新展开状态（初次绑定不需要动画）
        updateExpandState(bean, animate = false)

        // 点击整个item切换选中状态
        binding.root.setOnClickListener {
            bean.isSelected = !bean.isSelected
        }

        // 展开按钮点击事件
        binding.btnExpand.setOnClickListener {
            clickListener?.onItemClick(position, 1000)  // type=1000表示展开操作
        }

        binding.executePendingBindings()
    }

    /**
     * 初始化运单明细RecyclerView
     */
    private fun setupWaybillRecyclerView(bean: GjcUldUseBean) {
        if (binding.rvWaybillDetails.adapter == null) {
            binding.rvWaybillDetails.layoutManager = LinearLayoutManager(itemView.context)
            waybillAdapter = CommonAdapter(
                itemView.context,
                R.layout.item_assemble_waybill_detail,
                AssembleWaybillDetailViewHolder::class.java
            )
            binding.rvWaybillDetails.adapter = waybillAdapter
        }

        // 刷新运单明细数据
        waybillAdapter?.refresh(bean.waybillDetails ?: mutableListOf())
    }

    /**
     * 更新展开状态
     * @param bean ULD数据
     * @param animate 是否显示动画
     */
    private fun updateExpandState(bean: GjcUldUseBean, animate: Boolean = true) {
        if (bean.isExpanded) {
            // 展开
            if (animate) {
                expand(binding.llWaybillDetails)
            } else {
                binding.llWaybillDetails.visibility = View.VISIBLE
            }
            binding.btnExpand.text = "收起"
        } else {
            // 收起
            if (animate) {
                collapse(binding.llWaybillDetails)
            } else {
                binding.llWaybillDetails.visibility = View.GONE
            }
            binding.btnExpand.text = "展开"
        }
    }

    /**
     * 展开动画（高度从0到wrap_content）
     */
    private fun expand(view: View) {
        // 测量目标高度
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
        val targetHeight = view.measuredHeight

        // 初始高度设为0
        view.layoutParams.height = 0
        view.visibility = View.VISIBLE

        val animation = object : Animation() {
            override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
                view.layoutParams.height = if (interpolatedTime == 1f) {
                    ViewGroup.LayoutParams.WRAP_CONTENT
                } else {
                    (targetHeight * interpolatedTime).toInt()
                }
                view.requestLayout()
            }

            override fun willChangeBounds() = true
        }

        animation.duration = 300  // 动画时长300ms
        view.startAnimation(animation)
    }

    /**
     * 收起动画（高度从当前到0）
     */
    private fun collapse(view: View) {
        val initialHeight = view.measuredHeight

        val animation = object : Animation() {
            override fun applyTransformation(interpolatedTime: Float, t: Transformation?) {
                if (interpolatedTime == 1f) {
                    view.visibility = View.GONE
                } else {
                    view.layoutParams.height = initialHeight - (initialHeight * interpolatedTime).toInt()
                    view.requestLayout()
                }
            }

            override fun willChangeBounds() = true
        }

        animation.duration = 300  // 动画时长300ms
        view.startAnimation(animation)
    }
}
