package com.lukouguoji.aerologic.page.gnj.query.list

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONArray
import com.lukouguoji.aerologic.R
import com.lukouguoji.aerologic.page.gnj.query.details.GnjQueryDetailsActivity
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.GnjQueryBean
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.formatDate
import com.lukouguoji.module_base.ktx.launchCollect
import com.lukouguoji.module_base.ktx.launchLoadingCollect
import com.lukouguoji.module_base.ktx.toJson
import com.lukouguoji.module_base.ktx.toRequestBody
import com.lukouguoji.module_base.model.PadFilterDialogModel
import com.lukouguoji.module_base.model.PadFilterModel
import com.lukouguoji.module_base.model.ScanModel
import com.lukouguoji.module_base.ui.weight.search.layout.SearchLayoutType
import com.lukouguoji.module_base.util.Common
import com.lukouguoji.module_base.util.DictUtils
import dev.DevUtils
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils

class GnjQueryListViewModel : BasePageViewModel() {

    val date = MutableLiveData(DateUtils.getCurrentTime().formatDate())
    val dateEnd = MutableLiveData("")

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

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

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

    // 运单号列表
    val wbNoList = MutableLiveData<List<String>>()


    // 航班号
    val fNo = MutableLiveData("")

    // 始发港
    val dep = MutableLiveData("")

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

    // 运单类型
    val waybillType = MutableLiveData("")

    // 业务类型
    val businessType = MutableLiveData("")

    // 危险品描述
    val wxDesc = MutableLiveData("")

    // 危险品库
    val wxStash = MutableLiveData("")

    // 品名
    val goodsName = MutableLiveData("")

    val agentList = MutableLiveData(listOf(KeyValue("占位", "1"))).apply {
        DictUtils.getAgentList { value = it }
    }
    val spCodeList = MutableLiveData(listOf(KeyValue("占位", "1"))).apply {
        DictUtils.getSpecialCodeList(flag = 0, ieFlag = "I") { value = it }
    }
    val waybillTypeList = MutableLiveData(listOf(KeyValue("占位", "1"))).apply {
        DictUtils.getWaybillTypeList2(ieFlag = "I", type = "CI") { value = it }
    }
    val businessTypeList = MutableLiveData(listOf(KeyValue("占位", "1"))).apply {
        DictUtils.getBusinessTypeList(type = "CI") { value = it }
    }
    val wxDescList = MutableLiveData(listOf(KeyValue("占位", "1"))).apply {
        DictUtils.getWxDescList { value = listOf(KeyValue("全部", "")) + it }
    }
    val wxStashList = MutableLiveData(listOf(KeyValue("占位", "1"))).apply {
        DictUtils.getWxKuList { value = listOf(KeyValue("全部", "")) + it }
    }

    val itemViewHolder = GnjQueryListViewHolder::class.java
    val itemLayoutId = R.layout.item_gnj_query_list

    val count = MutableLiveData(0)
    val totalPc = MutableLiveData(0)
    val totalWeight = MutableLiveData(0.0)

    private val filterModel by lazy {
        PadFilterDialogModel(
            getTopActivity(), listOf(
                PadFilterModel(
                    fNo,
                    "航班号",
                    "请输入航班号",
                    type = SearchLayoutType.INPUT,
                    forcedCap = true
                ),
                PadFilterModel(
                    dep,
                    "始发港",
                    "请输入始发港",
                    type = SearchLayoutType.INPUT,
                    forcedCap = true
                ),
                PadFilterModel(
                    waybillType,
                    "运单类型",
                    "请选择运单类型",
                    type = SearchLayoutType.SPINNER,
                    waybillTypeList
                ),
                PadFilterModel(
                    businessType,
                    "业务类型",
                    "请选择业务类型",
                    type = SearchLayoutType.SPINNER,
                    businessTypeList
                ),
                PadFilterModel(
                    wxDesc,
                    "危险品描述",
                    "请选择危险品描述",
                    type = SearchLayoutType.SPINNER,
                    wxDescList
                ),
                PadFilterModel(
                    wxStash,
                    "危险品库",
                    "请选择危险品库",
                    type = SearchLayoutType.SPINNER,
                    wxStashList
                ),
                PadFilterModel(
                    goodsName,
                    "品名",
                    "请输入品名",
                    type = SearchLayoutType.INPUT,
                ),
            )
        ) {
            refresh()
        }
    }

    ///////////////////////////////////////////////////////////////////////////
    // 方法区
    ///////////////////////////////////////////////////////////////////////////

    fun waybillScanClick() {
        ScanModel.startScan(getTopActivity(), Constant.RequestCode.WAYBILL)
    }

    fun getWayBillList() {
        launchCollect({
            NetApply.api.getGnjQueryWbNoList(
                mapOf(
                    "page" to pageModel.page,
                    "limit" to pageModel.limit,
                    "beginDate" to date.value,
                    "endDate" to dateEnd.value,
                    "agentCode" to agent.value,
                    "spCode" to spCode.value,
                    "wbNo" to waybill.value,
                    "fno" to fNo.value,
                    "origin" to dep.value,
                    "awbType" to waybillType.value,
                    "businessType" to businessType.value,
                    "dgrDetail" to wxDesc.value,
                    "dgrLocation" to wxStash.value,
                    "goods" to goodsName.value,
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                val results = it.data!!
                when {
                    results.size == 1 -> {
                        waybill.value = results[0]
                        wbNoList.value = emptyList()
                    }
                    results.size > 1 -> {
                        wbNoList.value = results
                        showWbNoListSelect()
                    }
                    else -> {
                        wbNoList.value = emptyList()
                    }
                }
            }
            onFailed = { i: Int, s: String ->
                wbNoList.value = emptyList()
            }
        }
    }

    override fun getData() {
        launchLoadingCollect({
            NetApply.api.getGnjQueryList(
                mapOf(
                    "page" to pageModel.page,
                    "limit" to pageModel.limit,
                    "beginDate" to date.value,
                    "endDate" to dateEnd.value,
                    "agentCode" to agent.value,
                    "spCode" to spCode.value,
                    "wbNo" to waybill.value,
                    "fno" to fNo.value,
                    "origin" to dep.value,
                    "awbType" to waybillType.value,
                    "businessType" to businessType.value,
                    "dgrDetail" to wxDesc.value,
                    "dgrLocation" to wxStash.value,
                    "goods" to goodsName.value,
                ).toRequestBody()
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total
                totalPc.value = it.totalPc
                totalWeight.value = it.totalWeight
            }
        }
    }


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

    override fun onItemClick(position: Int, type: Int) {
        val adapter = pageModel.rv!!.commonAdapter()!!
        val bean = adapter.getItem(position) as GnjQueryBean
        GnjQueryDetailsActivity.start(getTopActivity(), bean.mawbId)
    }

    fun filterClick() {
        filterModel.show()
    }

    private fun showWbNoListSelect() {
        Common.singleSelect(
            DevUtils.getTopActivity(),
            "选择运单号",
            JSONArray.parseArray(wbNoList.value!!.map {
                mapOf(
                    "name" to it,
                    "code" to it,
                )
            }.toList().toJson(false)),
            null
        ) { position, _ ->
            waybill.value = wbNoList.value!![position]
        }
    }
}