package page.example;

import java.util.ArrayList;
import java.util.Collection;

import org.wikiwebserver.handler.http.FormData;
import org.wikiwebserver.handler.http.interfaces.*;
import org.wikiwebserver.util.ValidationCorrection;
import org.wikiwebserver.util.ValidationError;
import org.wikiwebserver.util.ValidationWarning;
import org.wikiwebserver.util.Validator;

import page.tools.html.LiveFormPage;

public class LiveFormTest extends LiveFormPage implements HTTPResponder {
    
    public String getFormName() {
        return getClass().getName();
    }
    
    public void complete(FormData formData) {
        
    }    
    
    public void populate() {
        
    }
	
    public void generate() {
        
        setTitle("Live Form Test - WikiWebServer.org");
        
        append("<h1>Live Form</h1>");
        
        append(liveTextField("e-mail", "Email"));
        append(livePasswordField("password", "Password"));
        append(livePasswordField("confirmpassword", "Confirm Password"));
        
        append(liveTextField("fullname", "Full name"));
        append(liveTextArea("profile", "Profile"));
        append(liveTextField("website", "Website"));
        append(liveCheckbox("newsletter", "News letter", "Send weekly news letter"));
        append(liveCheckbox("lockedoff", "Locked off", "Non selectable option"));
        append(liveCheckbox("lockedon",  "Locked on", "Can not be disabled")); 
        
        Collection<String> colours = new ArrayList<String>();
        colours.add("Red");
        colours.add("Blue");
        colours.add("Green");
        
        append(liveRadioOptions("colour", "Favourite Colour", colours));
        
        Collection<String> animals = new ArrayList<String>();
        animals.add("Cat");
        animals.add("Dog");
        animals.add("Rabbit");
        
        append(liveDropdown("animal", "Favourite Animal", animals));

    }
    
    public void validate(String name, String value) 
        throws ValidationCorrection, ValidationError, ValidationWarning {
        //System.out.println(name + "--" + value);
        
        if ("fullname".equals(name)) {
            if (value == null) return;
            String corrected = Validator.upperCaseFirstLetterOfWords(value);
            if (!corrected.equals(value)) {
                throw new ValidationCorrection("Corrected case", corrected);
            } 
            else if (value.split(" ").length == 1) {
                throw new ValidationError("Please provide first and last name");
            }
        }
        else if ("e-mail".equals(name)) {
            if (value == null) return;
            Validator.validateEmail(value);
        } 
        else if ("password".equals(name)) {
            if (value == null) return;
            Validator.validatePassword(value);                   
        }
        else if ("confirmpassword".equals(name)) {
            if (value == null) return;
            String pw = getInputValue("password");
            if (value.length() == 0 || !value.equals(pw)) {
                throw new ValidationError("Passwords must match");
            }
        } 
        else if ("website".equals(name)) {
            if (value == null) return;
            if (value.length() > 0) {
                Validator.validateWebAddress(value);
            }
        }
        else if ("profile".equals(name)) {
            if (value == null) return;
            if (value.contains("fuck")) {
                value = value.replaceAll("fuck", "");
                throw new ValidationCorrection("Bad word removed", value);
            }
        }        
        else if ("colour".equals(name)) {
            if (value == null) return;
            if (value.equals("Blue")) {
                throw new ValidationCorrection("Out of blue");
            }
        } 
        else if ("animal".equals(name)) {
            if (value == null) return;
            if (value.equals("Rabbit")) {
                throw new ValidationCorrection("No rabbits allowed!");
            }
        } 
        else if ("lockedoff".equals(name)) {
            if (value == null) return;
            if (value.length() > 0) {
                throw new ValidationCorrection("Disabled", "");    
            }
        } 
        else if ("lockedon".equals(name)) {
            if (value == null || value.length() == 0) {
                throw new ValidationCorrection("Re enabled", "Can not be disabled");    
            }
        }         
    }

}
