package com.miya.fastcashier.utils

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import androidx.core.content.res.ResourcesCompat
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.miya.fastcashier.R
import java.util.*

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

    var timePickerView: TimePickerView? = null
    fun showDatePicker() {
        if (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()
    }

    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, OnTimeSelectListener { 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 dismiss() {
        timePickerView?.dismiss()
    }


}