package com.lukouguoji.gjc.viewModel

import android.view.View
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.holder.GjcUnLoadViewHolder
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.GjcUnLoadListBean
import com.lukouguoji.module_base.bean.GjcWaybillBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.common.ConstantEvent
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.impl.FlowBus
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.finish
import com.lukouguoji.module_base.ktx.getActivity
import com.lukouguoji.module_base.ktx.launchCollect
import com.lukouguoji.module_base.ktx.loge
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.ktx.toRequestBody
import com.lukouguoji.module_base.model.ScanModel
import com.lukouguoji.module_base.util.CheckUtil
import com.lukouguoji.module_base.util.DictUtils
import dev.utils.app.info.KeyValue

class GjcUnLoadListViewModel : BasePageViewModel() {

    // 目的港
    val destPort = MutableLiveData("")

    // 代理
    val agent = MutableLiveData("")

    // 特码
    val specialCode = MutableLiveData("")

    // 运单号
    val waybillNo = MutableLiveData("")

    val itemLayoutId = R.layout.item_gjc_un_load
    val itemViewHolder = GjcUnLoadViewHolder::class.java

    val agentList = MutableLiveData<List<KeyValue>>(emptyList())
    val specialCodeList = MutableLiveData<List<KeyValue>>(emptyList())

    // 选中条数
    val count = MutableLiveData(0)

    init {
        DictUtils.getAgentList {
            agentList.postValue(it)
        }
        DictUtils.getSpecialCodeList(flag = 1) {
            specialCodeList.postValue(it)
        }
    }

    /**
     * 获取列表数据
     */
    override fun getData() {
        launchCollect({
            NetApply.api
                .getGjcUnloadWaybillList(
                    mapOf(
                        "dest" to destPort.value,
                        "agent" to agent.value,
                        "spCode" to specialCode.value,
                        "wbNo" to waybillNo.value,
                    ).toRequestBody()
                )
        }) {
            onSuccess = {
                loge("获取列表数据成功 ${it.data?.size}")
                pageModel.handleDataList(it.data)
            }

            onComplete = {
                pageModel.finishLoading()
            }
        }
    }

    /**
     * 搜索点击
     */
    fun searchClick(view: View) {
        refresh()
    }

    /**
     * 运单号 扫描
     */
    fun waybillNoScanClick(view: View) {
        ScanModel.startScan(view.context.getActivity(), Constant.RequestCode.WAYBILL)
    }

    /**
     * 按钮 点击
     */
    fun btnClick(view: View) {
        val list =
            (pageModel.rv?.commonAdapter()?.items ?: emptyList()) as List<GjcWaybillBean>
        val checkedList = list.filter { it.checked.get() }
        if (checkedList.isEmpty()) {
            showToast("请先选择运单")
            return
        }
        FlowBus.with<List<GjcWaybillBean>>(ConstantEvent.GJC_UNLOAD_LIST_CHECKED)
            .tryEmit(checkedList)
        view.context.finish()
    }

    /**
     * 代理 选中
     */
    fun agentSelected(position: Int) {
        agent.value = agentList.value?.get(position)?.value
    }

    /**
     * 特码 选中
     */
    fun specialCodeSelected(position: Int) {
        specialCode.value = specialCodeList.value?.get(position)?.value
    }

    /**
     * 全选 点击
     */
    fun checkAllClick(view: View) {
        CheckUtil.handleAllCheck(
            pageModel.rv?.commonAdapter()?.items as? List<GjcWaybillBean> ?: emptyList()
        )
        computeSelectCount()
    }

    /**
     * item 点击
     */
    override fun onItemClick(position: Int, type: Int) {
        when (type) {
            R.id.iv_icon -> {
                pageModel.rv?.commonAdapter()?.getItem(position)?.let { bean ->
                    (bean as GjcWaybillBean).checked.set(!bean.checked.get())
                }
                computeSelectCount()
            }

            else -> {}
        }
    }

    private fun computeSelectCount() {
        val list =
            (pageModel.rv?.commonAdapter()?.items ?: emptyList()) as List<GjcWaybillBean>
        count.postValue(list.filter { it.checked.get() }.size)
    }

}