package com.sixin.module_p.ui.user.message

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.lukouguoji.module_base.base.BaseDialogModel
import com.lukouguoji.module_base.ktx.formatDate
import com.lukouguoji.module_base.util.Common
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.enums.PopupPosition
import com.sixin.module_p.R
import com.sixin.module_p.databinding.DialogPdaMessageFilterBinding
import dev.DevUtils
import dev.utils.common.DateUtils
import java.util.Calendar

class PdaMessageFilterDialogModel(
    private val callBack: () -> Unit
) : BaseDialogModel<DialogPdaMessageFilterBinding>(DIALOG_TYPE_DRAWER) {

    val date = MutableLiveData(DateUtils.getCurrentTime().formatDate())
    val beginDate = MutableLiveData(DateUtils.getCurrentTime().formatDate())
    val endDate = MutableLiveData(DateUtils.getCurrentTime().formatDate())
    val keyWord = MutableLiveData("")

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

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

    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 onBeginDateClick() {
        Common.onYearMonthDay(DevUtils.getTopActivity(), beginDate.value) { year, month, day ->
            val calendar = Calendar.getInstance()
            calendar.set(year, month - 1, day)
            beginDate.value = DateUtils.formatDate(calendar.time, "yyyy-MM-dd")
        }
    }
    fun onEndDateClick() {
        Common.onYearMonthDay(DevUtils.getTopActivity(), endDate.value) { year, month, day ->
            val calendar = Calendar.getInstance()
            calendar.set(year, month - 1, day)
            endDate.value = DateUtils.formatDate(calendar.time, "yyyy-MM-dd")
        }
    }

    fun onResetClick() {
        date.value = DateUtils.getCurrentTime().formatDate()
        keyWord.value = ""
        onConfirmClick()
    }

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