package com.hongyegroup.cpt_pensonal.mvvm

import android.app.Activity
import android.app.DatePickerDialog
import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.guadou.cs_cptservices.binding.BaseDataBindingAdapter
import com.guadou.lib_baselib.base.vm.BaseViewModel
import com.guadou.lib_baselib.utils.DateAndTimeUtil
import com.hongyegroup.cpt_pensonal.BR
import com.hongyegroup.cpt_pensonal.R
import com.hongyegroup.cpt_pensonal.bean.MakeAppointmentTime
import java.util.*


class PensonalAppointmentMakeViewModel  @ViewModelInject constructor(

) : BaseViewModel() {

    var mDatas = mutableListOf<MakeAppointmentTime>()
    val mAdapter by lazy { BaseDataBindingAdapter(R.layout.item_make_appointment_time, BR.item, mDatas) }

    private var startDatePicker: DatePickerDialog? = null
    private var selectedDate = ""
    private var selectedDateTimeMilles = 0L

    /**
     * 选择预约的日期 (系统时间选择弹窗)
     */
    fun showPikeDateDialog(activity: Activity): LiveData<String> {

        val liveData = MutableLiveData<String>()
        val calendar = Calendar.getInstance()

        val dateSep: Array<String> = selectedDate.split("-").toTypedArray()

        val year = if (selectedDate.isEmpty()) calendar[Calendar.YEAR] else dateSep[0].toInt()
        val month = if (selectedDate.isEmpty()) calendar[Calendar.MONTH] else dateSep[1].toInt() - 1
        val day = if (selectedDate.isEmpty()) calendar[Calendar.DAY_OF_MONTH] else dateSep[2].toInt()
        startDatePicker = DatePickerDialog(activity, { _, year, month, day ->

            var monthStr = (month + 1).toString()
            if (monthStr.length == 1) {
                monthStr = "0$monthStr"
            }
            var dayStr = day.toString()
            if (dayStr.length == 1) {
                dayStr = "0$dayStr"
            }

            selectedDate = StringBuilder().append(year).append("-").append(monthStr).append("-").append(dayStr).toString()
            selectedDateTimeMilles = DateAndTimeUtil.getTimeStamp(selectedDate, "yyyy-MM-dd")
            liveData.value = selectedDateTimeMilles.toString()

        }, year, month, day)

        startDatePicker?.show()

        return liveData
    }

}