package com.dahe.dispatch.cuspop

import android.content.Context
import android.graphics.Color
import android.text.TextUtils
import android.util.Log
import android.widget.TextView
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.dahe.mylibrary.net.CommonResponseBean
import com.dahe.mylibrary.utils.TimeUtil
import com.dahe.pass.R
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils
import kotlinx.android.synthetic.main.pop_sel_time.view.*
import java.text.SimpleDateFormat
import java.util.*

/**
 * Description: 仿知乎底部评论弹窗
 * Create by dance, at 2018/12/25
 */
class SelectTimePopup(
    context: Context,
) : BottomPopupView(context) {
    override fun getImplLayoutId() = R.layout.pop_sel_time

    override fun onCreate() {
        super.onCreate()

        tvOk.setOnClickListener {
            if (listener != null) {
                val startTime = tvStartTime.text.toString()
                val endTime = tvEndTime.text.toString()
                listener!!.onItemClick(startTime, endTime)

                dismiss()
            }
        }

        tvStartTime.setOnClickListener { setBir(0, tvStartTime) }
        tvEndTime.setOnClickListener { setBir(1, tvEndTime) }
        tvCancel.setOnClickListener { dismiss() }
    }

    //完全可见执行
    override fun onShow() {
        super.onShow()
        Log.e("tag", "知乎评论 onShow")
    }

    //完全消失执行
    override fun onDismiss() {
        Log.e("tag", "知乎评论 onDismiss")
    }

    override fun getMaxHeight(): Int {
        return (XPopupUtils.getScreenHeight(context) * .6f).toInt()
    }

    private var listener: OnMyItemClickListener? = null
    fun setOnMyItemClickListener(listener: OnMyItemClickListener?): SelectTimePopup {
        this.listener = listener
        return this
    }

    interface OnMyItemClickListener {
        fun onItemClick(startTime: String, endTime: String)
    }


    fun setBir(type: Int, view: TextView) {
        val startDate = Calendar.getInstance()
        val endtDate = Calendar.getInstance()
        val currDate = Calendar.getInstance()
        startDate[1990, 0] = 1
        endtDate[2100, 0] = 1


        if (!TextUtils.isEmpty(view.getText())) {
            val date = TimeUtil.string2Date(view.getText().toString(),
                SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()))
            if (date != null) currDate.time = date
        }


        val pvTime = TimePickerBuilder(
            context
        ) { date, v -> //选中事件回调
//            TimeUtil.date2String(date,SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()))
            val data =
                TimeUtil.date2String(date, SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()))
            view.text = data
        }.setSubmitColor(Color.parseColor("#F1474B")) //确定按钮文字颜色
            .setCancelColor(Color.parseColor("#F1474B")) //取消按钮文字颜色
            .setTitleBgColor(Color.parseColor("#FFFFFF"))
            .setType(booleanArrayOf(true, true, true, false, false, false))
            .setTitleText(if (0 == type) {
                "开始时间"
            } else {
                "结束时间"
            })
            .setDate(currDate)
            .setRangDate(startDate, endtDate)
            .isDialog(true)
            .setBgColor(Color.parseColor("#F5F5F5"))
            .build()
        //                 pvTime.setDate(Calendar.getInstance());//注：根据需求来决定是否使用该方法（一般是精确到秒的情况），此项可以在弹出选择器的时候重新设置当前时间，避免在初始化之后由于时间已经设定，导致选中时间与当前时间不匹配的问题。
        pvTime.show()
    }
}