package unoofc.demo;

import java.io.*;
import unoofc.*;

/* This class demonstrates Draw document programming using unoofc. The program builds a two-page
 * Draw document with shapes. The single input argument is the pathname of the unoofc.demo folder. 
 * This folder contains input files required by this program, and the two-page Draw document, unoofc.odg, 
 * is written to this folder. The program also writes logging information to the console.
 */
public class ODrawDocDemo {
	private ODrawDoc iDoc = null;
	private ODrawArea iBaseArea = null;

	public static void main(String[] args) {
		try {
			File folder = new File(args[0]);
			folder.mkdir();
			ODrawDocDemo demo = new ODrawDocDemo();
			ODrawDoc.Page page = demo.setupFirstPage();
			demo.drawBasic(page);
			demo.drawText(page);
			demo.drawLine(page);
			demo.drawSecondPage(folder);
			demo.end(folder);
		} catch (java.lang.Exception xcp) {
			xcp.printStackTrace();
		}
	}
	
	private ODrawDocDemo() throws Exception {
		// create empty document
		iDoc = new ODrawDoc(null);
	}

	private ODrawDoc.Page setupFirstPage() throws Exception {
		// insert empty first page and invert to make landscape (all following pages too)
		ODrawDoc.Page page = iDoc.pages().get(0);
		page.invert();
		
		// setup origin position for all shapes
		int baseWidth = (Integer) page.prop(OProps.DrawPage.Width) / 5;
		int x = (Integer) page.prop(OProps.DrawPage.BorderLeft);
		int baseHeight = (Integer) page.prop(OProps.DrawPage.Width) / 5;
		int y = baseHeight + (Integer) page.prop(OProps.DrawPage.BorderTop);
		com.sun.star.awt.Point origin = new com.sun.star.awt.Point(x, y);
		iBaseArea = new ODrawArea(origin, new com.sun.star.awt.Size(baseWidth, baseHeight));
		return page;
	}
	
	private void drawBasic(ODrawDoc.Page page) throws Exception {
		// draw rectangle with default fill color
		ODrawDoc.PageLayer layer = page.backgroundLayer();
		OShape rect = new ODrawDoc.RectangleShape(iDoc, iBaseArea);
		layer.add(rect);
		
		// draw red ellipse with shadow
		ODrawArea area = iBaseArea.changeX(iBaseArea.size().Width).changeY(iBaseArea.size().Height);
		area.reshape(0.75f, 0.75f);
		OShape ellipse = new ODrawDoc.EllipseShape(iDoc, area);
		ellipse.setFillColor(OColor.RED);
		ellipse.setShadow( OColor.DARKGRAY, 300, 300);
		layer.add(ellipse);
		
		// draw connector between rectangle and ellipse
		OShape conn = new ODrawDoc.ConnectorShape(iDoc, rect, OProps.Connector.Bottom, ellipse, 
				OProps.Connector.Top, com.sun.star.drawing.ConnectorType.STANDARD);
		layer.add(conn);
	}
	
	private OShape drawText(ODrawDoc.Page page) throws Exception {
		ODrawDoc.PageLayer layer = page.backgroundLayer();
		ODrawArea area = iBaseArea.changeX(2 * iBaseArea.size().Width).changeY(iBaseArea.size().Height);
		area = area.changeY(-1 * iBaseArea.size().Height);
		OShape text = new ODrawDoc.TextShape(iDoc, area);
		layer.add(text);
		text.setText("text");
		text.setProp(OProps.Char.CharHeight, 60);
		text.setProp(OProps.Char.CharFontName, "Times New Roman");
	   text.setProp(OProps.Char.CharColor, OColor.BLACK);
	   text.setProp(OProps.Line.LineStyle, com.sun.star.drawing.LineStyle.SOLID);
	   text.setProp(OProps.Line.LineColor, OColor.GREEN);
	   return text;
	}
	
	private OShape drawLine(ODrawDoc.Page page) throws Exception {
		ODrawDoc.PageLayer layer = page.backgroundLayer();
		// draw green dashed line
		OShape line = new ODrawDoc.LineShape(iDoc, iBaseArea.position(), iBaseArea.endPos());
		line.setProp(OProps.Line.LineColor, OColor.GREEN);
		line.setProp(OProps.Line.LineWidth, 1000);
		ODrawDoc.LineDash dash = new ODrawDoc.LineDash(0, 200, 5, 200, 200);
		line.setLineDash(dash);
		layer.add(line);
		return line;
	}

	private void drawSecondPage(File folder) throws Exception {
		iDoc.insertPage(1);
		ODrawDoc.Page page = iDoc.pages().get(1);
		ODrawDoc.PageLayer layer = page.backgroundLayer();
		String url = OUtil.urlFromPath(folder.getAbsolutePath()) + "/example.jpg";
		OShape shape = new ODrawDoc.BitmapShape(iDoc, page.area(), true, url); 
		layer.add(shape);
	}
	
	private void end(File folder) throws Exception {
		String url = OUtil.urlFromPath(folder.getAbsolutePath()) + "/unoofc";
		iDoc.writeOdg(url);
		iDoc.logSummary();
	}
}