package page.info.michaelgardiner;


import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import org.wikiwebserver.core.WareHouse;
import org.wikiwebserver.handler.http.HTTPException;
import org.wikiwebserver.handler.http.interfaces.HTTPResponder;
import org.wikiwebserver.html.HTMLHelper.ContainerType;

import page.config.SiteTemplatedPage;

import static org.wikiwebserver.html.HTMLHelper.*;

public class Index extends SiteTemplatedPage implements HTTPResponder {
    
    public void generate() throws HTTPException {
    	
    	String host = getRequest().getHeaders().getFirst("Host");
    	if (host != null && !host.endsWith("michaelgardiner.info")) {
            throw new HTTPException(301,
                    "www.michaelgardiner.info preferred", 
                    "http://www.michaelgardiner.info/");             
    	}
        
        setTitle("Michael John Gardiner");
        String[] keywords =  { "Michael John Gardiner", "Michael Gardiner", "Michael J Gardiner", 
                               "Mike Gardiner", "Michael", "Gardiner" };
        setKeywords(Arrays.asList(keywords));
        
        addResourceRoot("/templates/info/michaelgardiner/");
        addCSSLink("profile.css");
        
        String meImage = image("mgardiner_small.jpg", "Michael John Gardiner", "id='profileImage'"); 
        
        String src = WareHouse.getUrlPathForClass(page.image.Email.class)
                   + "?email=mike-at-wikiwebserver-dot-org&size=12";
        
        String emailImage = image(src, "Bot proof email", "id='emailImage'");        
        
        Calendar birthday = Calendar.getInstance();
        birthday.set(1980, Calendar.DECEMBER, 26);        

        append(h(1, "Profile")  + meImage +
                div("profile", 
                    h(2, "id='name'", "Dr Michael John Gardiner PhD BSc Hons") +
                    detail("Email:", emailImage) +
                    detail("Age:", String.valueOf(getAge(birthday))) +
                    detail("Location:", "Tonbridge, Kent, ENGLAND.")) +
                    cleardiv());
        
        append(h(2, "Occupation") +
               p("I am currently working for PayPoint.net developing Internet " +
               	 " payment systems to simplify transferring money online."));       
        
        Calendar webServerStart = Calendar.getInstance();
        webServerStart.set(2002, Calendar.JULY, 01);  
        
        append(h(2, "Interests") + h(3, "Internet Server Technologies") +
                p("Since " + webServerStart.get(Calendar.YEAR) + " I have been developing a" +
                  " web server in my free time. The web server is written in pure Java and does" +
                  " not use any external libraries. Initially this was a very simple HTTP 1.0" +
                  " webserver for serving static content, this has evolved over the last " + 
                  getAge(webServerStart) + " years into a fast, reliable and extremely versatile" +
                  " web server supporting HTTP 1.1 with some very advanced features.") +   
                p("The development of the web server has spawned a number of applications:") +
                p(a("http://hostyourself.net/", "MyDiskServer") + " is a simple to configure web" +
                  " server for sharing files online. The server generates advanced file listings," +
                  " image thumbnails, includes audio streaming support, dynamic zipping of multiple" +
                  " files for downloading, user administration, bandwidth controls and many other" +
                  " powerful features. MyDiskServer is installed on over 10,000 computers" +
                  " world-wide.") +
                p(a("http://www.jads.co.uk/", "JADS") + " is an application designed to display news" +
                  " and announcements on large plasma screens. By using a web-based user interface," +
                  " the screen is easily updatable from any computer using just a web browser. JADS" +
                  " is used at a number of Universities and Schools throughout the UK.") +
                  p("More recently I have been developing " + a("http://www.wikiwebserver.org/", "WikiWebServer") + 
                  ". WikiWebServer is a web application server similar to Apache Tomcat with an easy to" +
                  " view and modify design that I use as a playground to develop, test and host new " +
                  " ideas online."));         
        
        append(h(2, "Education") +
                div("education", 
                    detail("PhD Electronic Engineering", "University of Kent (2002-2006)") +
                    detail("BSC Computer Science", "University of Kent (1999-2002)") +
                    detail("A levels", "Thomas Mills High School, Framlingham, Suffolk (1997-1999)") +
                    detail("GCSEs", "Debenham High School, Debenham, Suffolk (1992-1997)")) +
                    cleardiv());

    }
    
    private static int getAge(Calendar birthday) {
        Calendar today = Calendar.getInstance();
        today.setTime(new Date());
        
        Calendar lastBirthday = Calendar.getInstance();
        lastBirthday.set(today.get(Calendar.YEAR),
                         birthday.get(Calendar.MONTH),
                         birthday.get(Calendar.DAY_OF_MONTH));
        
        if (today.before(lastBirthday)) {
            lastBirthday.add(Calendar.YEAR, -1);
        }
        return lastBirthday.get(Calendar.YEAR) - birthday.get(Calendar.YEAR);
    }   
    
    private String detail(String key, String value) {
        return div(ContainerType.CLASS, "info", 
                   div(ContainerType.CLASS, "key", key) +
                   div(ContainerType.CLASS, "value", value));
    }
}