package page.example;


import org.wikiwebserver.core.WareHouse;
import org.wikiwebserver.handler.http.interfaces.*;
import page.config.SiteTemplatedPage;

import static org.wikiwebserver.html.HTMLHelper.*;

public class Index extends SiteTemplatedPage implements HTTPResponder {
    
    public void generate() {
        
        setTitle("Examples - WikiWebServer.org");
        addResourceRoot("/templates/default/examples/");
        
        append(h(1, "Examples"));
        append(p("Example WikiWebServer classes."));
        append(h(2, "Simple Examples:"));
        
        String[][] basicList = {
           { getUrl(page.example.HelloWorld.class), "HelloWorld",
               "Returning plain Hello World text" },
           { getUrl(page.example.HelloWorldPage.class), "HelloWorldPage",
               "Generating a Hello World page by extending TemplatedPage" },     
           { getUrl(page.example.HelloWorldSpeech.class), "HelloWorldSpeech",
               "Audio version of Hello World" },                 
           { getUrl(page.example.Notepad.class), "Notepad",
               "Storing text in the persistent store" },                                   
           { getUrl(page.example.PersistentData.class), "PersistentData",
               "Storing more data in the persistent store" },                                      
           { getUrl(page.example.Primes.class), "Primes",
               "Generating prime numbers" },                    
           { getUrl(page.example.ProtectedPage.class), "ProtectedPage",
               "Protecting a page from guest access" },    
           { getUrl(page.example.VelocityEnforcementTestPage.class), "VelocityEnforcementTestPage",
               "Controlling the rate at which requests for a page are served." },                  
        };
        append(exampleList(basicList));
        
        append(h(2, "Image Generation:"));   
        
        String[][] imageList = {
           { getUrl(page.image.Example.class), "Example",
               "Simple example of how to draw graphics elements" },     
           { getUrl(page.image.CacheableExample.class), "CacheableExample",
               "Simple example made cacheable" },                  
           { getUrl(page.image.Email.class), "Email",
               "Generating a bot proof email image" },
           { getUrl(page.image.Counter.class), "Counter",
               "Generating and returning a stored hit count" },  
           { getUrl(page.image.CAPTCHAImage.class), "CAPTCHAImage",
               "CAPTCHA Image for detecting humans" },     
           { getUrl(page.image.Pie.class) + "?d=1,2,3,4,5,6,7,8", "Pie",
               "Generating and returning a pie chart" },   
           { getUrl(page.image.LineGraph.class), "LineGraph",
               "Generating and returning a labeled line graph" },                  
           { getUrl(page.image.Fractal.class) + "?w=600&h=500", "Fractal",
               "Generating and returning a Mandlebrot fractal image" }, 
           { getUrl(page.image.Plasma.class) + "?w=600&h=500", "Plasma",
               "Generating and returning a Plasma image" },  
           { getUrl(page.image.SVGExample.class), "SVGExample",
               "Extends the simple example and uses Apache batik to output SVG" },   
           { getUrl(page.image.PDFExample.class), "PDFExample",
               "Extends the simple example and uses iText to output PDF" },  
           { getUrl(page.example.ThreeDimensionalTestPage.class), "ThreeDimensionalTestPage",
               "Displays a three dimensional test applet on a page." },                 
        };
        
        append(exampleList(imageList));
        
        append(h(2, "Communications:"));        
        
        String[][] commsList = {
           { getUrl(page.example.ViewRequestHeaders.class), "ViewRequestHeaders",
               "Read and display request headers sent from browser" },       
           { getUrl(page.example.TestRequestProcessing.class), "TestRequestProcessing",
               "Send an HTTP request to WikiWebServer and view response headers" },                
           { getUrl(page.tools.management.FileUploadPage.class), "UploadPage",
               "Upload files to user directory" },   
           { getUrl(page.example.SiteScraperPage.class), "SiteScraperPage",
               "Retrieve and apply regular expression to external page" },                   
           { getUrl(page.example.HTTPPoster.class), "HTTPPoster",
               "Sending and reading remote requests" },                     
           { getUrl(page.example.secpay.SECPayRawAPITest.class), "SECPayRawAPITest",
               "Construction of Raw SOAP and XMLRPC requests for SECPay API integration" },
           { getUrl(page.example.XSSToolkit.class), "XSSToolkit",
               "Toolkit for testing cross site scripting" },                 
        };
        
        append(exampleList(commsList));
        
        append(h(2, "Displaying Live Data:"));  
        
        String[][] updateList = {
           { getUrl(page.example.AJAXUpdate.class), "AJAXUpdate",
               "Keeping a page element up to date by AJAX" },       
           { getUrl(page.example.PushUpdate.class), "PushUpdate",
               "Keeping a page element up to date by server side push" },                   
           /*{ getUrl(page.example.betfair.BetFairWatcher.class), "BetFairWatcher",
               "Monitors BetFair markets and renders live graphs (UK only)" }*/    
        };
        
        append(exampleList(updateList));
        
        append(h(2, "Processing Data:"));         
        
        String[][] processingList = {
        
           { getUrl(page.example.LiveFormTest.class), "LiveFormTest",
               "Live server-side validation of form data using Ajax" },
           { getUrl(page.example.Base64EncoderPage.class), "Base64EncoderPage",
               "Base64 encoder and decoder" },       
           { getUrl(page.example.URLEncoderPage.class), "URLEncoderPage",
               "URL encoder and decoder" },  
           { getUrl(page.example.HTMLEntityEncoderPage.class), "HTMLEntityEncoderPage",
               "HTML entity encoder and decoder" },     
           { getUrl(page.example.QuoteEncoderPage.class), "QuoteEncoderPage",
               "Quote encoder and decoder for quoted fields" },                      
           { getUrl(page.example.EncryptionPage.class), "EncryptionPage",
               "Encrypts and decrypts plain text" },                 
           { getUrl(page.example.LineCount.class), "LineCount",
               "Counts number of lines of Java source code" },                
           { getUrl(page.example.ViewProperties.class), "ViewProperties",
               "Display system properties" },       
           { getUrl(page.example.IPToCountryLookup.class), "IPToCountryLookup",
               "Lookup an IP address to find the country it is assigned to" },                
        };
        
        append(exampleList(processingList));
        
        append(h(2, "Management:"));         
        
        String[][] managementList = {
           { getUrl(page.tools.stats.Status.class), "Status",
               "View WikiWebServer status and statistics"  },        
           { getUrl(page.tools.stats.ThreadStatus.class), "ThreadStatus",
               "View status and stack trace of executing threads"  },                 
           { getUrl(page.tools.admin.ManagementTools.class), "ManagementTools",
               "Server management options"  },  
           { getUrl(page.tools.management.LogViewer.class), "LogViewer",
               "View logs maintained by WikiWebServer" },             
        };        
        
        append(exampleList(managementList));        
        
        append(h(2, "Advanced Examples:"));
        
        String[][] advancedList = {
            { getUrl(page.example.ImageGallery.class), "ImageGallery",
                "Image gallery for displaying and navigating folders of images" },                    
            { getUrl(page.example.FractalGallery.class), "FractalGallery",
                "Generating and present dynamic images" }, 
            { getUrl(page.example.Mobiliser.class), "Mobiliser",
                "Connects to external pages and reformats them for mobile devices" },     
            { getUrl(page.example.TextToSpeech.class), "TextToSpeech",
                "Converts passages of text into audible speech" },                
        };
        append(exampleList(advancedList));     
        
        
    }
    
    private String getUrl(Class<?> cla) {
        return WareHouse.getUrlPathForClass(cla);
    }    
    
    private String exampleList(String[][] basicList) {
        StringBuilder list = new StringBuilder();
        list.append("<ul>");
        for (String[] line : basicList) {
            String path = line[0];            
            list.append("<li>");
            list.append(a(path, line[1]));
            list.append(" - ");
            list.append(line[2]);
            if (path.endsWith(".class") || path.contains(".class?")) {
                path = path.substring(0, path.indexOf(".class")) + ".java";
                String editLink = WareHouse.SOURCE_EDITOR_URL + "?path=" + path;
                list.append(" [" + a(editLink, "source") + "]");
            }
            list.append("</li>");
        }
        list.append("</ul>");
        return list.toString();
    }
}


