package com.r11.tools.model.constraints;

import com.r11.tools.model.MockedMessage;
import com.r11.tools.model.MockedMessageDto;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.springframework.http.HttpStatus;

/**
 * It's validator class. It checks if status value of {@link MockedMessageDto} is within bonds of http status values map.
 * @author Rafał Żukowicz
 */
public class HttpCodeValidation implements ConstraintValidator<HttpCode, Integer> {
    private Set<Integer> allowedValues;

    /**
     * Initializes {@link #allowedValues} with possible http status values.
     * @param targetEnum HttpCode context
     */
    @Override
    public void initialize(HttpCode targetEnum) {
        allowedValues = Stream.of(HttpStatus.values())
                .map(HttpStatus::value)
                .collect(Collectors.toSet());
    }

    /**
     * @param integer value of {@link MockedMessageDto#getHttpStatus()} or {@link MockedMessage#getHttpStatus()}
     * @param context context for validation
     * @return true if valid
     */
    @Override
    public boolean isValid(Integer integer, ConstraintValidatorContext context) {
        return allowedValues.contains(integer);
    }
}
