package jsp.beans.userinfo;

import java.io.*;
import java.util.*;
import jsp.util.*;

/**
 * This class contains information about a user. It's used to show
 * how a bean can be used to validate user input and format output
 * so it's suitable for HTML.
 *
 * @author Hans Bergsten, Gefion software <hans@gefionsoftware.com>
 * @version 1.0
 */
public class UserInfoBean implements Serializable {
    // Validation constants
    private static String DATE_FORMAT_PATTERN = "dd-MM-yyyy";
    private static String[] SEX_LIST = {"homme", "femme"};
    private static int MIN_LUCKY_NUMBER = 0;
    private static int MAX_LUCKY_NUMBER = 100;

    // Properties
    private String birthDate;
    private String birthDateInput;
    private String emailAddr;
    private String emailAddrInput;
    private String[] interests;
    private String luckyNumber;
    private String luckyNumberInput;
    private String sex;
    private String sexInput;
    private String userName;
    private String userNameInput;
    private boolean isInitialized;

    /**
     * Returns the birthDate property value
     */
    public String getBirthDate() {
        return (birthDate == null ? "" : birthDate);
    }

    /**
     * Returns the birthDate property value,
     * with all HTML special characters converted
     * to HTML character entities
     */
    public String getBirthDateFormatted() {
        return StringFormat.toHTMLString(getBirthDate());
    }

    /**
     * Sets the birthDate property value, if it's
     * valid
     */
    public void setBirthDate(String birthDate) {
        isInitialized = true;
        birthDateInput = birthDate;
        if (StringFormat.isValidDate(birthDate, DATE_FORMAT_PATTERN)) {
            this.birthDate = birthDate;
        }
    }

    /**
     * Returns the emailAddr property value
     */
    public String getEmailAddr() {
        return (emailAddr == null ? "" : emailAddr);
    }

    /**
     * Returns the emailAddr property value,
     * with all HTML special characters converted
     * to HTML character entities
     */
    public String getEmailAddrFormatted() {
        return StringFormat.toHTMLString(getEmailAddr());
    }

    /**
     * Sets the emailAddr property value, if it's
     * valid
     */
    public void setEmailAddr(String emailAddr) {
        isInitialized = true;
        emailAddrInput = emailAddr;
        if (StringFormat.isValidEmailAddr(emailAddr)) {
            this.emailAddr = emailAddr;
        }
    }

    /**
     * Returns the interests property value
     */
    public String[] getInterests() {
        return interests;
    }

    /**
     * Returns the interests property value,
     * with all HTML special characters converted
     * to HTML character entities
     */
    public String[] getInterestsFormatted() {
        String[] formatted = null;
        String[] interests = getInterests();
        if (interests != null) {
            formatted = new String[interests.length];
            for (int i = 0; i < interests.length; i++) {
                formatted[i] = StringFormat.toHTMLString(interests[i]);
            }
        }
        return formatted;
    }

    /**
     * Sets the interests property value
     */
    public void setInterests(String[] interests) {
        this.interests = interests;
    }

    /**
     * Returns the luckyNumber property value
     */
    public String getLuckyNumber() {
        return (luckyNumber == null ? "" : luckyNumber);
    }

    /**
     * Returns the luckyNumber property value,
     * with all HTML special characters converted
     * to HTML character entities
     */
    public String getLuckyNumberFormatted() {
        return StringFormat.toHTMLString(getLuckyNumber());
    }

    /**
     * Sets the luckyNumber property value, if it's
     * valid
     */
/*    public void setLuckyNumber(String luckyNumber) {
        isInitialized = true;
        luckyNumberInput = luckyNumber;
        if (StringFormat.isValidInteger(luckyNumber, MIN_LUCKY_NUMBER,
            MAX_LUCKY_NUMBER)) {
            this.luckyNumber = luckyNumber;
        }
    }
    */
    public void setLuckyNumber(String luckyNumber) {
    	isInitialized = true;
    	int j=0;
        isInitialized = true;
        luckyNumberInput = luckyNumber;
        for (int i=0; i < luckyNumber.length(); i++)
        	if (Character.isLetter(luckyNumber.charAt(i))) j++;
		
		if (j == luckyNumber.length()) this.luckyNumber = luckyNumber;
    }

