package com.module.backstage.dialog

import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.EditText
import com.hboxs.serialport.plc.DialogClickListener
import com.module.backstage.R
import com.module.backstage.databinding.BackstageDialogDateBinding
import com.quyunshuo.androidbaseframemvvm.base.mvvm.v.BaseFrameDialog
import com.quyunshuo.androidbaseframemvvm.common.util.ToastUtil
import com.quyunshuo.androidbaseframemvvm.common.util.UiUtil
import java.util.Calendar
import java.util.TimeZone

/**
 * 开关机的对话框
 */
class DateDialog(mContext: Context) : BaseFrameDialog<BackstageDialogDateBinding>(mContext) {

    //获取当前时间
    private var year: String? = null
    private var month: String? = null
    private var day: String? = null
    private var hour: String? = null

    var type1: Int = 1
    var type2: Int = 2
    //类型
    var type: Int = 0
        set(value) {
            field = value
            checkShow(type)
        }

    private fun checkShow(type: Int) {
        when (type) {
            0 -> {
                //时
                mBinding.etYear.setVisibility(View.VISIBLE)
                mBinding.railYear.setVisibility(View.VISIBLE)
                mBinding.etMonth.setVisibility(View.VISIBLE)
                mBinding.railMonth.setVisibility(View.VISIBLE)
                mBinding.etDay.setVisibility(View.VISIBLE)
                mBinding.railDay.setVisibility(View.VISIBLE)
                mBinding.etHour.setVisibility(View.VISIBLE)
                mBinding.railHour.setVisibility(View.VISIBLE)
            }

            1 -> {
                //日
                mBinding.etYear.setVisibility(View.VISIBLE)
                mBinding.railYear.setVisibility(View.VISIBLE)
                mBinding.etMonth.setVisibility(View.VISIBLE)
                mBinding.railMonth.setVisibility(View.VISIBLE)
                mBinding.etDay.setVisibility(View.VISIBLE)
                mBinding.railDay.setVisibility(View.VISIBLE)
                mBinding.etHour.setVisibility(View.GONE)
                mBinding.railHour.setVisibility(View.GONE)
            }

            2 -> {
                //月
                mBinding.etYear.setVisibility(View.VISIBLE)
                mBinding.railYear.setVisibility(View.VISIBLE)
                mBinding.etMonth.setVisibility(View.VISIBLE)
                mBinding.railMonth.setVisibility(View.VISIBLE)
                mBinding.etDay.setVisibility(View.GONE)
                mBinding.railDay.setVisibility(View.GONE)
                mBinding.etHour.setVisibility(View.GONE)

                mBinding.railHour.setVisibility(View.GONE)
            }

            3 -> {
                //年
                mBinding.etYear.setVisibility(View.VISIBLE)
                mBinding.railYear.setVisibility(View.VISIBLE)
                mBinding.etMonth.setVisibility(View.GONE)
                mBinding.railMonth.setVisibility(View.GONE)
                mBinding.etDay.setVisibility(View.GONE)
                mBinding.railDay.setVisibility(View.GONE)
                mBinding.etHour.setVisibility(View.GONE)
                mBinding.railHour.setVisibility(View.GONE)
            }
        }
    }

    private val TAG = "DateDialog"


    private var devOnoffDialoglistener: DialogClickListener? = null


    override fun BackstageDialogDateBinding.initView() {
        getTime()
        setTime(year!!, etYear)
        setTime(month!!, etMonth)
        setTime(day!!, etDay)
        setTime(hour!!, etHour)
        tvBack.setOnClickListener {
            dismiss()
        }
        tvStartDev.setOnClickListener {
            var text: String? = ""
            val year: String = etYear.getText().toString().trim()
            if (checkYear(year)) return@setOnClickListener
            val month: String = etMonth.getText().toString().trim()
            if (checkMonth(month)) return@setOnClickListener
            val day: String = etDay.getText().toString().trim()
            if (checkDay(month, day)) return@setOnClickListener
            val hour: String = etHour.getText().toString().trim()
            if (checkHour(month, day, hour)) return@setOnClickListener

            text = getText(text!!, year, month, day, hour)
            Log.d(TAG, "initView: "+text)
            devOnoffDialoglistener?.onClickListener(type1, text)
            dismiss()
        }

    }


    /**
     * 获取最终时间
     * @param text
     * @param year
     * @param month
     * @param day
     * @param hour
     * @return
     */
    private fun getText(
        text: String,
        year: String,
        month: String,
        day: String,
        hour: String
    ): String {
        var text = text
        var month = month
        var day = day
        var hour = hour
        when (type) {
            0 -> {
                //2016-01-24 02:51:18
                month = setTime(month)
                day = setTime(day)
                hour = setTime(hour)
                text = "$year-$month-$day $hour"
            }

            1 -> {
                month = setTime(month)
                day = setTime(day)
                text = "$year-$month-$day"
            }

            2 -> {
                month = setTime(month)
                text = "$year-$month"
            }

            3 -> text = year
        }
        return text
    }

