package rpgEngine.Logic.Camera;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.List;

import rpgEngine.Data.Abstract.Game;
import rpgEngine.Data.Abstract.Item;
import rpgEngine.Data.Abstract.Enemy.Enemy;
import rpgEngine.Data.Abstract.Enemy.EnemyParty;
import rpgEngine.Data.Abstract.Party.HumanPlayer;
import rpgEngine.Data.Abstract.Party.Inventory;
import rpgEngine.Data.Abstract.Party.InventoryItem;
import rpgEngine.Data.Abstract.Party.Skill;
import rpgEngine.Resources.FontPool;
import rpgEngine.Resources.ImagePool;
import rpgEngine.UI.BattlePanel;


public class BattleCamera extends GenericCamera {
	private BattlePanel panel;
	private Game game;
	private Integer cursorPos;
	private Integer enemyChoiceCursorPos;
	private Integer sourceChoiceCursorPos;
	private Integer playerChoiceCursorPos;
	private Integer currentPlayer;
	private Integer battleStep;
	private Integer screenMode;
	private String dialogText;
	
	public BattleCamera(BattlePanel p) {
		super();
		
		panel = p;
		
		game = null;
	}

	@Override
	public BufferedImage getImage() {
		BufferedImage img = null;
		Graphics2D g = null;
		Integer width = 800;
		Integer height = 600;
		
		//Set Parameters from Panel
		game = panel.getParentMainFrame().getGame();		
		cursorPos = panel.getCursorPosition();
		enemyChoiceCursorPos = panel.getEnemyChoiceCursorPosition();
		sourceChoiceCursorPos = panel.getSourceChoiceCursorPosition();
		playerChoiceCursorPos = panel.getPlayerChoiceCursorPosition();
		currentPlayer = panel.getCurrentPlayer();
		battleStep = panel.getBattleStep();
		screenMode = panel.getScreenMode();
		dialogText = panel.getDialogText();
		
		//Start Camera
		img = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
		g = img.createGraphics();
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, width, height);	

