package de.echox.hacklace.pix0lat0r.core;

import java.util.ArrayList;
import java.util.List;

import de.echox.hacklace.pix0lat0r.data.Matrix;
import de.echox.hacklace.pix0lat0r.data.MatrixImpl;

public class App {
	
	List<Matrix> pages = new ArrayList<Matrix>();
	
	private int pageIdx = 0;

	public void quit() {
		System.exit(0);
	}

	App() {
		pages.add(new MatrixImpl(5,7));
	}
	
	public Matrix nextPage() {
		pageIdx++;
		if(pageIdx == pages.size()) {
			pages.add(new MatrixImpl(5,7));
		}
		return pages.get(pageIdx);
	}

	public Matrix prevPage() {
		if (pageIdx > 0) {
			pageIdx--;
		}
		return pages.get(pageIdx);
	}

	public int getCurrentPage() {
		return pageIdx + 1;
	}

	public int getPageSize() {
		return this.pages.size();
	}
	
	public Matrix getCurrentPageData() {
		return this.pages.get(pageIdx);
	}

	public Matrix cloneNewPage() {
		Matrix matrix = pages.get(pageIdx).copy();
		pageIdx++;
		pages.add(pageIdx, matrix);
		return matrix;
	}

	public Matrix insertNewPage() {
		Matrix matrix = new MatrixImpl(5, 7);
		pageIdx++;
		pages.add(pageIdx, matrix);
		return matrix;
	}

	public Matrix delete() {
		if(!(pageIdx == 0 && pages.size() == 1)) {
			
			pages.remove(pageIdx);
			
			if(pageIdx != 0) {
				pageIdx--;
			}
			
		}
		
		return pages.get(pageIdx);
	}
}

@@ -1127,12 +1127,13 @@
 %7B%0A%09%09
+%0A%09%09
 if
-(!
 (pag
@@ -1167,16 +1167,113 @@
 = 1)
-) %7B%0A%09%09%09%0A
+ %7B%0A%09%09%09pages.remove(0);%0A%09%09%09pages.add(0,new MatrixImpl(5, 7));%0A%09%09%7D else %7B%0A%09%09%09if(pages.size() != 1) %7B%0A%09%09%09%09%0A%09
 %09%09%09p
@@ -1297,17 +1297,19 @@
 dx);%0A%09%09%09
-%0A
+%09%0A%09
 %09%09%09if(pa
@@ -1318,24 +1318,25 @@
 Idx != 0) %7B%0A
+%09
 %09%09%09%09pageIdx-
@@ -1345,13 +1345,20 @@
 %0A%09%09%09
+%09
 %7D%0A%09%09%09
+%09%0A%09%09%09%7D
 %0A%09%09%7D

clear the first page if its the only one left and delete is pressed