package com.multictrl.modules.job.dto;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.multictrl.common.validator.group.AddGroup;
import com.multictrl.common.validator.group.DefaultGroup;
import com.multictrl.common.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.AssertTrue;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Null;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Range;

import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 定时任务
 *
 * @author Sdy
 * @since 1.0.0
 */
@Data
@Schema(title = "定时任务")
public class ScheduleJobDTO implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    @Schema(description = "id")
    @Null(message = "{id.null}", groups = AddGroup.class)
    @NotNull(message = "{id.require}", groups = UpdateGroup.class)
    private Long id;

    @NotBlank(message = "任务名称不能为空", groups = DefaultGroup.class)
    @Schema(description = "任务名称")
    private String jobName;

    @Schema(description = "任务执行类")
    private String beanName = "routeTask";

    @NotNull(message = "定时类型不能为空", groups = DefaultGroup.class)
    @Schema(description = "定时类型 1指定时间 2每天几点 3每周周几几点 4每月几号几点")
    private Integer cronType;

    @Schema(description = "年")
    private Integer year;

    @Schema(description = "月")
    private Integer month;

    @Schema(description = "日")
    private List<Integer> day;

    @Schema(description = "时")
    private Integer hour;

    @Schema(description = "分")
    private Integer minute;

    @Schema(description = "参数(航线标识)")
    private String params;

    @Schema(description = "任务状态  0：暂停  1：正常 -1失效")
    @Range(min = -1, max = 1, message = "{schedule.status.range}", groups = DefaultGroup.class)
    private Integer status;

    @Schema(description = "备注")
    private String remark;

    @Schema(description = "创建时间")
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private Date createDate;

    @NotBlank(message = "机库sn不能为空", groups = DefaultGroup.class)
    @Schema(description = "机库sn")
    private String dockSn;

    @Schema(description = "航线名称")
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private String routeName;

    @Schema(description = "机库名称")
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private String dockName;

    @Schema(description = "下次执行时间")
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private String nextRunTime;

    @Schema(description = "执行规则")
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private String cronRules;

    @JsonIgnore
    @Schema(hidden = true)
    @AssertTrue(message = "参数不能为空", groups = DefaultGroup.class)
    public boolean isParamsValid() {
        return !Objects.equals(beanName, "routeTask") || StrUtil.isNotBlank(params);
    }

    @JsonIgnore
    @Schema(hidden = true)
    @AssertTrue(message = "时间格式有问题", groups = DefaultGroup.class)
    public boolean isCronTypeValid() {
        if (cronType == null) return false;
        return switch (cronType) {
            case 1 -> // 指定时间（年、月、日、时、分）
                    year != null && month != null && !day.isEmpty() && hour != null && minute != null;
            case 2 -> // 每天几点（时、分）
                    hour != null && minute != null;
            case 3 ->// 每周周几几点（周几、时、分）
                    !day.isEmpty() && hour != null && minute != null && day.stream().allMatch(d -> d >= 1 && d <= 7);
            case 4 ->// 每月几号几点（日、时、分）
                    !day.isEmpty() && hour != null && minute != null && day.stream().allMatch(d -> d >= 1 && d <= 31);
            default -> false;
        };
    }

    /**
     * 是否断点续飞
     */
    @Schema(description = "是否断点续飞")
    private Boolean isBreakpointFly;
    /**
     * 断点续飞最大时间
     */
    @Schema(description = "断点续飞最大时间 HH:mm:ss")
    private String breakMaxTime;

    /**
     * 断点续飞最小时间
     */
    @Schema(description = "断点续飞最小时间 HH:mm:ss")
    private String breakMinTime;

    @JsonIgnore
    @Schema(hidden = true)
    @AssertTrue(message = "断点续飞时间不能为空")
    public boolean isBreakTimeValid() {
        if (isBreakpointFly) {
            return StringUtils.isNotBlank(breakMaxTime) && StringUtils.isNotBlank(breakMinTime);
        } else {
            return true;
        }
    }
}