    override fun createVB() = BackstageDialogDateBinding.inflate(layoutInflater)


    override fun initObserve() {
    }

    override fun initRequestData() {
    }

    fun setListener(listener: DialogClickListener) {
        this.devOnoffDialoglistener = listener
    }

    /**
     * 小于两位补0
     */
    private fun setTime(time: String): String {
        if (time.length < 2) {
            return "0$time"
        }
        return time
    } //    public void setIsStartTime(boolean siTimeStart) {
    //
    //    }

    /**
     * 设置时间
     * @param time 系统时间
     * @param et 要设置的控件
     */
    private fun setTime(time: String, et: EditText) {
        et.setText(time)
    }

    /**
     * 获取时间
     */
    private fun getTime() {
        val cal = Calendar.getInstance()
        cal.timeZone = TimeZone.getTimeZone("GMT+8:00")
        //        cal.add(cal.DATE, 11); // 在日期中增加天数,1天,跨月，会自动修改，牛逼
        year = cal[Calendar.YEAR].toString()
        month = (cal[Calendar.MONTH] + 1).toString()
        if (month!!.length < 2) {
            month = "0$month"
        }
        day = cal[Calendar.DATE].toString()
        if (day!!.length < 2) {
            day = "0$day"
        }
        if (cal[Calendar.AM_PM] == 0) {
            hour = cal[Calendar.HOUR].toString()
            if (hour!!.length < 2) {
                hour = "0$hour"
            }
        } else {
            hour = (cal[Calendar.HOUR] + 12).toString()
        }
    }


    /**
     * 检查小时合法性
     * @param month
     * @param day
     * @param hour
     * @return
     */
    private fun checkHour(month: String?, day: String?, hour: String?): Boolean {
        if (type == 0) {
            if (month == null || TextUtils.isEmpty(month)) {
                ToastUtil.showToast(UiUtil.getStringRes(R.string.base_backstage_month_input))
                return true
            }
            if (day == null || TextUtils.isEmpty(day)) {
                ToastUtil.showToast(UiUtil.getStringRes(R.string.base_backstage_day_input))
                return true
            }
            if (hour == null || TextUtils.isEmpty(hour)) {
                ToastUtil.showToast(UiUtil.getStringRes(R.string.base_backstage_hour_input))
                return true
            }

            if (month.toInt() < 1 || month.toInt() > 12) {
                ToastUtil.showToast(UiUtil.getStringRes(R.string.base_backstage_ringht_month_input))
                return true
            }

            if (day.toInt() < 0 || day.toInt() > 31) {
                ToastUtil.showToast(UiUtil.getStringRes(R.string.base_backstage_ringht_day_input))
                return true
            }

            if (hour.toInt() < 0 || hour.toInt() > 24) {
                ToastUtil.showToast(UiUtil.getStringRes(R.string.base_backstage_ringht_hour_input))
                return true
            }
        }
        return false
    }

    /**
     * 检查时间合法性
     * @param month
     * @param day
     * @return
     */
    private fun checkDay(month: String?, day: String?): Boolean {
        if (type == 1) {
            if (month == null || TextUtils.isEmpty(month)) {
                ToastUtil.showToast(UiUtil.getStringRes(R.string.base_backstage_month_input))
                return true
            }
            if (day == null || TextUtils.isEmpty(day)) {
                ToastUtil.showToast(UiUtil.getStringRes(R.string.base_backstage_day_input))
                return true
            }
            if (month.toInt() < 1 || month.toInt() > 12) {
                ToastUtil.showToast(UiUtil.getStringRes(R.string.base_backstage_ringht_month_input))
                return true
            }

            if (day.toInt() < 0 || day.toInt() > 31) {
                ToastUtil.showToast(UiUtil.getStringRes(R.string.base_backstage_ringht_day_input))
                return true
            }
        }
        return false
    }

    /**
     * 检查月份合法性
     * @param month
     * @return
     */
    private fun checkMonth(month: String?): Boolean {
        if (type == 2) {
            if (month == null || TextUtils.isEmpty(month)) {
                ToastUtil.showToast(UiUtil.getStringRes(R.string.base_backstage_month_input))
                return true
            }
            if (month.toInt() < 1 || month.toInt() > 12) {
                ToastUtil.showToast(UiUtil.getStringRes(R.string.base_backstage_ringht_month_input))
            }
        }
        return false
    }

    /**
     * 检查年合法性
     * @param year
     * @return
     */
    private fun checkYear(year: String?): Boolean {
        if (year == null || TextUtils.isEmpty(year)) {
            ToastUtil.showToast(UiUtil.getStringRes(R.string.base_backstage_year_input))
            return true
        }
        return false
    }


}