		//Draw Background
		if(screenMode == BattlePanel.SCREEN_NORMAL) 
		{
			g.drawImage(ImagePool.getInstance().getPoolItem(ImagePool.ID_BATTLE_BACKGROUND),0,0,width,height,null);
		}
		else if(screenMode == BattlePanel.SCREEN_CHOOSE_ENEMY_FOR_FIGHT) {
			g.drawImage(ImagePool.getInstance().getPoolItem(ImagePool.ID_BATTLE_BACKGROUND),0,0,width,height,null);
		}
		else if(screenMode == BattlePanel.SCREEN_CHOOSE_ENEMY_FOR_SKILL) {
			g.drawImage(ImagePool.getInstance().getPoolItem(ImagePool.ID_BATTLE_BACKGROUND),0,0,width,height,null);
		}
		else if(screenMode == BattlePanel.SCREEN_DIALOG) {
			g.drawImage(ImagePool.getInstance().getPoolItem(ImagePool.ID_BATTLE_BACKGROUND),0,0,width,height,null);
		}
		else if(screenMode == BattlePanel.SCREEN_CHOOSE_PLAYER_FOR_ITEM) {
			g.drawImage(ImagePool.getInstance().getPoolItem(ImagePool.ID_BATTLE_BACKGROUND),0,0,width,height,null);
		}
		else if(screenMode == BattlePanel.SCREEN_CHOOSE_PLAYER_FOR_SKILL) {
			g.drawImage(ImagePool.getInstance().getPoolItem(ImagePool.ID_BATTLE_BACKGROUND),0,0,width,height,null);
		}
		else if(screenMode == BattlePanel.SCREEN_CHOOSE_ITEM) {
			g.drawImage(ImagePool.getInstance().getPoolItem(ImagePool.ID_BATTLE_BACKGROUND_WITH_CHOICE),0,0,width,height,null);
		}
		else if(screenMode == BattlePanel.SCREEN_CHOOSE_SKILL) {
			g.drawImage(ImagePool.getInstance().getPoolItem(ImagePool.ID_BATTLE_BACKGROUND_WITH_CHOICE),0,0,width,height,null);
		}
		
		
		//Draw Screen
		if((screenMode == BattlePanel.SCREEN_NORMAL)|| (screenMode == BattlePanel.SCREEN_CHOOSE_ENEMY_FOR_SKILL) || (screenMode == BattlePanel.SCREEN_CHOOSE_ENEMY_FOR_FIGHT) || (screenMode == BattlePanel.SCREEN_DIALOG)) 
		{
			if(battleStep == BattlePanel.BATTLE_PLAYER) {
				if(screenMode != BattlePanel.SCREEN_DIALOG) {
					drawCommandSelection(g);
				}
			}
			
			drawPlayerStatsAt(g, 0, 0, 384);
			drawPlayerStatsAt(g, 1, 200, 384);
			drawPlayerStatsAt(g, 2, 400, 384);
			drawPlayerStatsAt(g, 3, 600, 384);
			
			drawEnemies(g);
		}
		else if(screenMode == BattlePanel.SCREEN_CHOOSE_ITEM)  {
			drawInventory(g);
		}
		else if(screenMode == BattlePanel.SCREEN_CHOOSE_SKILL)  {
			drawSkillList(g);
		}
		else if((screenMode == BattlePanel.SCREEN_CHOOSE_PLAYER_FOR_ITEM) || (screenMode == BattlePanel.SCREEN_CHOOSE_PLAYER_FOR_SKILL)) {
			drawPlayerStatsAt(g, 0, 0, 384);
			drawPlayerStatsAt(g, 1, 200, 384);
			drawPlayerStatsAt(g, 2, 400, 384);
			drawPlayerStatsAt(g, 3, 600, 384);
			
			drawEnemies(g);
			
			g.setColor(Color.WHITE);
			
			if(playerChoiceCursorPos == 0) {
				g.fillRect(0, 352, 200, 32);
			}
			else if(playerChoiceCursorPos == 1) {
				g.fillRect(200, 352, 200, 32);
			}
			else if(playerChoiceCursorPos == 2) {
				g.fillRect(400, 352, 200, 32);
			}
			else if(playerChoiceCursorPos == 3) {
				g.fillRect(600, 352, 200, 32);
			}			
		}
			
		
		//Draw Dialog if needed
		if(screenMode == BattlePanel.SCREEN_DIALOG) {
			g.drawImage(ImagePool.getInstance().getPoolItem(ImagePool.ID_BATTLE_DIALOG), 50, 268, null);
			
			g.setColor(Color.WHITE);
			g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,20));
			g.drawString(dialogText, 65, 310);
		}
		
		return img;
	}
	
	
	private void drawSkillList(Graphics2D g) {
		HumanPlayer player = game.getParty().get(currentPlayer);
		Skill skill = null;
		List<Skill> skills = null;
		Double valD = 0.0;
		Integer valI = 0;
		Integer x = 0;
		Integer y = 0;
		
		skills = player.getSkills();
		
		g.setColor(Color.WHITE);
		g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,16));
		
		for(Integer i = 0; i < skills.size(); i++) {
			skill = skills.get(i);
			valD = Double.valueOf(i);
			
			if(i % 2 == 0) {
				x = 170;
			}
			else {
				x = 410;
			}
			
			valI = (int) Math.floor(valD / 2.0);
			y = (valI * 24) + 70;
			
			if(player.getMP() >= skill.getMpCost()) {
				g.setColor(Color.WHITE);
			}
			else {
				g.setColor(Color.GRAY);
			}
			
			g.drawString(skill.getName(), x, y);
			
			if(i == sourceChoiceCursorPos) {
				g.fillRect(x-18, y-12, 12, 12);
			}
		}
	}
	
	
	private void drawCommandSelection(Graphics2D g) {
		//Draw Selections
		g.setColor(Color.WHITE);
		g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,32));
		g.drawString("Fight", 40, 64);
		
		if(game.getParty().get(currentPlayer).getSkills().size() != 0) {
			g.setColor(Color.WHITE);
		}
		else {
			g.setColor(Color.GRAY);
		}
		g.drawString("Skill", 190, 64);
		
		if(game.getInventory().getItems().size() != 0) {
			g.setColor(Color.WHITE);
		}
		else {
			g.setColor(Color.GRAY);
		}
		g.drawString("Item", 330, 64);
		
		g.setColor(Color.WHITE);
		g.drawString("Defend", 470, 64);
		
		if(game.getActiveEnemyParty().isBossBattle() == false) {
			g.setColor(Color.WHITE);
			g.drawString("Run", 660, 64);
		}
		
		//Draw Cursor
		if(cursorPos == BattlePanel.CURSOR_FIGHT) {
			g.fillRect(16, 44, 16, 16);
		}
		else if(cursorPos == BattlePanel.CURSOR_SKILL) {
			g.fillRect(166, 44, 16, 16);
		}
		else if(cursorPos == BattlePanel.CURSOR_ITEM) {
			g.fillRect(306, 44, 16, 16);
		}
		else if(cursorPos == BattlePanel.CURSOR_DEFEND) {
			g.fillRect(446, 44, 16, 16);
		}
		else if(cursorPos == BattlePanel.CURSOR_RUN) {
			g.fillRect(636, 44, 16, 16);
		}
	}
	
	
	private void drawInventory(Graphics2D g) {
		Inventory inv = null;
		InventoryItem invItem = null;
		Double valD = 0.0;
		Integer valI = 0;
		Integer x = 0;
		Integer y = 0;
		
		inv = game.getInventory();
		
		g.setColor(Color.WHITE);
		g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,16));
		
		for(Integer i = 0; i < inv.getItems().size(); i++) {
			invItem = inv.getItems().get(i);
			valD = Double.valueOf(i);
			
			if(i % 2 == 0) {
				x = 170;
			}
			else {
				x = 410;
			}
			
			valI = (int) Math.floor(valD / 2.0);
			y = (valI * 24) + 70;
			
			if ((invItem.getItem().getType() == Item.TYPE_HEALING) || (invItem.getItem().getType() == Item.TYPE_MP_HEALING) || (invItem.getItem().getType() == Item.TYPE_REVIVE)) {
				g.setColor(Color.WHITE);
			}
			else {
				g.setColor(Color.GRAY);
			}
			
			g.drawString(invItem.getItem().getName() + " (" + invItem.getQuantity() +  ")", x, y);
			
			if(i == sourceChoiceCursorPos) {
				g.setColor(Color.WHITE);
				g.fillRect(x-18, y-12, 12, 12);
			}
		}
			
	}
	
	
	private void drawEnemies(Graphics2D g) {
		EnemyParty party = game.getActiveEnemyParty();
		Enemy enemy = null;
		Integer numEnemies = party.getEnemies().size();
		Integer x = 0;
		Integer y = 150;
		Integer xStep = 200;
		
		x = (800 - (xStep * numEnemies)) / 2;

		for(Integer i = 0; i < numEnemies; i++) {
			enemy = party.getEnemies().get(i);
			
			if(enemy.getHP() > 0) {
				if((screenMode == BattlePanel.SCREEN_CHOOSE_ENEMY_FOR_FIGHT) || (screenMode == BattlePanel.SCREEN_CHOOSE_ENEMY_FOR_SKILL)) {
					if(i == enemyChoiceCursorPos) {
						g.setColor(Color.DARK_GRAY);
						g.fillRect(x + (xStep*i), y, 200, 150);
					}
				}
				
				g.drawImage(enemy.getEnemySet().getImage(), x + (xStep*i) + 50, y, null);
			}
		}
	}
	
	
	private void drawPlayerStatsAt(Graphics2D g, Integer playerIndex, Integer x, Integer y) {
		HumanPlayer player = null;
		
		try {
			player = game.getParty().get(playerIndex);
		}
		catch(Exception e) {
			player = null;
		}
		
		if(player == null) {
			return;
		}
		
		//Draw Highlighter for Current Player
		if(battleStep == BattlePanel.BATTLE_PLAYER) {
			if(screenMode != BattlePanel.SCREEN_DIALOG) {
				if(currentPlayer == playerIndex) {
					g.setColor(new Color(0,0,214));
					g.fillRect(x+12, y+12, 176, 192);
				}
			}
		}
		
		g.setColor(Color.WHITE);
		
		g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,30));
		g.drawString(player.getName(), x+16, y+60);
		
		g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,20));
		g.drawString("HP: " + player.getHP() + " / " + player.getMaxHP(),x+16,y+120);
		g.drawString("MP: " + player.getMP() + " / " + player.getMaxMP(),x+16,y+150);
	}
	
}
