package unoofc.demo;

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

/* This class demonstrates Impress document programming using unoofc. The program builds an
 * Impress document consisting of three slides and displays it as a slide show. The single input 
 * argument is the pathname of the unoofc.demo folder. This folder contains input files required by 
 * this program, and the three-slide Impress document, unoofc.odp, is written to this folder. 
 * The program also writes logging information to the console.
 */
public class OImpressDocDemo {
	private CustomImpressDoc iDoc = null;
	private int iSlideNum = 0;
	
	public static void main(String[] args) {
		try {
			File folder = new File(args[0]);
			OImpressDocDemo demo = new OImpressDocDemo();
			demo.insertTitleSlide();
			demo.insertBulletSlide();
			demo.insertBitmapSlide(folder);
			demo.end(folder);
			demo.runPres();
		} catch (java.lang.Exception xcp) {
			xcp.printStackTrace();
		}
	}
	
	private OImpressDocDemo() throws Exception {
		// create empty document
		iDoc = new CustomImpressDoc(null);
	}
	
	private void insertTitleSlide() throws Exception {
		iDoc.insertTitleSlide(iSlideNum++, "This is a title", "This is a subtitle");	
	}
	
	private void insertBulletSlide() throws Exception {
		int bulletNum = 0;
		OStyle bulletStyle = iDoc.style(OStyle.Family.Default, "outline1");
		LinkedList<OImpressDoc.Bullet> bullets = new LinkedList<OImpressDoc.Bullet>();
		bullets.add(new OImpressDoc.Bullet(bulletNum++, "Bullet One"));
		bullets.add(new OImpressDoc.Bullet(bulletNum++, "Bullet Two"));
		bullets.add(new OImpressDoc.Bullet(bulletNum++, "Bullet Three"));
		iDoc.insertBulletSlide(iSlideNum++, "This is a bullet slide title", bullets, bulletStyle.name());
	}
	
	private void insertBitmapSlide(File folder) throws Exception {
		String url = OUtil.urlFromPath(folder.getAbsolutePath()) + "/example.jpg";
		OImpressDoc.Slide slide = iDoc.insertBitmapSlide(iSlideNum++, "This is a bitmap slide title", url);
		// set bitmap properties
		Iterator<OShape> iter = slide.shapesByType(ODrawDoc.ShapeType.GRAPHIC).iterator();
		OShape bitmap = iter.next();
		bitmap.setSolidLine(OColor.BLACK, 100);
	}
	
	private void end(File folder) throws Exception {
		String url = OUtil.urlFromPath(folder.getAbsolutePath()) + "/unoofc";
		iDoc.writeOdp(url);
		iDoc.logSummary();
	}
	
	private void runPres() throws java.lang.Exception {
		OPresentation pres = new OPresentation(iDoc);
		pres.setProp(OProps.Presentation.IsFullScreen, Boolean.TRUE);
		// show each slide for 3 seconds
		pres.setPropAllSlides(OProps.PresPage.Change, OPresentation.SlideChange.AUTOMATIC.value());
		pres.setPropAllSlides(OProps.PresPage.Duration, 3);
		pres.start();
	}
	
	/* Class that sets up custom styles and master.
	 */
	private class CustomImpressDoc extends OImpressDoc {
		
		CustomImpressDoc(String url) throws java.lang.Exception {
			super(url);
			setupStyles();
			setupMaster();
		}

		public void setupStyles() throws com.sun.star.uno.Exception {
			// setup title style
			OStyle style = style(OStyle.Family.Default, "title");
			style.setFillColor(OColor.DARKGRAY);
			style.setProp(OProps.Char.CharFontName, "Arial");
			style.setProp(OProps.Char.CharColor, OColor.WHITE);
			
			// setup subtitle style
			style = style(OStyle.Family.Default, "subtitle");
			style.setProp(OProps.Char.CharFontName, "Arial");
			float charHeight = (Float) style.prop(OProps.Char.CharHeight);
			
			// setup outline styles
			style = style(OStyle.Family.Default, "outline1");
			style.setProp(OProps.Char.CharHeight, charHeight);	// same as subtitle
			ONumbering numbering = new ONumbering(style);
			numbering.setProp(OProps.Numbering.BulletColor, 0, OColor.RED);
			style = style(OStyle.Family.Default, "outline2");
			style.setProp(OProps.Char.CharHeight, charHeight);
			style = style(OStyle.Family.Default, "outline3");
			style.setProp(OProps.Char.CharHeight, charHeight);
		}
		
		public void setupMaster() throws com.sun.star.uno.Exception {
			// fill master background with color LIGHTGRAY
			OStyle style = style(OStyle.Family.Default, "background");
			style.setProp(OProps.Fill.FillColor, OColor.LIGHTGRAY);
			style.setProp(OProps.Fill.FillStyle, com.sun.star.drawing.FillStyle.SOLID);
			
			// fill an area of the master with a pale yellow Rectangle shape
			ArrayList<OImpressDoc.Slide> masters = masterSlides();
			OImpressDoc.Slide master = masters.get(0);
			ODrawArea area = master.area().reshape(0.90f, 0.90f);
			OShape shape = new ODrawDoc.RectangleShape(this, area);
			OColor paleYellow = new OColor("paleyellow", 0xFFFFE0);
			shape.setFillColor(paleYellow);
			shape.setProp(OProps.DrawShape.ZOrder, 0);
			master.backgroundLayer().add(shape);
			
			// set up footer
			Iterator<OShape> iter = master.shapesByType(OImpressDoc.ShapeType.FOOTER).iterator();
			OShape footer = iter.next();
			footer.setText("This is the master footer");
			footer.setProp(OProps.Char.CharColor, OColor.BLUE);
			iter = master.shapesByType(OImpressDoc.ShapeType.DATETIME).iterator();
			OShape datetime = iter.next();
			Calendar cal = new GregorianCalendar();
			datetime.setText(utilt.Utility.DMY.format(cal.getTime()));
			datetime.setProp(OProps.Char.CharColor, OColor.BLACK);
		}
	}
}