package com.lukouguoji.gjj.holder

import android.graphics.Color
import android.view.View
import com.lukouguoji.gjj.R
import com.lukouguoji.gjj.databinding.ItemIntImpManifestBinding
import com.lukouguoji.module_base.adapter.setCommonAdapter
import com.lukouguoji.module_base.base.BaseViewHolder
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.bean.GjjManifest
import com.lukouguoji.module_base.ktx.refresh

/**
 * 国际进港舱单 ViewHolder
 */
class IntImpManifestViewHolder(view: View) :
    BaseViewHolder<GjjManifest, ItemIntImpManifestBinding>(view) {

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

        // 选中图标点击 - 切换选择状态（联动子列表）
        binding.ivIcon.setOnClickListener {
            val newCheckedState = !bean.checked.get()
            bean.checked.set(newCheckedState)
            FlowBus.with<String>(ConstantEvent.EVENT_CHECK_CHANGED).tryEmit("check_changed")
            binding.executePendingBindings()
        }

        // 整卡点击 - 跳转详情页
        notifyItemClick(position, binding.llContent)

        // 编辑按钮点击
        binding.btnEdit.setOnClickListener {
            clickListener?.onItemClick(position, 101)  // 101=编辑
        }

        // 删除按钮点击
        binding.btnDelete.setOnClickListener {
            clickListener?.onItemClick(position, 102)  // 102=删除
        }

        // 展开按钮颜色：绿色背景时黑色，否则保持原色
        if (bean.ref == "1") {
            binding.ivShow.setColorFilter(Color.parseColor("#666666"))
        } else {
            binding.ivShow.clearColorFilter()
        }

        // 展开按钮点击事件 - 先加载分单数据，再切换展开状态
        binding.ivShow.setOnClickListener {
            if (!bean.showMore.get() && bean.haWbList.isNullOrEmpty()) {
                // 首次展开且无数据，通知 ViewModel 加载分单
                clickListener?.onItemClick(position, 103)  // 103=加载分单
            } else {
                bean.showMore.set(!bean.showMore.get())
            }
        }

        // 初始化分单子列表 RecyclerView
        setCommonAdapter(
            binding.rvSub,
            IntImpManifestSubViewHolder::class.java,
            R.layout.item_int_imp_manifest_sub
        )

        // 设置父Bean引用（用于子列表反向联动）
        binding.rvSub.tag = bean
        val subList = bean.haWbList ?: emptyList()
        binding.rvSub.refresh(subList)
        updateSubListVisibility(bean.haWbList != null, subList.isNotEmpty())
    }

    /**
     * 根据分单数据状态，切换表头/列表与空数据提示的显隐
     * @param loaded 是否已加载过数据（haWbList != null）
     * @param hasData 是否有数据
     */
    private fun updateSubListVisibility(loaded: Boolean, hasData: Boolean) {
        if (!loaded || hasData) {
            // 未加载或有数据：显示表头+列表，隐藏空提示
            binding.llHeader.visibility = View.VISIBLE
            binding.dividerHeader.visibility = View.VISIBLE
            binding.rvSub.visibility = View.VISIBLE
            binding.tvEmpty.visibility = View.GONE
        } else {
            // 已加载但无数据：隐藏表头+列表，显示空提示
            binding.llHeader.visibility = View.GONE
            binding.dividerHeader.visibility = View.GONE
            binding.rvSub.visibility = View.GONE
            binding.tvEmpty.visibility = View.VISIBLE
        }
    }
}
