package com.smartgwt.sample.client;

import java.util.Date;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.rpc.RPCManager;
import com.smartgwt.client.types.Cursor;
import com.smartgwt.client.types.ExportDisplay;
import com.smartgwt.client.types.LineCap;
import com.smartgwt.client.types.LinePattern;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.HTMLPane;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.drawing.DrawLabel;
import com.smartgwt.client.widgets.drawing.DrawLine;
import com.smartgwt.client.widgets.drawing.DrawPane;
import com.smartgwt.client.widgets.drawing.Point;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;

public class PrintDrawIssue implements EntryPoint {
	
	private static final int AXIS_LENGTH = 400;
	private static final int WEIGHT_AXIS_START = 26;
	private static final int ARM_AXIS_START = 85;

	public void onModuleLoad() {

		final VLayout example = new VLayout();
		
		IButton button = new IButton("Print", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				Canvas.showPrintPreview(example);
			}
		});
		example.addMember(button);
		
		IButton pdfButton = new IButton("Print to PDF", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				// Now print it
				DSRequest requestProperties = new DSRequest();
				requestProperties.setExportFilename("chris" + (new Date()).getTime());
				requestProperties.setExportDisplay(ExportDisplay.DOWNLOAD);
				requestProperties.setContentType("application/pdf");
				requestProperties.setDownloadResult(true);
				RPCManager.exportContent(example, requestProperties);
			}
		});
		example.addMember(pdfButton);
		
		HTMLPane htmlPane = new HTMLPane();  
        htmlPane.setWidth(230);  
        htmlPane.setHeight(150);  
        htmlPane.setShowEdges(true);  
        htmlPane.setStyleName("exampleTextBlock");  
        String contents = "<span class='exampleDropTitle'>Ajax  </span> <b>A</b>synchronous " +  
                "<b>J</b>avaScript <b>A</b>nd <b>X</b>ML (AJAX) is a Web development technique for creating interactive " +  
                "<b>web applications</b>. The intent is to make web pages feel more responsive by exchanging small amounts " +  
                "of data with the server behind the scenes, so that the entire Web page does not have to be reloaded each time " +  
                "the user makes a change. This is meant to increase the Web page's <b>interactivity</b>, <b>speed</b>, and " +  
                "<b>usability</b>. (Source: <a href='http://www.wikipedia.org' title='Wikipedia' target='_blank'>Wikipedia</a>)";  
        htmlPane.setContents(contents); 
		example.addMember(htmlPane);
		
		HLayout moreContent = new HLayout();
		moreContent.setBackgroundColor("lightGrey");
		example.addMember(moreContent);
		
		HTMLPane moreHtml = new HTMLPane();  
		moreHtml.setWidth(230);  
		moreHtml.setHeight(150);  
		moreHtml.setShowEdges(true);  
		moreHtml.setStyleName("exampleTextBlock");  
        moreHtml.setContents(contents); 
		moreContent.addMember(moreHtml);

		DrawPane drawPane = new DrawPane();
		drawPane.setHeight(500);
		drawPane.setWidth(500);
		drawPane.setTop(5);
		drawPane.setLeft(25);
		drawPane.setOverflow(Overflow.HIDDEN);
		drawPane.setCursor(Cursor.AUTO);
		moreContent.addMember(drawPane);

		DrawLine weightAxis = new DrawLine();
		weightAxis.setDrawPane(drawPane);
		weightAxis.setStartPoint(new Point(ARM_AXIS_START,
				WEIGHT_AXIS_START));
		weightAxis.setEndPoint(new Point(ARM_AXIS_START,
				WEIGHT_AXIS_START + AXIS_LENGTH ));
		weightAxis.setLineWidth(1);
		weightAxis.setLineCap(LineCap.SQUARE);
		weightAxis.setLinePattern(LinePattern.SOLID);
		weightAxis.draw();

		DrawLine armAxis = new DrawLine();
		armAxis.setDrawPane(drawPane);
		armAxis.setStartPoint(new Point(ARM_AXIS_START,
				WEIGHT_AXIS_START + AXIS_LENGTH ));
		armAxis.setEndPoint(new Point(ARM_AXIS_START + AXIS_LENGTH
				, WEIGHT_AXIS_START + AXIS_LENGTH
				));
		armAxis.setLineWidth(1);
		armAxis.setLineCap(LineCap.SQUARE);
		armAxis.setLinePattern(LinePattern.SOLID);
		armAxis.draw();

		// Draw static labels
		DrawLabel awl = new DrawLabel();
		awl.setContents("Aircraft");
		awl.setDrawPane(drawPane);
		awl.setLeft(ARM_AXIS_START - 56);
		awl.setTop(0);
		awl.setFontSize(12);
		awl.setLineColor("#545454");
		// awl.rotateTo(270.0f);
		awl.draw();

		DrawLabel awl2 = new DrawLabel();
		awl2.setContents("Weight LBS");
		awl2.setDrawPane(drawPane);
		awl2.setLeft(ARM_AXIS_START - 84);
		awl2.setTop(13);
		awl2.setFontSize(12);
		awl2.setLineColor("#545454");
		// awl2.rotateTo(270.0f);
		awl2.draw();

		DrawLabel fsl = new DrawLabel();
		fsl.setContents("Fuselage Station Inches" );
		fsl.setDrawPane(drawPane);
		fsl.setLeft(ARM_AXIS_START + AXIS_LENGTH  / 2 - 70);
		fsl.setTop(WEIGHT_AXIS_START + AXIS_LENGTH  + 25);
		fsl.setFontSize(12);
		fsl.setLineColor("#545454");
		fsl.draw();

		int numAxisLabels = 6;

		Float minGraphArm = 100.0f;
		Float maxGraphArm = 500.0f;
		Float minGraphWeight = 10000.0f;
		Float maxGraphWeight = 80000.0f;

		Float minArmAxis = minGraphArm
				- ((maxGraphArm - minGraphArm) * 0.10f);
		Float maxArmAxis = maxGraphArm
				+ ((maxGraphArm - minGraphArm) * 0.10f);

		Float minWeightAxis = minGraphWeight
				- ((maxGraphWeight - minGraphWeight) * 0.10f);
		Float maxWeightAxis = maxGraphWeight
				+ ((maxGraphWeight - minGraphWeight) * 0.10f);

		// Draw the Arm Axis Labels
		Float armAxisSize = maxArmAxis - minArmAxis;
		Float armPixelsPer = AXIS_LENGTH  / armAxisSize;
		Float armSpacing = armAxisSize / numAxisLabels;
		Float initialArmInterval = armSpacing / 2;

		for (int i = 0; i < numAxisLabels; i++) {
			int value = Math.round(initialArmInterval
					+ (i * armSpacing) + minArmAxis);
			DrawLabel label = new DrawLabel();
			label.setContents(value + "");
			label.setDrawPane(drawPane);
			label.setLeft(ARM_AXIS_START
					- 12
					+ Math.round((initialArmInterval + (i * armSpacing))
							* armPixelsPer));
			label.setTop(WEIGHT_AXIS_START + AXIS_LENGTH 
					+ 5);
			label.setFontSize(12);
			label.setLineColor("#545454");
			label.draw();
		}

		// Draw the Weight Axis Labels
		Float weightAxisSize = maxWeightAxis - minWeightAxis;
		// System.out.println("minWeightAxis = " + minWeightAxis
		// + " maxWeightAxis = " + maxWeightAxis
		// + " maxWeightAxis = " + maxWeightAxis);

		Float weightPixelsPer = AXIS_LENGTH  / weightAxisSize;
		// System.out.println("weightPixelsPer = " + weightPixelsPer);
		Float weightSpacing = weightAxisSize / numAxisLabels;
		Float initialWeightInterval = weightSpacing / 2;
		// System.out.println("weightSpacing = " + weightSpacing
		// + " initialWeightInterval = " + initialWeightInterval);

		for (int i = 0; i < numAxisLabels; i++) {
			int value = Math.round(maxWeightAxis
					- initialWeightInterval - (i * weightSpacing));
			DrawLabel label = new DrawLabel();
			label.setContents(value + "");
			label.setDrawPane(drawPane);
			label.setLeft(ARM_AXIS_START - 48);
			label.setTop(WEIGHT_AXIS_START
					- 6
					+ Math.round((initialWeightInterval + (i * weightSpacing))
							* weightPixelsPer));
			label.setFontSize(12);
			label.setLineColor("#545454");
			label.draw();
		}

		example.draw();
	}
}
