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

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSONArray
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lukouguoji.module_base.ktx.formatDate
import com.lukouguoji.module_base.ktx.toJson
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.DialogPdaCunfangFilterBinding
import com.sixin.module_p.databinding.DialogPdaDaishouyunFilterBinding
import dev.DevUtils
import dev.utils.app.info.KeyValue
import dev.utils.common.DateUtils
import java.util.Calendar

class PdaDaiShouYunFilterDialogModel(
    private val callBack: () -> Unit
) : BaseDialogModel<DialogPdaDaishouyunFilterBinding>(DIALOG_TYPE_DRAWER) {

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

    val flightNo = MutableLiveData("")

    val dest = MutableLiveData("")

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

    // 特码
    val spCode = MutableLiveData("")
    val spCodeDesc = MutableLiveData("全部")

    val agentList = MutableLiveData<List<KeyValue>>(listOf()).apply {
        DictUtils.getAgentList {
            value = it
        }
    }
    val spCodeList = MutableLiveData<List<KeyValue>>(listOf()).apply {
        DictUtils.getSpecialCodeList {
            value = it
        }
    }

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

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

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

    fun onAgentSelected(position: Int) {
        agent.value = agentList.value?.get(position)?.value
    }

    fun onSpCodeSelected(position: Int) {
        spCode.value = spCodeList.value?.get(position)?.value
    }

    fun spCodeClick() {
        Common.singleSelect(
            DevUtils.getTopActivity(),
            "选择特码",
            JSONArray.parseArray(spCodeList.value!!.map {
                mapOf(
                    "name" to it.key,
                    "code" to it.value,
                )
            }.toList().toJson(false)),
            null
        ) { position, _ ->
            spCode.value = spCodeList.value?.get(position)?.value
            spCodeDesc.value = spCodeList.value?.get(position)?.key
        }
    }

    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 onResetClick() {
        date.value = DateUtils.getCurrentTime().formatDate()
        flightNo.value = ""
        if (binding.spinnerArea.adapter.count > 0) {
            binding.spinnerArea.setSelection(0)
        }
        spCode.value = ""
        spCodeDesc.value = "全部"
        dest.value = ""

        callBack.invoke()
        dismiss()
    }

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