package rpgEngine.Logic.Camera;

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

import rpgEngine.Data.Abstract.Game;
import rpgEngine.Data.Abstract.Party.HumanPlayer;
import rpgEngine.Data.Abstract.Party.Skill;
import rpgEngine.Resources.FontPool;
import rpgEngine.Resources.ImagePool;
import rpgEngine.UI.SkillsMenuPanel;

public class SkillsMenuCamera extends GenericCamera {
	private SkillsMenuPanel panel;
	
	public SkillsMenuCamera(SkillsMenuPanel p) {
		super();
		
		panel = p;
	}

	@Override
	public BufferedImage getImage() {
		BufferedImage img = null;
		Game game = null;
		Graphics2D g = null;
		Integer width = 800;
		Integer height = 600;
		Integer cursorPos = 0;
		Integer cursorPartyPos = 0;
		Integer cursorSkillPos = 0;
		Double valD = 0.0;
		HumanPlayer player = null;
		Skill skill = null;
		Integer valI = 0;
		Integer x = 0;
		Integer y = 0;
		
		cursorPos = panel.getCursorPosition();
		cursorPartyPos = panel.getCursorPartyPosition();
		cursorSkillPos = panel.getCursorSkillPosition();
		
		img = new BufferedImage(800,600,BufferedImage.TYPE_INT_RGB);
		
		g = img.createGraphics();
		
		//Draw Background
		g.setColor(Color.BLACK);
		g.fillRect(0, 0, width, height);		
		g.drawImage(ImagePool.getInstance().getPoolItem(ImagePool.ID_MENU_SKILL_BACKGROUND),0,0,width,height,null);
		
		//Get Game
		game = panel.getParentMainFrame().getGame();
		
		if(game == null) {
			return img;
		}
		
		
		//Draw Menu Choices
		g.setColor(Color.WHITE);
		//g.setFont(new Font("Sans Serif",Font.BOLD,32));
		g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.BOLD,32));
		g.drawString("Back", 40, 64);
		
		//Draw Player Names
		x = 40;
		y = 180;
		for(int i = 0; i < game.getParty().size(); i++) {
			g.drawString(game.getParty().get(i).getName(), x, y);
			y = y + 60;
		}
		
		//Draw Skills if on a player
		if((cursorPos == SkillsMenuPanel.CURSOR_SKILL) || (cursorPos == SkillsMenuPanel.CURSOR_PARTY)) {
			if(cursorPartyPos != -1) {
				player = game.getParty().get(cursorPartyPos);
				
				//g.setFont(new Font("Sans Serif", Font.PLAIN, 16));
				g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.PLAIN,16));
				
				for(Integer i = 0; i < player.getSkills().size(); i++) {
					skill = player.getSkills().get(i);
					
					valD = Double.valueOf(i);
										
					if(i % 2 == 0) {
						x = 310;
					}
					else {
						x = 560;
					}
					
					valI = (int) Math.floor(valD / 2.0);
					y = (valI * 24) + 30;
					
					if(skill.getMpCost() <= player.getMP()) {
						if((skill.getType() == Skill.TYPE_HEALING) || (skill.getType() == Skill.TYPE_REVIVE)) {
							g.setColor(Color.WHITE);
						}
						else {
							g.setColor(Color.GRAY);
						}
					}
					else {
						g.setColor(Color.GRAY);
					}
					
					g.drawString(skill.getName(), x, y);
				}
			}
		}
		
		//Draw Skill Description if one is selected
		if(cursorPos == SkillsMenuPanel.CURSOR_SKILL) {
			if(cursorPartyPos != -1) {
				if(cursorSkillPos != -1) {
					player = game.getParty().get(cursorPartyPos);
					skill = player.getSkills().get(cursorSkillPos);
					
					g.setColor(Color.WHITE);
					//g.setFont(new Font("Sans Serif", Font.PLAIN, 20));
					g.setFont(FontPool.getInstance().getPoolItem(FontPool.FONT_MINECRAFT).deriveFont(Font.PLAIN,20));
					
					g.drawString(skill.getDescription(), 280, 574);
					
				}
			}
		}
		
		//Draw Cursor
		g.setColor(Color.WHITE);
		if(cursorPos == SkillsMenuPanel.CURSOR_QUIT) {
			g.fillRect(16, 44, 16, 16);
		}
		else if(cursorPos == SkillsMenuPanel.CURSOR_PARTY) {
			if(cursorPartyPos != -1) {
				g.fillRect(20, 160 + (cursorPartyPos * 60), 16, 16);
			}
		}
		else if(cursorPos == SkillsMenuPanel.CURSOR_SKILL) {
			if(cursorSkillPos != -1) {
				if(cursorSkillPos % 2 == 0) {
					x = 310;
				}
				else {
					x = 560;
				}
				
				valD = Double.valueOf(cursorSkillPos);
				valI = (int) Math.floor(valD / 2.0);
				y = (valI * 24) + 30;
				
				g.fillRect(x-18,y-12,12,12);
			}
		}		
		
		return img;
	}
	
}
