package com.lukouguoji.gnj.page.yiku.handover

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.lukouguoji.gnj.R
import com.lukouguoji.gnj.databinding.ActivityGnjYikuHandoverBinding
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.DetailsPageType
import com.lukouguoji.module_base.ktx.addOnItemClickListener
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.router.ARouterConstants

/**
 * 国内进港移交编辑/详情页
 */
@Route(path = ARouterConstants.ACTIVITY_URL_GNJ_YIKU_HANDOVER)
class GnjYiKuHandoverActivity :
    BaseBindingActivity<ActivityGnjYikuHandoverBinding, GnjYiKuHandoverViewModel>() {

    override fun layoutId() = R.layout.activity_gnj_yiku_handover

    override fun viewModelClass() = GnjYiKuHandoverViewModel::class.java

    override fun initOnCreate(savedInstanceState: Bundle?) {
        viewModel.initOnCreated(intent)

        when (viewModel.pageType.value) {
            DetailsPageType.Modify -> setBackArrow("国内进港移交编辑")
            DetailsPageType.Details -> setBackArrow("国内进港移交详情")
            else -> setBackArrow("国内进港移交")
        }

        binding.viewModel = viewModel

        // 绑定图片 RecyclerView 引用
        viewModel.rvImages = binding.rvImages

        // 绑定图片列表点击事件（编辑模式下可删除图片）
        binding.rvImages.addOnItemClickListener(viewModel)

        // 监听图片列表变化并更新 adapter
        viewModel.imageList.observe(this) { images ->
            binding.rvImages.commonAdapter()?.refresh(images)
        }
    }

    companion object {
        @JvmStatic
        fun startForEdit(context: Context, id: String) {
            val starter = Intent(context, GnjYiKuHandoverActivity::class.java)
                .putExtra(Constant.Key.PAGE_TYPE, DetailsPageType.Modify.name)
                .putExtra(Constant.Key.ID, id)
            context.startActivity(starter)
        }

        @JvmStatic
        fun startForDetails(context: Context, id: String) {
            val starter = Intent(context, GnjYiKuHandoverActivity::class.java)
                .putExtra(Constant.Key.PAGE_TYPE, DetailsPageType.Details.name)
                .putExtra(Constant.Key.ID, id)
            context.startActivity(starter)
        }
    }
}
