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

import android.content.Context
import android.text.Editable
import android.widget.EditText
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import com.alibaba.fastjson.JSONArray
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lukouguoji.module_base.http.net.NetApply
import com.lukouguoji.module_base.ktx.formatDate
import com.lukouguoji.module_base.ktx.getActivity
import com.lukouguoji.module_base.ktx.getLifecycleOwner
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.util.Common
import com.lukouguoji.module_base.util.DictUtils
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupPosition
import com.sixin.module_p.R
import com.sixin.module_p.databinding.DialogPdaShouyunFilterBinding
import dev.DevUtils
import dev.utils.app.ViewUtils.findViewById
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils
import java.util.Calendar

class PdaShouYunFilterDialogModel(
    private val scanClick: () -> Unit,
    private val callBack: () -> Unit
) : BaseDialogModel<DialogPdaShouyunFilterBinding>(DIALOG_TYPE_DRAWER) {

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

    val flightNo = MutableLiveData("")

    val waybill = MutableLiveData("")
    private val waybillLive: LiveData<String> = waybill

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

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

    override fun onDialogCreated(context: Context) {
        binding.model = this

        waybillLive.observe(context.getLifecycleOwner(), Observer { result ->
            if (result != null && result.length >=4 && result.length < 9) {
                getWbNoList(result)
            }

        })
    }

    override fun onBuild(builder: XPopup.Builder) {
        builder.popupPosition(PopupPosition.Right)

    }

    fun onDateClick() {
        Common.onYearMonthDay(DevUtils.getTopActivity(), date.value) { year, month, day ->
            val calendar = Calendar.getInstance()
            calendar.set(year, month - 1, day)
            date.value = DateUtils.formatDate(calendar.time, "yyyy-MM-dd")
        }
    }

    fun onDateRKClick() {
        Common.onYearMonthDay(DevUtils.getTopActivity(), dateRuKu.value) { year, month, day ->
            val calendar = Calendar.getInstance()
            calendar.set(year, month - 1, day)
            dateRuKu.value = DateUtils.formatDate(calendar.time, "yyyy-MM-dd")
        }
    }

    fun onResetClick() {
        dateRuKu.value = DateUtils.getCurrentTime().formatDate()
        date.value = ""
        flightNo.value = ""
        waybill.value = ""
        callBack.invoke()
        dismiss()
    }

    fun onConfirmClick() {
        callBack.invoke()
        dismiss()
    }

    fun onWaybillScanClick() {
        scanClick.invoke()
    }
    private fun getWbNoList(input: String) {
        val requestBody = mapOf(
            "page" to 1,
            "limit" to 20,
            "fdate" to date.value,
            "opDate" to dateRuKu.value,
            "fno" to flightNo.value,
            "wbNo" to input,
        ).toRequestBody()
        launchCollect({
            NetApply.api.getGncShouYunWbNoList(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]
        }
    }
}