package com.miya.fastcashier.utils

import android.app.Activity
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.view.TimePickerView
import java.util.*

class DateSelectHelper(val act: Activity, val callback: (Date) -> Unit) {

    var timePickerView: TimePickerView? = null

    private fun initTimePicker() {
        val calendar = Calendar.getInstance()
        val startDate = Calendar.getInstance()
        val endDate = Calendar.getInstance()

        startDate.set(
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),
            0,
            0,
            0
        )
        endDate.set(
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),
            calendar.get(Calendar.HOUR_OF_DAY),
            calendar.get(Calendar.MINUTE)
        )
        timePickerView = TimePickerBuilder(act) { date, v ->
            callback(date)
        }
            .setTitleText("请选择开始时间")
            .setType(booleanArrayOf(false, false, false, true, true, false))
            .setDate(startDate)
            .setContentTextSize(30)
            .setTitleSize(30)
            .setSubCalSize(30)
            .setLineSpacingMultiplier(1.9f)
            .setRangDate(startDate, endDate)
            .setOutSideCancelable(false)
            .setDecorView(act.findViewById(android.R.id.content))
            .build()
    }

    fun showDatePicker() {
        timePickerView = null
        initTimePicker()
        val date = Calendar.getInstance()
        date.set(
            date.get(Calendar.YEAR),
            date.get(Calendar.MONTH),
            date.get(Calendar.DAY_OF_MONTH),
            0,
            0
        )
        timePickerView!!.setDate(date)
        timePickerView!!.show()
    }

    fun dismiss() {
        timePickerView?.dismiss()
    }
}