package hud;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import engine.Game;
import engine.Game.STATE;
import engine.MouseInput;
import player.Player;

/*  ------------- Nihilne ------------
    * Created by Mitch Weaver - 2016 *
      ~~~~ All Rights Reserved ~~~~ */

public final class Map {
	
	private static final int frameWidth = 10, frameHeight = 10;
	private static int width, height, x, y;
	public static BufferedImage map, mapCopy;
	public static Rectangle mapBounds = new Rectangle(0,0,0,0);
	public static double scaleFactor = 1;
	public static boolean clicked, hasBeenClicked;
	
	private static int tempX, tempY, latestX, latestY;
	public final static void tick(){
		if(Game.state != STATE.MAP) return;
		
		if(!hasBeenClicked){
			width = (int)(Game.game.getWidth()/1.5);
			height = (int)(Game.game.getHeight()/1.5);
			x = (int)(Game.game.getWidth()/2 - width/2);
			y = (int)(Game.game.getHeight()/2 - height/2);
			tempX = latestX = x; tempY = latestY = y;
		}
		
		if(clicked){
			latestX = tempX + MouseInput.mouseXY.x - MouseInput.clickPos.x;
			latestY = tempY + MouseInput.mouseXY.y - MouseInput.clickPos.y;
			hasBeenClicked = true;
		} 
		
		if(hasBeenClicked && !clicked){
			tempX = latestX;
			tempY = latestY;
		}
		
		mapBounds.x = x+frameWidth;
		mapBounds.y = y+frameWidth;
		mapBounds.width = width - frameWidth*2;
		mapBounds.height = height - frameHeight*2;
	}
	
	public static BufferedImage overlay;
	public final static void render(Graphics2D g2d) {
		if(Game.state != STATE.MAP) return;
		
		g2d.setColor(Color.black);
		g2d.fillRect(x-frameWidth, y-frameHeight, width+frameWidth*2, height+frameHeight*2);
		g2d.setColor(Color.gray);
		g2d.fillRect(x, y, width, height);
		g2d.setColor(Color.black);
		g2d.fillRect(x+frameWidth, y+frameWidth, width-frameWidth*2, height-frameWidth*2);
		
		g2d.setClip(x+frameWidth, y+frameWidth, width-frameWidth*2, height-frameWidth*2);
		g2d.scale(scaleFactor, scaleFactor);
		
		g2d.translate(-Player.center.x/96d + width/2d / scaleFactor, -Player.center.y/96d + height/2d / scaleFactor);

		g2d.drawImage(map, (int)(latestX / scaleFactor), (int)(latestY / scaleFactor), null);
		g2d.drawImage(overlay, (int)(latestX / scaleFactor), (int)(latestY / scaleFactor), null);
		
		BufferedImage playerPosOverlay = new BufferedImage(map.getWidth(), map.getHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics2D playerPos = (Graphics2D) playerPosOverlay.getGraphics();
		playerPos.setColor(Color.orange); int WH = 96; /* 1/96 scale = 1 pixel */
		playerPos.scale(1/96d, 1/96d);
		playerPos.fillRect((int)Player.center.x-WH/2, (int)Player.center.y-WH/2, WH, WH);
		playerPos.setColor(Color.red);
		WH = 960;
		playerPos.drawOval((int)Player.center.x-WH/2-96, (int)Player.center.y-WH/2-96, WH, WH);
		g2d.drawImage(playerPosOverlay, (int)(latestX / scaleFactor), (int)(latestY / scaleFactor), null); 
		playerPos.dispose(); playerPos = null; playerPosOverlay = null;
	}
	
	public static synchronized final void cleanUp() {
		map = mapCopy = null; mapBounds = null;
	}
	
}
