package com.sixin.module_p.ui.gnc.shouyun.add

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.lukouguoji.module_base.adapter.bindAdapter
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.ktx.loge
import com.lukouguoji.module_base.ktx.toJson
import com.sixin.module_p.R
import com.sixin.module_p.base.BasePdaActivity
import com.sixin.module_p.databinding.ActivityPdaShouYunAddBinding
import dev.utils.app.info.KeyValue

class PdaShouYunAddActivity :
    BasePdaActivity<ActivityPdaShouYunAddBinding, PdaShouYunAddViewModel>() {

    override fun layoutId(): Int {
        return R.layout.activity_pda_shou_yun_add
    }

    override fun viewModelClass(): Class<PdaShouYunAddViewModel> {
        return PdaShouYunAddViewModel::class.java
    }

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow("国内出港开始收运")
        binding.viewModel = viewModel

        viewModel.init(intent)

        viewModel.dataBean.observe(this) {
            onInitDataChanged()
        }
        viewModel.agentList.observe(this) {
            bindAdapter(binding.spinnerAgent, it)
            binding.spinnerAgent.setSelection(getIndex(viewModel.dataBean.value!!.agentCode, it))
        }
        viewModel.spCodeList.observe(this) {
            bindAdapter(binding.spinnerSpCode, it)
            binding.spinnerSpCode.setSelection(getIndex(viewModel.dataBean.value!!.spCode, it))
        }
        viewModel.businessTypeList.observe(this) {
            bindAdapter(binding.spinnerBusinessType, it)
            binding.spinnerBusinessType
                .setSelection(getIndex(viewModel.dataBean.value!!.businessType, it))
        }
        viewModel.dangerousGoodsList.observe(this) {
            bindAdapter(binding.spinnerWxku, it)
            binding.spinnerWxku.setSelection(getIndex(viewModel.dataBean.value!!.dgrLocation, it))
        }
        viewModel.dangerousGoodsDescList.observe(this) {
            bindAdapter(binding.spinnerWxdesc, it)
            binding.spinnerWxdesc.setSelection(getIndex(viewModel.dataBean.value!!.dgrDetail, it))
        }
        viewModel.packageTypeList.observe(this) {
            bindAdapter(binding.spinnerPackageType, it)
            binding.spinnerPackageType.setSelection(getIndex(viewModel.dataBean.value!!.packageType, it))
        }
    }

    private fun onInitDataChanged() {
        viewModel.dataBean.value?.let {
            binding.spinnerAgent.setSelection(getIndex(it.agentCode, viewModel.agentList.value!!))
            binding.spinnerSpCode.setSelection(getIndex(it.spCode, viewModel.spCodeList.value!!))
            binding.spinnerBusinessType.setSelection(
                getIndex(
                    it.businessType,
                    viewModel.businessTypeList.value!!
                )
            )
            binding.spinnerWxku.setSelection(
                getIndex(
                    it.dgrLocation,
                    viewModel.dangerousGoodsList.value!!
                )
            )
            binding.spinnerWxdesc.setSelection(
                getIndex(
                    it.dgrDetail,
                    viewModel.dangerousGoodsDescList.value!!
                )
            )
            binding.spinnerPackageType.setSelection(
                getIndex(
                    it.packageType,
                    viewModel.packageTypeList.value!!
                )
            )
        }
    }

    private fun getIndex(data: String, list: List<KeyValue>): Int {
        val index = list.indexOfFirst { it.value == data }.coerceAtLeast(0)
        return index
    }

    companion object {
        @JvmStatic
        fun start(context: Context, waybill: String = "") {
            val starter = Intent(context, PdaShouYunAddActivity::class.java)
                .putExtra(Constant.Key.WAYBILL, waybill)
            context.startActivity(starter)
        }
    }

}