package com.hongyegroup.cpt_parttime.mvvm

import android.app.Activity
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.guadou.lib_baselib.base.vm.BaseViewModel
import java.util.*

class PopupDateViewModel : BaseViewModel() {

    private var mStartTimeStamp: Long = System.currentTimeMillis()
    private var mEndTimeStamp: Long = System.currentTimeMillis()

    private val startCalendar = Calendar.getInstance()
    private val endCalendar = Calendar.getInstance()

    init {
        //当前弹窗全局设置区间
        startCalendar.add(Calendar.YEAR, -3)
        endCalendar.add(Calendar.YEAR, 1)
    }

    /**
     * 选择开始时间
     */
    fun pickStartDate(activity: Activity): LiveData<String> {
        val liveData = MutableLiveData<String>()

        //时间选择器
        val pickerBuilder = TimePickerBuilder(activity) { date, _ ->
            mStartTimeStamp = date.time
            liveData.value = mStartTimeStamp.toString()
        }
            .isDialog(true)
            .setRangDate(startCalendar, endCalendar)
            .setTitleText("开始日期")
            .setType(booleanArrayOf(true, true, true, false, false, false))

        //默认选中现在时间
        val calendar = Calendar.getInstance()
        calendar.timeInMillis = mStartTimeStamp
        pickerBuilder.setDate(calendar)

        pickerBuilder.build().show()

        return liveData
    }

    /**
     * 选择结束时间
     */
    fun pickEndDate(activity: Activity) : LiveData<String>{
        val liveData = MutableLiveData<String>()

        //时间选择器
        val pickerBuilder = TimePickerBuilder(activity) { date, _ ->
            mEndTimeStamp = date.time
            liveData.value = mEndTimeStamp.toString()
        }
            .setTitleText("结束日期")
            .isDialog(true)
            .setRangDate(startCalendar, endCalendar)
            .setType(booleanArrayOf(true, true, true, false, false, false))

        val calendar = Calendar.getInstance()
        calendar.timeInMillis = mEndTimeStamp
        pickerBuilder.setDate(calendar)

        pickerBuilder.build().show()

        return liveData
    }

}