package ru.ulstu.odin.model;

import org.hibernate.validator.constraints.Email;
import ru.ulstu.odin.model.annotation.OdinString;
import ru.ulstu.odin.model.annotation.OdinString.OdinStringType;

import javax.validation.constraints.Size;
import java.lang.reflect.Field;

import static ru.ulstu.odin.model.annotation.OdinString.OdinStringType.EMAIL;

public class OdinStringField extends OdinField {
    private final int minLength;
    private final int maxLength;
    private final OdinStringType type;

    public OdinStringField(Field field) {
        super(field, OdinFieldType.STRING);
        this.minLength = getValue(Size.class, "min", Integer.class);
        this.maxLength = getValue(Size.class, "max", Integer.class);
        this.type = field.isAnnotationPresent(Email.class)
                ? EMAIL
                : getValue(OdinString.class, "type", OdinStringType.class);
    }

    public int getMinLength() {
        return minLength;
    }

    public int getMaxLength() {
        return maxLength;
    }

    public String getType() {
        return type.toString();
    }
}
