package page.example.betfair;
/*
import java.io.IOException;

import org.wikiwebserver.core.WareHouse;
import org.wikiwebserver.core.WikiMap;
import org.wikiwebserver.handler.http.interfaces.HTTPResponder;
import org.wikiwebserver.util.BetFairHelper;

import page.tools.html.TemplatedPushPage;
import static org.wikiwebserver.html.HTMLHelper.*;

import com.betfair.www.publicapi.types.exchange.v3.*;

public class BetFairWatcher extends TemplatedPushPage implements HTTPResponder {

    private BetFairHelper betFair;
    
    private String username, password;
    private static int marketID = 2839755;
  
    private Market market;
    
    protected void generate() {
        
        if (getFormData() != null) {
            username = getFormData().getFirst("username");
            password = getFormData().getFirst("password");  
            marketID = Integer.parseInt(getFormData().getFirst("market"));            
            setUpdatePeriod(6100);
            setPushEnabled(true);
        }
        
        
        setTitle("BetFair Market Watcher - WikiWebServer.org");
        addResourceRoot("/templates/default/betfair/");
        addCSSLink("betfair.css");     
        
        String url = WareHouse.getUrlPathForClass(page.image.LineGraph.class)
                   + "?w=700&f=reciprocal&d=" + marketID;
        
        append("<h1>BetFair Market Watcher</h1>" +
               "<p>The BetFair market watcher uses the BetFair Java API to monitor " +
               " and render graphical betting information for live sporting events." +
               " (Requires BetFair login, secure connection recommended)</p>" +
               "<h2>Log in</h2>" +
               form(div("userdetails", div("loginDiv", "Not logged in") + 
                                       div("balanceDiv", "") +
                                       div("errorDiv", "")) + 
                    inputRow("Username: ", textfield("username", username), "(Betfair login)") +
                    inputRow("Password: ", passwordfield("password", password), "") +
                    inputRow("MarketID: ", textfield("market", String.valueOf(marketID)), "") +
                    "<div style='clear: left'></div>" + submitbutton("action", "Begin", "class='submit'")));

        	   
        append("<h2>Market</h2>" + div("marketDiv") +
               "<h2>Betting data</h2>" + div("dataDiv") +
               image(url, "Graph of odds", "id='graph'"));        	   

    }
    
    protected void begin() throws IOException {
        
        updateDiv("loginDiv", "Logging in as " + username + "...");
        
        betFair = new BetFairHelper(username, password);
        
        try {
            String result = betFair.login();
            updateDiv("loginDiv", result);
        } catch (Exception ex) {
            updateDiv("loginDiv", ex.getMessage());
            throw new IOException(ex.getMessage());
        }
        
        try {
            GetAccountFundsResp funds = betFair.getAcctFunds();
            updateDiv("balanceDiv", "Available Funds: <br/>£" + funds.getAvailBalance());
            
        } catch (Exception ex) {
            updateDiv("balanceDiv", ex.getMessage());
            throw new IOException(ex.getMessage());
        } 
        
        try {
            market = betFair.getMarket(marketID);
        } catch (Exception ex) {
            updateDiv("marketDiv", ex.getMessage());
            throw new IOException(ex.getMessage());
        }             
    }
    
    protected void update() throws IOException {          
        
        try {   

            StringBuilder buffer = new StringBuilder();
            buffer.append("<h3>" + market.getName() + "</h3>");
            buffer.append("<p>" + market.getMarketDescription() + "</p>");
            updateDiv("marketDiv", buffer.toString());
            
            MarketPrices marketPrices = betFair.getMarketPrices(marketID);

            if (marketPrices.getMarketStatus() == MarketStatusEnum.CLOSED) {
                throw new Exception("Market is now " + marketPrices.getMarketStatus());
            }
            
            buffer = new StringBuilder();            
            buffer.append("<p>Status: <b>" + marketPrices.getMarketStatus() + "</b></p>");
            
            Runner[] runners = market.getRunners();
            RunnerPrices[] prices = marketPrices.getRunnerPrices();
            
            buffer.append("<table>");
            buffer.append("<tr><th>Name</th><th>Price 1</th><th>Price 2</th><th>Price 3</th></tr>");
            
            // Data for graphing
            StringBuilder headings = new StringBuilder();
            headings.append("Time");
            StringBuilder dataRow = new StringBuilder();
            long time = System.currentTimeMillis();
            dataRow.append(String.valueOf(time));
            
            for (int i=0; i<runners.length; i++) {
                
                buffer.append("<tr><td>" + runners[i].getName() + "</td>");
                
                Price[] bestPrices = prices[i].getBestPricesToBack();
                
                // Data for graphing
                headings.append("," + runners[i].getName());   
                dataRow.append("," + bestPrices[0].getPrice());
                
                for (int j=0; j<bestPrices.length; j++) {
                    buffer.append("<td><b>" + bestPrices[j].getPrice() + "</b><br/>");                        
                    buffer.append("£" + bestPrices[j].getAmountAvailable() + "</td>");
                }
                buffer.append("</tr>"); 
            }  
            buffer.append("</table>");
            

            
            String lgd = (String) getData().get(String.valueOf(marketID));
            if (lgd == null) lgd = headings.toString() + "\n";
            lgd += dataRow.toString() + "\n";
            getData().put(String.valueOf(marketID), lgd);
            

            
            String url = WareHouse.getUrlPathForClass(page.image.LineGraph.class)
                       + "?w=700&f=reciprocal&t=" + time + "&d=" + marketID;
            append(javaScript(updateSrcScript("graph", url)));
            
            updateDiv("dataDiv", buffer.toString());              

        } catch (Exception e) {
            updateDiv("errorDiv", e.getMessage());
            setPushEnabled(false); 
        }                
    }
    
    private void updateDiv(String id, String newContent) throws IOException {
        append(javaScript(updateHTMLScript(id, newContent)));
        flush();
    }
    
    private String inputRow(String label, String field, String detail) {
        return "<div class='inputRow'>" +
        	   "<div class='inputLabel'>" + label + "</div>" +
        	   "<div class='inputField'>" + field + "</div>" +
        	   "<div class='inputDetail'>" + detail + "</div></div>";
        	   
    }
    
    public WikiMap getData() {     
        WikiMap data = WareHouse.getWikiMap("LineGraphData");
        if (data == null) data = WareHouse.initWikiMap("LineGraphData");
        
        return data;
    }  
}
    */

