package com.xty.common.picker

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.tamsiree.rxkit.RxTimeTool
import com.xty.common.R
import java.text.SimpleDateFormat
import java.util.*

class CustomTimeSelect(var context: Context, var select: (index: Int, options1: Int, time: String, view: View?) -> Unit) {
    var format = "yyyy-MM-dd"  //需要在外部输出的时间格式
    private var selectedDate = Calendar.getInstance()
    var startDate = Calendar.getInstance()
    var endDate = Calendar.getInstance()
    var timeView: View? = null
    var index: Int = 0
    var options1: Int = 0

    //是分秒是否显示
    var showYear = true
    var showMonth = true
    var showDay = true
    var showHour = false
    var showMin = false
    var showSec = false

    init {
        val instance = Calendar.getInstance()
        startDate.set(
            instance.get(Calendar.YEAR) - 1,
            instance.get(Calendar.MONTH),
            instance.get(Calendar.DATE)
        )
        endDate.set(
            instance.get(Calendar.YEAR),
            instance.get(Calendar.MONTH),
            instance.get(Calendar.DATE)
        )
    }

    private var pvTime: CustomTimePickerView? = null

    fun initPickerView(){
        pvTime = CustomTimePickerBuilder(context
        ) { date, _ ->
            val date2String = RxTimeTool.date2String(date, SimpleDateFormat(format))
            if (null != timeView) {
                if (timeView is TextView) {
                    (timeView as TextView).text = date2String
                }
            }
            select.invoke(index, options1, date2String, timeView)
        }
            .setType(
                booleanArrayOf(
                    showYear,
                    showMonth,
                    showDay,
                    showHour,
                    showMin,
                    showSec
                )
            )// 显示需要的时间
            .setCancelText(context.getString(R.string.cancel))//取消按钮文字
            .setSubmitText(context.getString(R.string.confirm))//确认按钮文字
            //.setContentSize(18)//滚轮文字大小
            .setTitleSize(18)//标题文字大小
            .setTitleText("")//标题文字
            .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
            .isCyclic(false)//是否循环滚动
            .setTitleColor(ContextCompat.getColor(context, R.color.col_455)!!)//标题文字颜色
            .setSubmitColor(ContextCompat.getColor(context, R.color.start_col))!!//确定按钮文字颜色
            .setCancelColor(ContextCompat.getColor(context, R.color.col_c64)!!)//取消按钮文字颜色
            .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
            .setBgColor(Color.WHITE)//滚轮背景颜色 Night mode
            .setDate(selectedDate)// 如果不设置的话，默认是系统时间*/
            .setRangDate(startDate, endDate)//起始终止年月日设定
            .setLabel("年", "月", "日", "时", "分", "")//默认设置为年月日时分秒
            .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
            .isDialog(false)//是否显示为对话框样式
            .build();
    }

    fun setConfig(format: String, showHour: Boolean = true, showMin: Boolean = true) {
        setConfig(
            showYear = false,
            showMonth = false,
            showDay = false,
            showHour = showHour,
            showMin = showMin,
            showSec = false,
            timeView = null,
            format = format
        )
    }

    fun setConfig(
        showYear: Boolean = true,
        showMonth: Boolean = true,
        showDay: Boolean = true,
        showHour: Boolean = true,
        showMin: Boolean = true,
        showSec: Boolean = true,
        timeView: View? = null,
        format: String = "yyyy-MM-dd"
    ) {
        this.showYear = showYear
        this.showMonth = showMonth
        this.showDay = showDay
        this.showHour = showHour
        this.showMin = showMin
        this.showSec = showSec
        this.timeView = timeView
        this.format = format
    }

    fun show(index: Int, options1: Int) {
        if (null == pvTime){
            initPickerView()
        }
        this.index = index
        this.options1 = options1
        pvTime?.show()
    }

    fun selectTime(index: Int, options1: Int, format: String = "yyyy-MM-dd") {
        selectTime(index, options1,null, format)
    }

    fun selectTime(index: Int, options1: Int,timeTv: View?, format: String = "yyyy-MM-dd") {
        this.index = index
        this.options1 = options1
        this.format = format
        this.timeView = timeTv
        if (null == pvTime){
            initPickerView()
        }
        pvTime?.show()
    }

}