package com.lukouguoji.gnj.page.yiku.edit

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.ActivityGnjYikuEditBinding
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_EDIT)
class GnjYiKuEditActivity :
    BaseBindingActivity<ActivityGnjYikuEditBinding, GnjYiKuEditViewModel>() {

    override fun layoutId() = R.layout.activity_gnj_yiku_edit

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

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

        // 根据页面类型设置标题
        when (viewModel.pageType.value) {
            DetailsPageType.Add -> setBackArrow("国内进港移库新增")
            DetailsPageType.Modify -> setBackArrow("国内进港移库编辑")
            DetailsPageType.Details -> setBackArrow("国内进港移库详情")
            null -> setBackArrow("国内进港移库")
        }

        binding.viewModel = viewModel

        // 绑定图片列表点击事件
        binding.rvImages.addOnItemClickListener(viewModel)

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

    companion object {
        @JvmStatic
        fun startForAdd(context: Context) {
            val starter = Intent(context, GnjYiKuEditActivity::class.java)
                .putExtra(Constant.Key.PAGE_TYPE, DetailsPageType.Add.name)
            context.startActivity(starter)
        }

        @JvmStatic
        fun startForEdit(context: Context, id: String) {
            val starter = Intent(context, GnjYiKuEditActivity::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, GnjYiKuEditActivity::class.java)
                .putExtra(Constant.Key.PAGE_TYPE, DetailsPageType.Details.name)
                .putExtra(Constant.Key.ID, id)
            context.startActivity(starter)
        }
    }
}
