package rpgEngine.UI;

import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

import rpgEngine.Data.Abstract.Game;
import rpgEngine.Data.Abstract.Party.HumanPlayer;
import rpgEngine.Data.Abstract.Party.Skill;
import rpgEngine.Logic.Camera.GenericCamera;
import rpgEngine.Logic.Camera.SkillsMenuCamera;
import rpgEngine.Logic.Game.PlayerHealingSkillUse;

public class SkillsMenuPanel extends GenericPanel {
	private static final long serialVersionUID = 6538729434652140830L;
	private GenericCamera camera;
	private Integer cursorPosition;
	private Integer cursorPartyPosition;
	private Integer cursorSkillPosition;
	private GenericDialog partyDialog;
	
	public static final Integer CURSOR_QUIT = 0;
	public static final Integer CURSOR_PARTY = 1;
	public static final Integer CURSOR_SKILL = 2;
	
	public static final Integer SKILL_LIST_MIN = 0;
	public static final Integer SKILL_LIST_MAX = 41;
	
	
	public SkillsMenuPanel(Integer id, MainFrame p) {
		super(id, p);
		
		camera = new SkillsMenuCamera(this);
		cursorPosition = CURSOR_QUIT;
		cursorPartyPosition = -1;
		cursorSkillPosition = -1;
		partyDialog = null;
	}
	
	
	@Override
	public void ProcessKeyEvent(KeyEvent e) {
		Game game = null;
		HumanPlayer player = null;
		Integer numSkills = 0;
		
		game = this.getParentMainFrame().getGame();
		
		if(game == null) {
			return;
		}

		//handle dialog input since its modal
		if(partyDialog != null) {
			partyDialog.processKeyEvent(e);
			
			if(partyDialog.isProcessed() == false) {
				return;
			}
			else {
				if(partyDialog.getResult() != PartyDialog.RESULT_CANCEL) {
					//Use Item
					useSkill(false,partyDialog.getResult());
					
					partyDialog = null;
					return;
				}
				else {
					partyDialog = null;
					return;
				}
			}
		}
		
		//handle normal input otherwise
		if(e.getKeyCode() == KeyEvent.VK_UP) {
			if(cursorPosition == CURSOR_PARTY) {
				if(cursorPartyPosition == 0) {
					cursorPosition = CURSOR_QUIT;
					cursorPartyPosition = -1;
				}
				else {
					cursorPartyPosition--;
				}
			}
			else if(cursorPosition == CURSOR_SKILL) {
				cursorSkillPosition = cursorSkillPosition - 2;
				
				if(cursorSkillPosition < 0) {
					cursorSkillPosition = 0;
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
			if(cursorPosition == CURSOR_QUIT) {
				cursorPosition = CURSOR_PARTY;
				cursorPartyPosition = 0;
			}
			else if(cursorPosition == CURSOR_SKILL) {
				player = game.getParty().get(cursorPartyPosition);
				numSkills = player.getSkills().size();
				
				cursorSkillPosition = cursorSkillPosition + 2;
				
				if(cursorSkillPosition >= numSkills) {
					cursorSkillPosition = numSkills - 1;
				}
			}
			else if(cursorPosition == CURSOR_PARTY) {
				cursorPartyPosition++;
				
				if(game.getParty().size() <= cursorPartyPosition) {
					cursorPartyPosition = game.getParty().size() - 1;
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
			if(cursorPosition == CURSOR_SKILL) {
				cursorSkillPosition = cursorSkillPosition - 1;
				
				if(cursorSkillPosition < 0) {
					cursorSkillPosition = 0;
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
			if(cursorPosition == CURSOR_SKILL) {
				player = game.getParty().get(cursorPartyPosition);
				numSkills = player.getSkills().size();
				
				cursorSkillPosition = cursorSkillPosition + 1;
				
				if(cursorSkillPosition >= numSkills) {
					cursorSkillPosition = numSkills - 1;
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_ENTER) {
			processActionKey();
		}
		else if(e.getKeyCode() == KeyEvent.VK_SPACE) {
			processActionKey();
		}
		else if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
			processEscapeKey();
		}
	}
	
	
	private void processActionKey() {
		if(cursorPosition == CURSOR_QUIT) {
			cursorPosition = CURSOR_QUIT;
			cursorPartyPosition = -1;
			cursorSkillPosition = -1;
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_MAIN);
		}
		else if(cursorPosition == CURSOR_PARTY) {
			if(cursorPartyPosition == -1) {
				return;
			}
			else {
				Game game = this.getParentMainFrame().getGame();
				HumanPlayer player = null;
				
				if(game == null) {
					return;
				}
				
				player = game.getParty().get(cursorPartyPosition);
				
				if(player.getSkills().size() == 0) {
					return;
				}
				
				cursorPosition = CURSOR_SKILL;
				cursorSkillPosition = 0;
			}
		}
		else if(cursorPosition == CURSOR_SKILL) {
			Game game = this.getParentMainFrame().getGame();
			HumanPlayer player = null;
			Skill skill = null;
			PlayerHealingSkillUse psu = null;
			
			if(game == null) {
				return;
			}
			
			player = game.getParty().get(cursorPartyPosition);
			skill = player.getSkills().get(cursorSkillPosition);
			psu = new PlayerHealingSkillUse(player,skill);
			
			if((skill.getType() == Skill.TYPE_HEALING) || (skill.getType() == Skill.TYPE_REVIVE)) {
				if(psu.canUseSkill()) {
					if(skill.getTarget() == Skill.TARGET_SINGLE) {
						partyDialog = new PartyDialog(game);
					}
					else {
						useSkill(true,-1);
					}
				}
			}
		}
	}
	
	
	private void processEscapeKey() {
		if(cursorPosition == CURSOR_QUIT) {
			cursorPosition = CURSOR_QUIT;
			cursorPartyPosition = -1;
			cursorSkillPosition = -1;
			this.getParentMainFrame().showScreen(MainFrame.PANEL_MENU_MAIN);
		}
		else if(cursorPosition == CURSOR_PARTY) {
			cursorPosition = CURSOR_QUIT;
			cursorPartyPosition = -1;
			cursorSkillPosition = -1;
		}
		else if(cursorPosition == CURSOR_SKILL) {
			cursorPosition = CURSOR_PARTY;
			cursorSkillPosition = -1;
		}
	}
	
	
	private void useSkill(boolean allPlayers, int playerNum) {
		Game game = this.getParentMainFrame().getGame();
		HumanPlayer player = null;
		Skill skill = null;
		PlayerHealingSkillUse psu = null;
		HumanPlayer targetPlayer = null;
		
		if(game == null) {
			return;
		}
		
		player = game.getParty().get(cursorPartyPosition);
		skill = player.getSkills().get(cursorSkillPosition);
		psu = new PlayerHealingSkillUse(player,skill);
		
		if(psu.canUseSkill()) {
			if(allPlayers == true) {
				psu.useSkillOnParty(game.getParty());
			}
			else {
				targetPlayer = game.getParty().get(playerNum);
				psu.useSkillOnPlayer(targetPlayer);
			}
		}
	}
	
	
	@Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
     
        BufferedImage img = camera.getImage();
        
        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
        
        //draw dialog on top of image
        if(partyDialog != null) {
        	partyDialog.drawDialogCenteredAt(g, this.getWidth()/2, this.getHeight()/2);
        }
    }
	
	
	public Integer getCursorPosition() {
		return cursorPosition;
	}

	public void setCursorPosition(Integer cursorPosition) {
		this.cursorPosition = cursorPosition;
	}


	public Integer getCursorPartyPosition() {
		return cursorPartyPosition;
	}


	public void setCursorPartyPosition(Integer cursorPartyPosition) {
		this.cursorPartyPosition = cursorPartyPosition;
	}


	public Integer getCursorSkillPosition() {
		return cursorSkillPosition;
	}


	public void setCursorSkillPosition(Integer cursorSkillPosition) {
		this.cursorSkillPosition = cursorSkillPosition;
	}
	
}
