package com.lukouguoji.gnc.page.shouyun.unlist

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONArray
import com.lukouguoji.gnc.R
import com.lukouguoji.gnc.page.shouyun.details.GncShouYunDetailsActivity
import com.lukouguoji.gnc.page.shouyun.list.GncShouYunListActivity
import com.lukouguoji.module_base.base.BasePageViewModel
import com.lukouguoji.module_base.bean.GncShouYunBean
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.loge
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.util.Common
import com.lukouguoji.module_base.util.DictUtils
import dev.DevUtils
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils

class GncShouYunUnListViewModel : BasePageViewModel() {

    // 日期
    val date = MutableLiveData(DateUtils.getCurrentTime().formatDate())

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

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

    // 代理
    val agentList = MutableLiveData<List<KeyValue>>().apply {
        DictUtils.getGncAgentList() {
            value = it
        }
    }
    var agent = MutableLiveData("")

    // 特码
    val spCodeList = MutableLiveData<List<KeyValue>>().apply {
        DictUtils.getSpecialCodeList(parentcode = "ALL") {
            value = it
        }
    }
    var spCode = MutableLiveData("")

    val wbNoList = MutableLiveData<List<String>>()


    val itemLayoutId = R.layout.item_gnc_shouyun_un
    val itemViewHolder = GncShouYunUnListViewHolder::class.java

    // 数量
    val count = MutableLiveData("0")

    private val filterDialog by lazy {
        PadFilterDialogModel(
            getTopActivity(), listOf(
                PadFilterModel(dest, title = "目的港", hint = "请输入目的港", forcedCap = true)
            )
        ) {
            refresh()
        }
    }

    fun getWayBillList() {
        val requestBody = mapOf(
            "pageNum" to pageModel.page,
            "pageSize" to pageModel.limit,
            "fdate" to date.value,
            "fno" to fno.value,
            "agentCode" to agent.value,
            "spCode" to spCode.value,
            "dest" to dest.value,
            "wbNo" to waybill.value
        ).toRequestBody()
        launchCollect({
            NetApply.api.getGncShouYunUnWbNoList(
                requestBody
            )
        }) {
            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()
            }
        }

    }

    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]
        }
    }

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

    override fun getData() {
        val requestBody = mapOf(
            "pageNum" to pageModel.page,
            "pageSize" to pageModel.limit,
            "fdate" to date.value,
            "fno" to fno.value,
            "agentCode" to agent.value,
            "spCode" to spCode.value,
            "wbNo" to waybill.value,
            "dest" to dest.value,
        ).toRequestBody()
        launchLoadingCollect({
            NetApply.api.getGncShouYunUnList(
                requestBody
            )
        }) {
            onSuccess = {
                pageModel.handleListBean(it)
                count.value = it.total.toString()
            }
        }
    }

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

    fun onFilterClick() {
        filterDialog.show()
    }

    fun onAddClick() {
        GncShouYunDetailsActivity.start(getTopActivity(), "")
    }

    override fun onItemClick(position: Int, type: Int) {
        val adapter = pageModel.rv!!.commonAdapter()!!
        val bean = adapter.getItem(position) as GncShouYunBean
        when (type) {
            R.id.ll -> {
                GncShouYunDetailsActivity.start(getTopActivity(), bean.wbNo)
            }

            else -> {}
        }
    }

    /**
     *  点击
     */
    fun onShouYunClick() {
        GncShouYunListActivity.start(getTopActivity())
    }

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

                else -> {}
            }
        }
    }

}