package cn.estsh.i3plus.pojo.aps.validator;

import cn.estsh.i3plus.pojo.aps.annotation.ShiftTimeAnnotation;
import cn.estsh.i3plus.pojo.aps.model.TimeBlock;
import cn.estsh.i3plus.pojo.base.util.StringUtil;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class ShiftTimeValidator implements ConstraintValidator<ShiftTimeAnnotation, String> {
    SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");

    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        if (StringUtil.isEmpty(s)) {
            return false;
        }
        for (String strTime : s.split(",")) {
            String[] times = strTime.split("-");
            if (times.length != 2) {
                return false;
            }

            try {
                TimeBlock timeBlock = new TimeBlock();
                timeBlock.setBegin(timeFormat.parse(times[0]));
                timeBlock.setEnd(timeFormat.parse(times[1]));
            } catch (ParseException e) {
                return false;
            }
        }
        return true;
    }
}
