package com.example.iot_controlhost.ui.dialog;

import android.content.Context;

import androidx.annotation.NonNull;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.TimeUtils;
import com.example.iot_controlhost.R;
import com.example.iot_controlhost.adapter.UvTaskAdapter;
import com.example.iot_controlhost.base.BaseDialog;
import com.example.iot_controlhost.databinding.DialogUvtaskInsertBinding;
import com.example.iot_controlhost.model.MyTask;
import com.example.iot_controlhost.model.thread.QueueIOTask;
import com.example.iot_controlhost.utils.MyQueue;
import com.example.iot_controlhost.utils.MyUtil;
import com.example.iot_controlhost.utils.control.disinfect.DisinfectController;
import com.example.iot_controlhost.utils.global.RoomController;
import com.example.iot_controlhost.utils.timer.UVTaskUtil;
import com.example.iot_controlhost.utils.log.UserLog;

import java.util.Date;

import es.dmoral.toasty.Toasty;

/**
 * @Description 新增消毒计划
 * @Author DuanKaiji
 * @CreateTime 2024年03月07日 17:26
 */
public class UvTaskInsertDialog extends BaseDialog<DialogUvtaskInsertBinding> {
    UvTaskAdapter adapter;
    public UvTaskInsertDialog(@NonNull Context context, UvTaskAdapter adapter) {
        super(context);
        this.adapter = adapter;
        //时间选择器
        TimePickerView picker1 = new TimePickerBuilder(mContext, (date, v) -> binding.tvStartTime.setText(TimeUtils.date2String(date, "HH:mm"))).isDialog(true)
                .setContentTextSize(35).setTitleSize(35).isCyclic(false).setType(new boolean[]{false, false, false, true, true, false}).build();
        TimePickerView picker2 = new TimePickerBuilder(mContext, (date, v) -> binding.tvEndTime.setText(TimeUtils.date2String(date, "HH:mm"))).isDialog(true)
                .setContentTextSize(35).setTitleSize(35).isCyclic(false).setType(new boolean[]{false, false, false, true, true, false}).build();
        binding.tvStartTime.setOnClickListener(view -> picker1.show());
        binding.tvEndTime.setOnClickListener(view -> picker2.show());
        binding.btnYes.setOnClickListener(view -> {
            String startTimeStr = binding.tvStartTime.getText().toString();
            String endTimeStr = binding.tvEndTime.getText().toString();
            Date startTime = TimeUtils.string2Date(startTimeStr, "HH:mm");
            Date endTime = TimeUtils.string2Date(endTimeStr, "HH:mm");
            // 检查结束时间是否小于开始时间
            if (startTime == null || endTime == null) {
                new ConfirmDialog(mContext, mContext.getString(R.string.error), mContext.getString(R.string.select_end_time_tip2), null).show();
            } else if (endTime.compareTo(startTime) <= 0) {
                new ConfirmDialog(mContext, mContext.getString(R.string.error), mContext.getString(R.string.select_end_time_tip), null).show();
            } else {
                //新增消毒计划
                int[] startTimes = MyUtil.getHourAndMinuteUsingLocalTime(startTime);
                int[] endTimes = MyUtil.getHourAndMinuteUsingLocalTime(endTime);
                if (!UVTaskUtil.addUvTask(new MyTask(startTimes[0], startTimes[1], endTimes[0], endTimes[1]))) {
                    Toasty.error(mContext, "当前时间与已有计划重叠，请重新设置").show();
                    return;
                }
                boolean target = UVTaskUtil.getUvTask().stream().anyMatch(task ->
                        DisinfectController.isBetween(task.getStartTimeHour(), task.getStartTimeMinute(), task.getEndTimeHour(), task.getEndTimeMinute()));
                MyQueue.getInstance(MyQueue.TYPE_CONTROLLER).addTask(new QueueIOTask(() -> MyUtil.autoControlOperateThird(RoomController.uvLight, target)));
                adapter.setDataList(UVTaskUtil.getUvTask());
                UserLog.operate("本机屏幕", "新增消毒计划成功：" + startTimeStr + "→" + endTimeStr);
                dismiss();
            }
        });
    }
}
