package page.misc;

import org.wikiwebserver.core.WareHouse;
import org.wikiwebserver.core.WikiMap;
import org.wikiwebserver.handler.http.HTTPException;
import org.wikiwebserver.handler.http.HTTPRequest;
import org.wikiwebserver.handler.http.HTTPResponse;
import org.wikiwebserver.handler.http.interfaces.HTTPResponder;
import org.wikiwebserver.html.HTMLHelper;
import org.wikiwebserver.html.TemplatedPage;

import page.config.SiteTemplatedPage;

import static org.wikiwebserver.html.HTMLHelper.*;

public class DevelopmentTips extends SiteTemplatedPage implements HTTPResponder {

    public static final String LF = "\r\n";
    
    private String resourcePath = "/templates/default/sandbox/";
	
    public void generate() throws HTTPException {

        setTitle("Development Tips - WikiWebServer.org");
        addResourceRoot(resourcePath);
        addCSSLink("sandbox.css");
        
        String editor = WareHouse.SOURCE_EDITOR_URL + "?path=";
        String templatedPage = editor + WareHouse.getUrlPathForSource(TemplatedPage.class);
        String htmlHelper = editor + WareHouse.getUrlPathForSource(HTMLHelper.class);
        String httpResponder = editor + WareHouse.getUrlPathForSource(HTTPResponder.class);
        String httpException = editor + WareHouse.getUrlPathForSource(HTTPException.class);
        String httpRequest = editor + WareHouse.getUrlPathForSource(HTTPRequest.class);
        String httpResponse = editor + WareHouse.getUrlPathForSource(HTTPResponse.class);
        String wikiMap = editor + WareHouse.getUrlPathForSource(WikiMap.class);      

        append(h(1, "Development Tips"));
        
        append(h(2, "Returning basic content"));
        
        String code = "public class MyTestPage implements HTTPResponder {\n" 
            + "  respond(HTTPHandler conn) {\n"
            + "    return \"HelloWorld!\";\n"
            + "  }\n"
            + "}";
        
        append(p("Implement the " + a(httpResponder, "HTTPResponder") +
                 " interface and return the Object, such as a String, that you" +
                 " want to display to visitors." + codeBox(code)));
       
        append(p("Valid response types include byte[], String, BufferedImage and " +
        		 a(httpException, "HTTPException") + "."));
        
        append(h(2, "Returning a templated page"));
        
        code = "public class MyTestPage extends TemplatedPage implements HTTPResponder {\n" 
            + "  generate() {\n"
            + "    setTitle(\"HelloWorld\");\n"
            + "    append(\"HelloWorld\");\n"
            + "  }\n"
            + "}";
        
        append(p("Extend the " + a(templatedPage, "TemplatedPage") +
                 " class to return a formatted HTML page." + codeBox(code)));   
        
        append(h(2, "Get help writing HTML"));
        
        code = "append(h(1 \"This is a large heading\"));\n"
            + "append(p(\"This is a paragraph.\"))\n"
            + "append(image(\"image.jpg\"))\n"
            + "append(a(\"http://www.google.com/\", \"A link to google\"))\n"
            + "append(form(textfield(\"name\", name) + br() + submitButton(\"action\", \"Save\")));";
        
        append(p("Add a static import for " + a(htmlHelper, "HTMLHelper") +
                 " to format pages without wrting HTML." + codeBox(code)));          
        
        append(h(2, "Reading form data"));
        
        code = "// Read where the visitor came from\n"
            + "Map<String, String> reqHeaders = conn.getRequest().getHeaders();\n"
            + "String referer = reqHeaders.get(\"Referer\");\n\n"
            + "// Read the input field named \"name\" sent from the browser\n"
            + "Map<String, String> formData = conn.getRequest().getFormData();\n"
            + "String sentText = formData.get(\"name\");\n";
        
        append(p("Headers and form data sent from the browser can be read from the " +
                a(httpRequest, "HTTPRequest") + "." + codeBox(code)));
        
        
        append(h(2, "Specify the content type"));
        
        code = "Map<String, String> resHeaders = conn.getResponse().getHeaders();\n"
            + "resHeaders.put(\"Content-Type\", \"application/rss+xml\");";
        
        append(p("The default content type is text/html. The content type, and other" +
        		 "response headers can be specified in the " +
        		 a(httpResponse, "HTTPResponse") + ".") + codeBox(code));

        code = "// Read an object from MyDataStore with the name MyDataName\n"
            + "Object obj = WareHouse.getWikiMap(\"MyDataStore\").get(\"MyDataName\");\n\n"
            + "// Put the current date into MyDataStore, with the name LastAccessed\n"
            + "long dateTime = System.currentTimeMillis();\n"
            + "WareHouse.getWikiMap(\"MyDataStore\").put(\"LastAccessed\", dateTime));\n\n" 
            + "// Put the number 27 into Age, within Mike, within UserDetails\n"
            + "WareHouse.getWikiMap(\"UserDetails\", \"Mike\").put(\"LastAccessed\", 27);";         
        
        append(h(2, "Store persistent data"));
        append(p(a(wikiMap, "WikiMaps") + " provide a thread-safe, persistent" +
        		" key-value mapping for storing data and sharing between connections." +
        		codeBox(code)));   

        append(p("Only immutable objects can be put into WikiMaps with the exception" +
        		 " of inner WikiMaps which facilitates a tree structure of data."));
    }
}
