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.Item;
import rpgEngine.Data.Abstract.Shop;
import rpgEngine.Logic.Camera.GenericCamera;
import rpgEngine.Logic.Camera.ShopCamera;
import rpgEngine.Logic.Game.ItemTrader;

public class ShopPanel extends GenericPanel {
	private static final long serialVersionUID = 6538729434640140830L;
	private GenericCamera camera;
	
	private Integer cursorPosition;
	private Integer invCursorPosition;
	private String message;
	
	public static final Integer CURSOR_CANCEL = 0;
	public static final Integer CURSOR_BUY = 1;
	public static final Integer CURSOR_SELL = 2;
	public static final Integer CURSOR_BUY_INVENTORY = 3;
	public static final Integer CURSOR_SELL_INVENTORY = 4;
	
	
	public ShopPanel(Integer id, MainFrame p) {
		super(id, p);
		
		camera = new ShopCamera(this);
		cursorPosition = CURSOR_BUY;
		invCursorPosition = 0;
		message = "";
	}
	
	@Override
	public void ProcessKeyEvent(KeyEvent e) {
		message = "";
		
		if(e.getKeyCode() == KeyEvent.VK_ENTER) {
			processActionKey();
		}
		else if(e.getKeyCode() == KeyEvent.VK_SPACE) {
			processActionKey();
		}
		else if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
			processEscapeKey();
		}
		else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
			if(cursorPosition == CURSOR_BUY_INVENTORY) {
				invCursorPosition = invCursorPosition + 2;
				
				if(invCursorPosition > getShopItemCount()) {
					invCursorPosition = getShopItemCount();
				}	
			}
			else if(cursorPosition == CURSOR_SELL_INVENTORY) {
				invCursorPosition = invCursorPosition + 2;
				
				if(invCursorPosition > getPlayerItemCount()) {
					invCursorPosition = getPlayerItemCount();
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_UP) {
			if(cursorPosition == CURSOR_BUY_INVENTORY) {
				invCursorPosition = invCursorPosition - 2;
				
				if(invCursorPosition < 1) {
					invCursorPosition = 1;
				}
			}
			else if(cursorPosition == CURSOR_SELL_INVENTORY) {
				invCursorPosition = invCursorPosition - 2;
				
				if(invCursorPosition < 1) {
					invCursorPosition = 1;
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_LEFT) {
			if(cursorPosition == CURSOR_BUY) {
				cursorPosition = CURSOR_CANCEL;
			}
			else if(cursorPosition == CURSOR_SELL) {
				cursorPosition = CURSOR_BUY;
			}
			else if(cursorPosition == CURSOR_CANCEL) {
				cursorPosition = CURSOR_SELL;
			}
			else if(cursorPosition == CURSOR_BUY_INVENTORY) {
				invCursorPosition = invCursorPosition - 1;
				
				if(invCursorPosition < 1) {
					invCursorPosition = 1;
				}
			}
			else if(cursorPosition == CURSOR_SELL_INVENTORY) {
				invCursorPosition = invCursorPosition - 1;
				
				if(invCursorPosition < 1) {
					invCursorPosition = 1;
				}
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
			if(cursorPosition == CURSOR_BUY) {
				cursorPosition = CURSOR_SELL;
			}
			else if(cursorPosition == CURSOR_SELL) {
				cursorPosition = CURSOR_CANCEL;
			}
			else if(cursorPosition == CURSOR_CANCEL) {
				cursorPosition = CURSOR_BUY;
			}
			else if(cursorPosition == CURSOR_BUY_INVENTORY) {
				invCursorPosition = invCursorPosition + 1;
				
				if(invCursorPosition > getShopItemCount()) {
					invCursorPosition = getShopItemCount();
				}
			}
			else if(cursorPosition == CURSOR_SELL_INVENTORY) {
				invCursorPosition = invCursorPosition + 1;
				
				if(invCursorPosition > getPlayerItemCount()) {
					invCursorPosition = getPlayerItemCount();
				}
			}
		}
	}
	
	
	private void processActionKey() { 
		if(cursorPosition == CURSOR_CANCEL) {
			this.getParentMainFrame().showScreen(MainFrame.PANEL_GAME);
		}
		else if(cursorPosition == CURSOR_BUY) {
			if(shopHasInventory()) {
				cursorPosition = CURSOR_BUY_INVENTORY;
				invCursorPosition = 1;
			}
			else {
				message = "Shop has nothing to sell";
			}
		}
		else if(cursorPosition == CURSOR_SELL) {
			if(playerHasInventory()) {
				cursorPosition = CURSOR_SELL_INVENTORY;
				invCursorPosition = 1;
			}
			else {
				message = "Player has nothing to sell";
			}
		}
		else if(cursorPosition == CURSOR_BUY_INVENTORY) {
			//Buy Item
			Boolean result = false;
			ItemTrader it = null;
			Item toBuy = null;
			Game game = this.getParentMainFrame().getGame();
			Shop shop = game.getActiveShop();
			
			toBuy = shop.getItems().get(invCursorPosition - 1);
			
			it = new ItemTrader(game);
			result = it.buyItem(toBuy);
			
			if(result) {
				message = "Bought " + toBuy.getName();
			}
			else {
				message = "Can't afford " + toBuy.getName();
			}
		}
		else if(cursorPosition == CURSOR_SELL_INVENTORY) {
			//Sell Item
			Boolean isStillInInventory = false;
			ItemTrader it = null;
			Item toSell = null;
			Game game = this.getParentMainFrame().getGame();
			
			toSell = game.getInventory().getItems().get(invCursorPosition - 1).getItem();
			
			it = new ItemTrader(game);
			it.sellItem(toSell);
			
			message = "Sold " + toSell.getName();
			
			if(game.getInventory().getItems().size() == 0) {
				cursorPosition = CURSOR_SELL;
			}
			else {
				for(Integer i = 0; i < game.getInventory().getItems().size(); i++) {
					if(toSell.getID() == game.getInventory().getItems().get(i).getItem().getID()) {
						isStillInInventory = true;
					}
				}
				
				if(isStillInInventory == false) {
					invCursorPosition = 1;
				}
			}
		}
	}
	
	
	private Integer getShopItemCount() {
		Integer result = 0;
		Game game = null;
		Shop shop = null;
		
		game = this.getParentMainFrame().getGame();
		
		if(game == null) {
			return result;
		}
		
		shop = game.getActiveShop();
		
		if(shop == null) {
			return result;
		}
		
		result = shop.getItems().size();
		
		return result;
	}
	
	
	private Integer getPlayerItemCount() {
		Integer result = 0;
		
		Game game = null;
		
		game = this.getParentMainFrame().getGame();
		
		if(game == null) {
			return result;
		}
		
		result = game.getInventory().getItems().size();
			
		return result;
	}
	
	
	private Boolean shopHasInventory() {
		Boolean result = false;
		
		if(getShopItemCount() != 0) {
			result = true;
		}
		
		return result;
	}
	
	
	private Boolean playerHasInventory() {
		Boolean result = false;
		
		if(getPlayerItemCount() != 0) {
			result = true;
		}
		
		return result;
	}
	
	
	private void processEscapeKey() {		
		if(cursorPosition == CURSOR_BUY_INVENTORY) {
			cursorPosition = CURSOR_BUY;
		}
		else if (cursorPosition == CURSOR_SELL_INVENTORY) {
			cursorPosition = CURSOR_SELL;
		}
		else {
			cursorPosition = CURSOR_BUY;
			invCursorPosition = 0;
			this.getParentMainFrame().showScreen(MainFrame.PANEL_GAME);	
		}
	}
	
	
	@Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
     
        BufferedImage img = camera.getImage();
        
        g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), null);
    }

	public Integer getCursorPosition() {
		return cursorPosition;
	}

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

	public Integer getInvCursorPosition() {
		return invCursorPosition;
	}

	public void setInvCursorPosition(Integer invCursorPosition) {
		this.invCursorPosition = invCursorPosition;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	

}
