package com.module.backstage.dialog

import android.content.Context
import com.module.backstage.R
import com.module.backstage.databinding.BackstageDialogLocalAlarmClockBinding
import com.quyunshuo.androidbaseframemvvm.base.mvvm.v.BaseFrameDialog
import com.quyunshuo.androidbaseframemvvm.common.bean.LocalAlarmClockBean
import com.quyunshuo.androidbaseframemvvm.common.enums.AlarmClockEnum
import com.quyunshuo.androidbaseframemvvm.common.enums.WeekEnum
import com.quyunshuo.androidbaseframemvvm.common.util.ToastUtil


/**
 * 本地闹钟app的对话框
 */
class LocalAlarmClockDialog(mContext: Context) :
    BaseFrameDialog<BackstageDialogLocalAlarmClockBinding>(mContext) {
    private var localAlarmClockBean: LocalAlarmClockBean? = null
    var addOrUpdate: Int = 1 // 1 是添加， 2 是更新。
    private var listener: DialogClickListener? = null
    var add:Int =1
    var update:Int =2
    var timePickerDialog: TimePickerDialog? = null
    var selectTime = "12:00"
    var alarmclockTypeEnum : AlarmClockEnum? =null
    var id:Int? = 0

    override fun BackstageDialogLocalAlarmClockBinding.initView() {
        tvBack.setOnClickListener {
            dismiss()
        }
        tvTime.setOnClickListener{
            showTimePickerDialog()
        }
        tvConfirm.setOnClickListener{
            saveAlarmClock(addOrUpdate)
        }
        rgType.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.rb_open_dev -> alarmclockTypeEnum = AlarmClockEnum.OPEN_DEV
                R.id.rb_close_dev -> alarmclockTypeEnum = AlarmClockEnum.CLOSE_DEV
                R.id.rb_open_sleep -> alarmclockTypeEnum = AlarmClockEnum.OPEN_SLEEP
                R.id.rb_close_sleep -> alarmclockTypeEnum = AlarmClockEnum.CLOSE_SLEEP
                else -> {
                    alarmclockTypeEnum = null
                }
            }
        }
    }

    private fun saveAlarmClock(addOrUpdate: Int) {
        var alarmClockBean:LocalAlarmClockBean? =null
        if (addOrUpdate ==add) {
            alarmClockBean = LocalAlarmClockBean()
        }else{
            alarmClockBean = localAlarmClockBean
        }

        // todo 保存本地闹钟。
        val calendars: ArrayList<WeekEnum> = ArrayList<WeekEnum>()
        if (mBinding.cbSunday.isChecked) {
            calendars.add(WeekEnum.SUNDAY)
        }
        if (mBinding.cbMonday.isChecked) {
            calendars.add(WeekEnum.MONDAY)
        }
        if (mBinding.cbTuesday.isChecked) {
            calendars.add(WeekEnum.TUESDAY)
        }
        if (mBinding.cbWednesday.isChecked) {
            calendars.add(WeekEnum.WEDNESDAY)
        }
        if (mBinding.cbThursday.isChecked) {
            calendars.add(WeekEnum.THURSDAY)
        }
        if (mBinding.cbFriday.isChecked) {
            calendars.add(WeekEnum.FRIDAY)
        }
        if (mBinding.cbSaturday.isChecked) {
            calendars.add(WeekEnum.SATURDAY)
        }
        if (calendars.isEmpty()) {
            ToastUtil.showToast("请设置星期")
            return
        }
        alarmClockBean!!.id = ""+id
        alarmClockBean.week = calendars
        val split = selectTime.split(":")
        alarmClockBean.hour = split[0]
        alarmClockBean.minute = split[1]
        alarmClockBean.type = alarmclockTypeEnum
        alarmClockBean.status = mBinding.scStatus.isChecked
        listener?.onClickListener(addOrUpdate, alarmClockBean)
        dismiss()
    }

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


    override fun initObserve() {
    }

    override fun initRequestData() {
    }

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


    fun showTimePickerDialog(){
        if (timePickerDialog == null) {
            timePickerDialog = TimePickerDialog(
                this@LocalAlarmClockDialog.context
            )
            timePickerDialog?.setListener(object : com.hboxs.serialport.plc.DialogClickListener {
                override fun onClickListener(type: Int, text: String?) {
                    when (type) {
                        timePickerDialog?.type1 -> {
//                            updateApk()
                        }
                        200->{
                            mBinding.tvTime.text = "选择时间：$text"
                            selectTime = text!!
                        }
                        else -> {}
                    }
                }

            })
        }

        timePickerDialog?.show()

    }

    fun add(id: Int?, addOrUpdate: Int) {
        mBinding.tvTime.text = "选择时间：12:00"
        this.id = id
        this.addOrUpdate = addOrUpdate
        mBinding.cbSunday.isChecked = true
        mBinding.cbMonday.isChecked = true
        mBinding.cbTuesday.isChecked = true
        mBinding.cbWednesday.isChecked = true
        mBinding.cbThursday.isChecked = true
        mBinding.cbFriday.isChecked = true
        mBinding.cbSaturday.isChecked = true

        mBinding.rbOpenDev.isChecked = true
    }

    fun update(localAlarmClockBean: LocalAlarmClockBean?, addOrUpdate: Int) {
        this.localAlarmClockBean = localAlarmClockBean
        this.id = localAlarmClockBean?.id?.toInt()
        this.addOrUpdate = addOrUpdate
        localAlarmClockBean?.week?.forEach {
            when (it) {
                WeekEnum.SUNDAY -> mBinding.cbSunday.isChecked = true
                WeekEnum.MONDAY -> mBinding.cbMonday.isChecked = true
                WeekEnum.TUESDAY -> mBinding.cbTuesday.isChecked = true
                WeekEnum.WEDNESDAY -> mBinding.cbWednesday.isChecked = true
                WeekEnum.THURSDAY -> mBinding.cbThursday.isChecked = true
                WeekEnum.FRIDAY -> mBinding.cbFriday.isChecked = true
                WeekEnum.SATURDAY -> mBinding.cbSaturday.isChecked = true
            }
        }
        when (localAlarmClockBean?.type) {
            AlarmClockEnum.OPEN_SLEEP -> mBinding.rbOpenSleep.isChecked =true
                AlarmClockEnum.CLOSE_SLEEP -> mBinding.rbCloseSleep.isChecked =true
            AlarmClockEnum.CLOSE_DEV -> mBinding.rbCloseDev.isChecked =true
            AlarmClockEnum.OPEN_DEV -> mBinding.rbOpenDev.isChecked =true
            null -> mBinding.rbOpenDev.isChecked =true
        }
    }

    interface DialogClickListener {
        fun onClickListener(type: Int, localAlarmClockBean: LocalAlarmClockBean)

    }
}