package edu.wisc.cs.games10.chrononaut;

import processing.core.PApplet;
import processing.core.PImage;

public class SquareLevel extends Level {
	public SquareLevel(String ln, PApplet canvas) {
		levelname = ln;
		
		pasttiles = Level.loadEraFromImage(levelname, "a", canvas);
		presenttiles = Level.loadEraFromImage(levelname, "b", canvas);
		futuretiles = Level.loadEraFromImage(levelname, "c", canvas);
		
		pastSquares = Level.loadEraFromImage2(levelname, "a", canvas);
		presentSquares = Level.loadEraFromImage2(levelname, "b", canvas);
		futureSquares = Level.loadEraFromImage2(levelname, "c", canvas);
		
		
		width = pasttiles.length;
		height = pasttiles[0].length;
		
		// get images
		pastbackground = canvas.loadImage("levels/" + levelname + "/a_back.png");
		presentbackground = canvas.loadImage("levels/" + levelname + "/b_back.png");
		futurebackground = canvas.loadImage("levels/" + levelname + "/c_back.png");
		
		squareImages = new PImage[34];
		squareImages[0] = canvas.loadImage("levels/" + levelname + "/tiles/solid_ground.png");
		squareImages[1] = canvas.loadImage("levels/" + levelname + "/tiles/cliff_left.png");
		squareImages[2] = canvas.loadImage("levels/" + levelname + "/tiles/cliff_right.png");
		squareImages[3] = canvas.loadImage("levels/" + levelname + "/tiles/cliff_upper_left.png");
		squareImages[4] = canvas.loadImage("levels/" + levelname + "/tiles/cliff_upper_right.png");
		squareImages[5] = canvas.loadImage("levels/" + levelname + "/tiles/solid_upper_left.png");
		squareImages[6] = canvas.loadImage("levels/" + levelname + "/tiles/solid_upper_middle.png");
		squareImages[7] = canvas.loadImage("levels/" + levelname + "/tiles/solid_upper_right.png");
		squareImages[8] = canvas.loadImage("levels/" + levelname + "/tiles/tree_upper_left.png");
		squareImages[9] = canvas.loadImage("levels/" + levelname + "/tiles/tree_upper_middle.png");
		squareImages[10] = canvas.loadImage("levels/" + levelname + "/tiles/tree_upper_right.png");
		squareImages[11] = canvas.loadImage("levels/" + levelname + "/tiles/tree_lower_left.png");
		squareImages[12] = canvas.loadImage("levels/" + levelname + "/tiles/tree_lower_middle.png");
		squareImages[13] = canvas.loadImage("levels/" + levelname + "/tiles/tree_lower_right.png");
		squareImages[14] = canvas.loadImage("levels/" + levelname + "/tiles/tree_lower_trunk.png");
		squareImages[15] = canvas.loadImage("levels/" + levelname + "/tiles/tree_trunk.png");
		squareImages[16] = canvas.loadImage("levels/" + levelname + "/tiles/tree_middle.png");
		squareImages[17] = canvas.loadImage("levels/" + levelname + "/tiles/tree_middle_left.png");
		squareImages[18] = canvas.loadImage("levels/" + levelname + "/tiles/tree_middle_right.png");
		squareImages[19] = canvas.loadImage("levels/" + levelname + "/tiles/grass_left.png");
		squareImages[20] = canvas.loadImage("levels/" + levelname + "/tiles/grass_right.png");
		squareImages[21] = canvas.loadImage("levels/" + levelname + "/tiles/grass_cliff_foot_left.png");
		squareImages[22] = canvas.loadImage("levels/" + levelname + "/tiles/grass_cliff_foot_right.png");
		squareImages[23] = canvas.loadImage("levels/endflag.png");
		squareImages[24] = canvas.loadImage("levels/endflagTop.png");
		squareImages[25] = canvas.loadImage("levels/" + levelname + "/tiles/grass_tree_trunk.png");
		squareImages[26] = canvas.loadImage("levels/" + levelname + "/tiles/tree_trunk_lpiece_left.png");
		squareImages[27] = canvas.loadImage("levels/" + levelname + "/tiles/tree_trunk_lpiece_right.png");
		squareImages[28] = canvas.loadImage("levels/" + levelname + "/tiles/tree_trunk_sideways.png");
		squareImages[29] = canvas.loadImage("levels/" + levelname + "/tiles/tree_trunk_intersect_left.png");
		squareImages[30] = canvas.loadImage("levels/" + levelname + "/tiles/tree_trunk_intersect_right.png");
		squareImages[31] = canvas.loadImage("levels/" + levelname + "/tiles/grass.png");
		squareImages[32] = canvas.loadImage("levels/" + levelname + "/tiles/cliff_foot_left.png");
		squareImages[33] = canvas.loadImage("levels/" + levelname + "/tiles/cliff_foot_right.png");

		reloadLevel(canvas);
	}
	
	@Override
	public Vector2D getStartLocation() {
		// TODO: load this location from file
		return new Vector2D(1, 2);
	}
	
	@Override
	public PImage getBackground(Era era) {
		if (era == Era.PAST) {
			return pastbackground;
		} else if (era == Era.PRESENT) {
			return presentbackground;
		} else {
			return futurebackground;
		}
	}
	
	@Override
	public PImage getForeground(Era era) {
		// TODO Auto-generated method stub
		return null;
	}

	
	@Override
	public int isLevelFinished(Player p) {
		int x = (int) Math.floor(p.loc.x);
		int y = (int) Math.floor(p.loc.y);
		
		// the level is done when we have collected all
		// creatures and reached the flag
		if (creatures.size() == 0 && (
				getSquare(p.currentEra, x, y) == Square.FLAG_TOP ||
				getSquare(p.currentEra, x + 1, y) == Square.FLAG_TOP ||
				getSquare(p.currentEra, x, y + 1) == Square.FLAG_TOP ||
				getSquare(p.currentEra, x + 1, y + 1) == Square.FLAG_TOP)) {
			return 0;
		}
		return -1;
	}
	
	public Square getSquare(Era era, int x, int y) {
		// don't crash on invalid tiles
		if (x < 0 || x >= width || y < 0 || y >= height) {
			return Square.BLANK_TILE;
		}
		
		if (era == Era.PAST) {
			return pastSquares[x][y];
		} else if (era == Era.PRESENT) {
			return presentSquares[x][y];
		} else {
			return futureSquares[x][y];
		}
	}
	
	public PImage[] getImages() {
		return squareImages;
	}
	
	PImage[] squareImages;
	
	PImage pastbackground;
	PImage presentbackground;
	PImage futurebackground;
}