    /**
     * Returns the sex property value
     */
    public String getSex() {
        return (sex == null ? "" : sex);
    }

    /**
     * Returns the sex property value,
     * with all HTML special characters converted
     * to HTML character entities
     */
    public String getSexFormatted() {
        return StringFormat.toHTMLString(getSex());
    }

    /**
     * Sets the sex property value, if it's
     * valid
     */
    public void setSex(String sex) {
        isInitialized = true;
        sexInput = sex;
        if (StringFormat.isValidString(sex, SEX_LIST, true)) {
            this.sex = sex;
        }
    }

    /**
     * Returns the userName property value
     */
    public String getUserName() {
        return (userName == null ? "" : userName);
    }

    /**
     * Returns the userName property value,
     * with all HTML special characters converted
     * to HTML character entities
     */
    public String getUserNameFormatted() {
        return StringFormat.toHTMLString(getUserName());
    }

    /**
     * Sets the userName property value
     */
    public void setUserName(String userName) {
    	int j=0;
        isInitialized = true;
        userNameInput = userName;
        for (int i=0; i < userName.length(); i++)
        	if (Character.isLetter(userName.charAt(i))) j++;
		
		if (j == userName.length()) this.userName = userName;
    }

    /**
     * Returns an HTML fragment with information about
     * all invalid property values, or an empty String
     * if all properties are valid.
     */
    public String getPropertyStatusMsg() {
        StringBuffer msg = new StringBuffer();
        if (!isInitialized()) {
            msg.append("Complétez tous les champs suivants :");
        }
        else if (!isValid()) {
            msg.append("Les champs suivants sont incomplets ou invalides : ");
            msg.append("<ul>");
            if (birthDate == null) {
                if (birthDateInput == null) {
                    msg.append("<li>Date de naissance manquante");
                }
                else {
                    msg.append("<li>Date de naissance invalide: " + birthDateInput);
                }
            }
            if (emailAddr == null) {
                if (emailAddrInput == null) {
                    msg.append("<li>Adresse email manquante");
                }
                else {
                    msg.append("<li>Adresse email invalide: " + emailAddrInput);
                }
            }
            if (luckyNumber == null) {
                if (luckyNumberInput == null) {
                    msg.append("<li>Réponse clé manquante");
                }
                else {
                    msg.append("<li>Réponse clé invalide: " + luckyNumberInput);
                }
            }
            if (sex == null) {
                if (sexInput == null) {
                    msg.append("<li>Sexe manquant");
                }
                else {
                    msg.append("<li>Sexe invalide: " + sexInput);
                }
            }
            if (userName == null) {
            	if (userNameInput == null) {
                	msg.append("<li>Nom manquant");
                }
                else {
                	msg.append("<li>Nom invalide: " + userNameInput);
                }
            }
            msg.append("</ul>");
            msg.append("Veuillez rentrer des données valides");
        }
        else {
        	msg.append("Données reçues :");
        	msg.append("<ul>");
        	msg.append("<li>Nom: " + userName);
        	msg.append("<li>Email: " + emailAddr);
        	msg.append("<li>Date de naissance: " + birthDate);
        	msg.append("<li>Sexe: " + sexInput);
        	msg.append("<li>Question secrète: " + luckyNumber);
        	msg.append("</ul>");
            msg.append("Merci d'avoir participé !");
        }
        return msg.toString();
    }

    /**
     * Returns true if all property values have valid values
     * (they are only set if the value is valid).
     */
    public boolean isValid() {
        return isInitialized() &&
            birthDate != null &&
            emailAddr != null &&
            luckyNumber != null &&
            sex != null &&
            userName != null;
    }

    /**
     * Returns true if at least one property has been set
     */
    private boolean isInitialized() {
        return isInitialized;
    }
}





