package guessNumber;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import java.util.Random;


public class UserNumberBean {

    Integer userNumber = null;
    Integer randomInt = null;
    String response = null;

    private int maximum = Integer.MAX_VALUE;
    private int minimum = Integer.MIN_VALUE;;

    protected String[] status = null;

    public UserNumberBean() {
        Random randomGR = new Random();
        randomInt = new Integer(randomGR.nextInt(10));
    }

    public void setUserNumber(Integer user_number) {
        userNumber = user_number;
    }

    public Integer getUserNumber() {
        return userNumber;
    }

    public String getResponse() {
        if (userNumber != null && userNumber.compareTo(randomInt) == 0) {
            return "Yay! You got it!";
        } else {
            return "Sorry, " + userNumber + " is incorrect.";
        }
    }

    public String[] getStatus() {
        return status;
    }

    public void setStatus(String[] newStatus) {
        status = newStatus;
    }

    public int getMaximum() {
        return (this.maximum);
    }

    public void setMaximum(int maximum) {
        this.maximum = maximum;
    }

    public int getMinimum() {
        return (this.minimum);
    }

    public void setMinimum(int minimum) {
        this.minimum = minimum;
    }

    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if (context == null || component == null) {
            throw new NullPointerException();
        }
        
        if (value != null) {
    		Integer i = (Integer)value;
            int converted = i.intValue();
            if (converted > maximum) {
            	Object[] objects = {new Integer(minimum),new Integer(maximum)};
            	FacesMessage msg = MessageFactory.getMessage(context,Validator.NOT_IN_RANGE_MESSAGE_ID,objects);
            	throw new ValidatorException(msg);
            }
            if (converted < minimum ) {
            	Object[] objects = {new Integer(minimum),new Integer(maximum)};
            	FacesMessage msg = MessageFactory.getMessage(context,Validator.NOT_IN_RANGE_MESSAGE_ID,objects);
            	throw new ValidatorException(msg);
            }
        }
    }
}