package com.lukouguoji.aerologic.page.uld.list

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.aerologic.R
import com.lukouguoji.aerologic.page.uld.edit.UldEditActivity
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.ULDBean
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.ktx.commonAdapter
import com.lukouguoji.module_base.ktx.launchLoadingCollect
import com.lukouguoji.module_base.ktx.noNull
import com.lukouguoji.module_base.ktx.showToast
import com.lukouguoji.module_base.model.ConfirmDialogModel
import com.lukouguoji.module_base.ktx.toRequestBody
import com.lukouguoji.module_base.model.ScanModel
import dev.DevUtils
import dev.utils.app.info.KeyValue

class UldListViewModel : BasePageViewModel() {

    // 搜索条件
    val status = MutableLiveData("")
    val uldSuffix = MutableLiveData("")
    val uldNo = MutableLiveData("")

    // 状态下拉列表
    val statusList = MutableLiveData<List<KeyValue>>(
        listOf(
            KeyValue("全部", ""),
            KeyValue("正常", "0"),
            KeyValue("故障", "1"),
        )
    )

    // 所属航司下拉列表（mock，后续替换为接口）
    val carrierList = MutableLiveData<List<KeyValue>>(
        listOf(
            KeyValue("全部", ""),
            KeyValue("MU - 东方航空", "MU"),
            KeyValue("CA - 国航", "CA"),
            KeyValue("CZ - 南方航空", "CZ"),
            KeyValue("HO - 吉祥航空", "HO"),
            KeyValue("FM - 上海航空", "FM"),
        )
    )

    // 合计数量
    val count = MutableLiveData(0L)

    val itemLayoutId = R.layout.item_uld_list
    val itemViewHolder = UldListViewHolder::class.java

    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getUldList(
                mapOf(
                    "pageNum" to pageModel.page,
                    "pageSize" to pageModel.limit,
                    "status" to status.value?.ifEmpty { null },
                    "uldSuffix" to uldSuffix.value?.ifEmpty { null },
                    "uld" to uldNo.value?.ifEmpty { null },
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it.toBaseListBean())
                count.value = it.total
            }
        }
    }

    fun onUldScanClick() {
        ScanModel.startScan(DevUtils.getTopActivity(), Constant.RequestCode.ULD)
    }

    fun onAddClick() {
        UldEditActivity.start(DevUtils.getTopActivity())
    }

    override fun onItemClick(position: Int, type: Int) {
        val bean = pageModel.rv?.commonAdapter()?.getItem(position) as? ULDBean ?: return
        when (type) {
            R.id.ll_content -> UldEditActivity.start(DevUtils.getTopActivity(), bean.uld)
            R.id.tv_modify -> UldEditActivity.start(DevUtils.getTopActivity(), bean.uld, true)
            R.id.tv_delete -> deleteUld(bean.uld)
            else -> {}
        }
    }

    private fun deleteUld(uld: String) {
        ConfirmDialogModel("是否确定删除该ULD记录？") {
            launchLoadingCollect({
                NetApply.api.deleteUld(uld)
            }) {
                onSuccess = {
                    showToast("删除成功")
                    refresh()
                }
            }
        }.show()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode == Activity.RESULT_OK && data != null) {
            when (requestCode) {
                Constant.RequestCode.ULD -> {
                    uldNo.value = data.getStringExtra(Constant.Result.CODED_CONTENT).noNull()
                }
                else -> {}
            }
        }
    }
}
