package rapport;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;


public class RapportCheckout extends Rapport {
	private static int maxrow = 26;
	
	private static class Row{
		public int numeroChambre;
		public Date depart;
		public String reste;
		public String supplement;
		public Row(int numeroChambre, Date depart, String reste,
				String supplement) {
			super();
			this.numeroChambre = numeroChambre;
			this.depart = depart;
			this.reste = reste;
			this.supplement = supplement;
		}
	};

	private Date dateCheckout;
	private List<Row> checkoutLst;
	
	private int cursor;
	private PDFont bold;
	private PDFont font;
	
	/**
	 * constructor
	 * @throws IOException
	 */
	public RapportCheckout() throws IOException {
		super();
		bold = PDType1Font.HELVETICA_BOLD;
		font = PDType1Font.HELVETICA;
		checkoutLst = new ArrayList<>();
		cursor = 0;
	}

	@Override
	public int countPage() {
		return checkoutLst.size()/maxrow+(checkoutLst.size()%maxrow==0?0:1);
	}

	@Override
	public void firstPageHeader() throws IOException {
		// TODO Auto-generated method stub
	}

	@Override
	public void header() throws IOException {
		addText("Checkout", bold, 24, getW()/2, 24);
		addTextLeftAlign(new Date(System.currentTimeMillis()).toString(), font, 14, getW()-40, 20 , 10);

		addVerticalText("Épousettage",bold,12,25,120);
		addVerticalText("Salle de bain",bold,12,75,120);
		addVerticalText("Aspirateur",bold,12,125,120);
		addVerticalText("Planché",bold,12,175,120);

		addText("Numero de chambre",bold,12,getW()-350,120);
		addText("Reste",bold,12,getW()-250,120);
		addText("Départ",bold,12,getW()-190,120);
		addText("Supplément",bold,12,getW()-110,120);
		addText("Initiale",bold,12,getW()-40,120);
		addSeparator(130);
	}

	@Override
	public void footer() throws IOException {
		addSeparator(getH()-20);
		addTextRightAlign("Page "+currentPage()+" de "+countPage(), font, 12, getW()-120, getH()-12, 15);
	}

	@Override
	public void lastPageFooter() throws IOException {
		// TODO Auto-generated method stub

	}

	@Override
	public void body() throws IOException {
		int n = checkoutLst.size()-cursor>maxrow?maxrow:checkoutLst.size()-cursor;
		for (int y=0;y<n;y++){
			addBox(25, 150+y*24, 20, 20);
			addBox(75, 150+y*24, 20, 20);
			addBox(125, 150+y*24, 20, 20);
			addBox(175, 150+y*24, 20, 20);
			
			addText(""+checkoutLst.get(cursor+y).numeroChambre, font, 12, getW()-350, 150+y*24);
			addText(checkoutLst.get(cursor+y).reste,bold,10,getW()-250,150+y*24);
			addText(checkoutLst.get(cursor+y).depart.toString(),bold,10,getW()-190,150+y*24);
			addTextLeftAlign(checkoutLst.get(cursor+y).supplement,bold,8,getW()-90,150+y*24,20);
			
			addLine(getW()-72, 64, 160+y*24);
		}
		cursor+=maxrow;
	}

	@Override
	public void generate() throws IOException {
		boolean notlastpage=true;
		while(cursor < checkoutLst.size()){
			header();
			body();
			footer();
			notlastpage = cursor<checkoutLst.size();
			if (notlastpage)
				nextPage();
		}
	}

	/**
	 * set the date the report is covering
	 * @param dateCheckout
	 */
	public void setDateCheckout(Date dateCheckout) {
		this.dateCheckout = dateCheckout;
	}
	
	/**
	 * add chambre to the report
	 * @param numeroChambre as int
	 * @param depart as Date
	 * @param reste as String
	 * @param supplement as String
	 * @return boolean on success
	 */
	public boolean add(int numeroChambre, 
			           Date depart, 
			           String reste,
			           String supplement) {
		return checkoutLst.add(new Row(numeroChambre, depart, reste, supplement));
	}

	public static void main(String...arg) throws IOException, COSVisitorException{
		RapportCheckout rapport = new RapportCheckout();
		
		rapport.setDateCheckout(new Date(System.currentTimeMillis()));
		for (int i=0;i<27;i++)
			rapport.add(200, new Date(0), "...", "oui");
		
		rapport.generate();
		
		rapport.getDocument().save("test.pdf");
		rapport.getDocument().close();
	    File myFile = new File("test.pdf");
	    Desktop.getDesktop().open(myFile);
	}
}
