package com.lukouguoji.gjj.holder

import android.view.View
import com.lukouguoji.gjj.R
import com.lukouguoji.gjj.activity.IntImpQueryDetailsActivity
import com.lukouguoji.gjj.activity.IntImpStorageUseActivity
import com.lukouguoji.gjj.databinding.ItemIntImpStorageUseBinding
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.GjcMaWb
import com.lukouguoji.module_base.ktx.refresh

/**
 * 国际进港-仓库 ViewHolder
 *
 * 双形态布局：平板变体含 iv_icon/iv_show/rv_sub/ll_header/divider_header/tv_empty
 * （多选 + 库位子列表），手机变体含 ll/btn_clear/btn_modify/btn_out/btn_in
 * （整卡进详情 + 卡片级四操作按钮）。单侧独有的 id 在 Binding 基类中为 @Nullable，必须判空访问。
 */
class IntImpStorageUseViewHolder(view: View) :
    BaseViewHolder<GjcMaWb, ItemIntImpStorageUseBinding>(view) {

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

        // ==================== 平板：多选 + 子列表 ====================

        // 图标点击切换选择状态
        binding.ivIcon?.setOnClickListener {
            bean.checked.set(!bean.checked.get())
            FlowBus.with<String>(ConstantEvent.EVENT_CHECK_CHANGED).tryEmit("check_changed")
            binding.executePendingBindings()
        }

        // 展开按钮点击事件
        binding.ivShow?.setOnClickListener {
            bean.showMore.set(!bean.showMore.get())
        }

        // 初始化库位明细子列表 RecyclerView
        binding.rvSub?.let { rvSub ->
            setCommonAdapter(
                rvSub,
                IntImpStorageUseSubViewHolder::class.java,
                R.layout.item_int_imp_storage_use_sub
            )

            // 刷新库位明细数据（传递父Bean引用）
            val storageUseList = bean.storageUseList ?: emptyList()
            rvSub.tag = bean
            rvSub.refresh(storageUseList)
            updateSubListVisibility(storageUseList.isNotEmpty())
        }

        // ==================== 手机：整卡进详情 + 卡片级操作 ====================

        val activity = itemView.context as? IntImpStorageUseActivity

        // 整卡点击进详情（复用进港查询详情页，3 Tab 运单/仓库/库位信息）
        binding.ll?.setOnClickListener {
            IntImpQueryDetailsActivity.start(itemView.context, bean.prefix, bean.no)
        }

        // 清仓（已清仓时置灰不可点，样式由 XML 按 bean.isCleared 切换）
        binding.btnClear?.setOnClickListener {
            if (!bean.isCleared) activity?.cardClear(bean)
        }

        binding.btnModify?.setOnClickListener { activity?.cardModifyStorage(bean) }
        binding.btnOut?.setOnClickListener { activity?.cardOutStorage(bean) }
        binding.btnIn?.setOnClickListener { activity?.cardInStorage(bean) }
    }

    private fun updateSubListVisibility(hasData: Boolean) {
        binding.llHeader?.visibility = if (hasData) View.VISIBLE else View.GONE
        binding.dividerHeader?.visibility = if (hasData) View.VISIBLE else View.GONE
        binding.rvSub?.visibility = if (hasData) View.VISIBLE else View.GONE
        binding.tvEmpty?.visibility = if (hasData) View.GONE else View.VISIBLE
    }
}
