/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ContactPanel.java
 *
 * Created on Jan 8, 2010, 4:37:48 PM
 */

package usda.weru.weps.startup;

import java.awt.Component;
import org.netbeans.spi.wizard.WizardPage;
import usda.weru.util.ConfigData;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class UserPage extends WizardPage {
    private static final long serialVersionUID = 1L;

    private WelcomeWizard c_wizard;

    private boolean c_loaded;

    /** Creates new form ContactPanel */
    public UserPage() {
        initComponents();        
    }

	/**
	 *
	 * @param wizard
	 */
	public UserPage(WelcomeWizard wizard){
        this();
        c_wizard = wizard;
    }

	/**
	 *
	 * @return
	 */
	public static String getDescription(){
        return "User Information";
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameLabel = new javax.swing.JLabel();
        emailLabel = new javax.swing.JLabel();
        emailField = new javax.swing.JTextField();
        nameField = new javax.swing.JTextField();

        nameLabel.setFont(nameLabel.getFont().deriveFont(nameLabel.getFont().getStyle() & ~java.awt.Font.BOLD));
        nameLabel.setText("Full name:");

        emailLabel.setFont(emailLabel.getFont().deriveFont(emailLabel.getFont().getStyle() & ~java.awt.Font.BOLD));
        emailLabel.setText("Email address:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(emailLabel)
                    .addComponent(nameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(nameField, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE)
                    .addComponent(emailField, javax.swing.GroupLayout.DEFAULT_SIZE, 307, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(nameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(emailLabel)
                    .addComponent(emailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(243, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField emailField;
    private javax.swing.JLabel emailLabel;
    private javax.swing.JTextField nameField;
    private javax.swing.JLabel nameLabel;
    // End of variables declaration//GEN-END:variables

	/**
	 *
	 */
	@Override
    protected void renderingPage() {
        if(c_loaded){
            //only need to load once
            return;
        }
        nameField.setText(c_wizard.getValue(ConfigData.UserFullName));
        //grab the first not null email setting
        emailField.setText(firstNotNull(true, c_wizard.getValue(ConfigData.EmailSender), c_wizard.getValue(ConfigData.MantisEmail)));
    }

	/**
	 *
	 * @param component
	 * @param event
	 * @return
	 */
	@Override
    protected String validateContents(Component component, Object event) {
        if(nameField.equals(component)){
            return firstNotNull(validateName(), validateEmail());
        }
        else if (emailField.equals(component)){
            return firstNotNull(validateEmail(), validateName());
        }
        else{
            return firstNotNull(validateName(), validateEmail());
        }
    }

    private String validateName(){
        String name = nameField.getText();
        String result = validateRequired("Full name", name);
        if(result == null){
            //set the value
            c_wizard.setValue(ConfigData.UserFullName, name);
        }
        return result;
    }

    private String validateRequired(String name, String value){
            boolean error = value== null || value.trim().length() == 0;
            return error ? (name + " is required.") : null;
    }

    private String validateEmail(){
        String email = emailField.getText();
        String result = firstNotNull(validateRequired("Email address", email), validateEmailAddress("Email address", email));
        if(result == null){
            //set the value, we have two email settings, might want to consolidate
            c_wizard.setValue(ConfigData.EmailSender, email);
            c_wizard.setValue(ConfigData.MantisEmail, email);
        }
        return result;
    }
    private String validateEmailAddress(String name, String email){
            boolean error = !email.contains("@") || !email.contains(".");
            return error ? (name + " is not valid.") : null;
    }


    private String firstNotNull(boolean trimToNull, String... ss){
        for(int i = 0; i < ss.length; i++){
            String s = ss[i];
            if(s == null){
                continue;
            }
            s = s.trim();
            s = s.length() > 0 ? s : null;
            ss[i] = s;
        }
        return firstNotNull(ss);
    }

    private String firstNotNull(String... ss){
        for (String s : ss){
            if (s != null){
                return s;
            }
        }
        return null;
    }


    


}
