package com.lukouguoji.gjj.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.lukouguoji.gjj.R
import com.lukouguoji.gjj.databinding.ActivityIntImpManifestDetailsBinding
import com.lukouguoji.gjj.holder.GjjManifestPicViewHolder
import com.lukouguoji.gjj.viewModel.IntImpManifestDetailsViewModel
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.base.CommonAdapter
import com.lukouguoji.module_base.bean.FileBean
import com.lukouguoji.module_base.bean.GjjManifest
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.router.ARouterConstants
import com.lukouguoji.module_base.util.MediaUtil
import com.lukouguoji.module_base.ktx.noNull

/**
 * 国际进港舱单详情
 */
@Route(path = ARouterConstants.ACTIVITY_URL_INT_IMP_MANIFEST_DETAILS)
class IntImpManifestDetailsActivity :
    BaseBindingActivity<ActivityIntImpManifestDetailsBinding, IntImpManifestDetailsViewModel>() {

    override fun layoutId() = R.layout.activity_int_imp_manifest_details
    override fun viewModelClass() = IntImpManifestDetailsViewModel::class.java

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow("国际进港舱单详情")
        binding.viewModel = viewModel
        viewModel.initOnCreated(intent)

        // 交接图片
        val picAdapter = CommonAdapter(
            this,
            R.layout.item_gjj_manifest_pic,
            GjjManifestPicViewHolder::class.java
        )
        binding.rvPic.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        binding.rvPic.adapter = picAdapter

        viewModel.dataBean.observe(this) { bean ->
            val picList = bean.pic.noNull().split(",").filter { it.isNotEmpty() }
            val originalPicList = bean.originalPic.noNull().split(",").filter { it.isNotEmpty() }
            val list = picList.mapIndexed { index, picUrl ->
                val originalFile = originalPicList.getOrElse(index) { picUrl }
                FileBean(
                    path = MediaUtil.fillUrl(picUrl),
                    url = picUrl,
                    originalPic = MediaUtil.fillUrl(originalFile)
                )
            }
            picAdapter.refresh(list)
        }
    }

    companion object {
        @JvmStatic
        fun start(context: Context, manifest: GjjManifest) {
            val starter = Intent(context, IntImpManifestDetailsActivity::class.java)
                .putExtra(Constant.Key.DATA, manifest)
            context.startActivity(starter)
        }
    }
}
