package storagesim;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

import javax.imageio.ImageIO;

import lib.SQLConnection;

public class StorageMap {
	public static BufferedImage STORE_TILE = null;
	public static BufferedImage PALETTE_TILE = null;
	public static HashMap<Integer, BufferedImage> PRODUCT_TILES = null;

	int tileSize = 15;
	int tileColumns, tileRows;
	
	int selectedTile;

	private ArrayList<Tile> tiles;

	/**
	 * Stores the drawn map as a single image for caching purposes
	 */
	BufferedImage img;

	public StorageMap(int tileColumns, int tileRows) {
		this.tileColumns = tileColumns;
		this.tileRows = tileRows;

		this.selectedTile = -1;
		
		this.tiles = new ArrayList<>(Arrays.asList(new Tile[this.tileColumns * this.tileRows]));

		// fill the map with tile objects
		for (int x = 0; x < this.tileColumns; x++) {
			for (int y = 0; y < this.tileRows; y++) {
				this.setTile(x, y, new Tile());
			}
		}

		this.img = new BufferedImage(this.tileColumns * this.tileSize, this.tileRows * this.tileSize,
				BufferedImage.TYPE_INT_RGB);

		// preload the tile images
		try {
			if (STORE_TILE == null) {
				STORE_TILE = ImageIO.read(StorageMap.class.getResource("/res/store.png"));
			}
			if (PALETTE_TILE == null) {
				PALETTE_TILE = ImageIO.read(StorageMap.class.getResource("/res/palette.png"));
			}
			if (PRODUCT_TILES == null) {
				PRODUCT_TILES = new HashMap<>();
				File[] ls = new File(StorageMap.class.getResource("/res/pixelart/").getFile()).listFiles();
		        for (File f : ls) {
		            if (f.isFile()) {
		                if (f.getName().endsWith(".png")) {
		                    try {
		                        BufferedImage img = ImageIO.read(f);
		                        PRODUCT_TILES.put(Integer.parseInt(f.getName().replace(".png", "")), img);
		                    } catch (IOException e) {
		                        e.printStackTrace();
		                    }
		                }
		            }
		        }
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Draws the tiles onto the image
	 */
	public void drawMap() {
		this.img = new BufferedImage(this.tileColumns * this.tileSize, this.tileRows * this.tileSize,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g2 = this.img.createGraphics();
		for (int x = 0; x < this.tileColumns; x++) {
			for (int y = 0; y < this.tileRows; y++) {
				BufferedImage[] textures = this.getTile(x, y).getTextures(x, y, this);
				for (BufferedImage texture : textures) {
					g2.drawImage(texture, x * this.tileSize, y * this.tileSize, this.tileSize, this.tileSize, null);
				}
			}
		}
		g2.dispose();
	}

	/**
	 * Synchronises the tile content with the database
	 */
	public void updateMap(SQLConnection db) {
		for (int i = 0; i < this.tiles.size(); i++) {
			ArrayList<HashMap<String, String>> product = db.queryToMap("SELECT f_Artikel_ID FROM t_lager WHERE Lagerplatz = " + (i + 1));
			if (product.size() == 0) {
				this.tiles.get(i).filled = false;
				this.tiles.get(i).product_id = 0; // standartwert
			} else {
				this.tiles.get(i).filled = true;
				this.tiles.get(i).product_id = Integer.parseInt(product.get(0).get("f_Artikel_ID"));
			}
		}
		
		this.drawMap();
	}

	private void setTile(int x, int y, Tile t) {
		this.tiles.set(y * this.tileColumns + x, t);
	}

	public Tile getTile(int x, int y) {
		return this.tiles.get(y * this.tileColumns + x);
	}

	/**
	 * Stores the relevant information of all tiles in objects
	 * 
	 * @author Manuel
	 */
	private class Tile {

		public boolean filled;
		public int product_id;

		private Tile() {
			this.filled = false;
			this.product_id = 0;
		}

		public BufferedImage[] getTextures(int x, int y, StorageMap map) {
			if (this.filled) {
				if (this.product_id > 0) {
					return new BufferedImage[] { STORE_TILE, PRODUCT_TILES.get(this.product_id) };
				} else {
					return new BufferedImage[] { STORE_TILE, PALETTE_TILE };
				}
			} else {
				return new BufferedImage[] { STORE_TILE };
			}
		}
	}
}
