package rpgEngine.UI;


import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Date;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;


import javax.swing.JFrame;

import rpgEngine.Data.Abstract.Game;
import rpgEngine.Logic.Game.MusicPlayer;

public class MainFrame extends JFrame implements KeyListener {
	
	private static final long serialVersionUID = 3369363660409967640L;
	private Game game;
	private Dictionary<Integer,GenericPanel> panels;
	private GenericPanel activePanel;
	private Date lastKeyStrokeTime;
	
	public static final Integer PANEL_TITLE = 0;
	public static final Integer PANEL_GAME = 1;
	public static final Integer PANEL_MENU_MAIN = 2;
	public static final Integer PANEL_MENU_STATUS = 3;
	public static final Integer PANEL_MENU_ITEMS = 4;
	public static final Integer PANEL_MENU_SKILLS = 5;
	public static final Integer PANEL_MENU_EQUIP = 6;
	public static final Integer PANEL_SAVE_GAME = 7;
	public static final Integer PANEL_LOAD_GAME = 8;
	public static final Integer PANEL_SHOP = 9;
	public static final Integer PANEL_BATTLE = 10;
	
	
	public MainFrame() {
		super();
		
		panels = new Hashtable<Integer,GenericPanel>();
		activePanel = null;
		game = null;
		lastKeyStrokeTime = new Date();
		
		this.setSize(800, 600);
		this.setTitle("RPG Game");
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.addKeyListener(this);
	}

	
	public void showScreen(int p) {
		if(panels.get(p) == null) {
			
			//load panel if needed
			if(p == PANEL_TITLE) {
				panels.put(p, new TitlePanel(PANEL_TITLE,this));
			}
			else if(p == PANEL_GAME) {
				panels.put(p, new GamePanel(PANEL_GAME,this));
			}
			else if(p == PANEL_MENU_MAIN) {
				panels.put(p, new MainMenuPanel(PANEL_MENU_MAIN,this));
			}
			else if(p == PANEL_MENU_STATUS) {
				panels.put(p, new StatusMenuPanel(PANEL_MENU_STATUS,this));
			}
			else if(p == PANEL_MENU_ITEMS) {
				panels.put(p, new ItemMenuPanel(PANEL_MENU_ITEMS,this));
			}
			else if(p == PANEL_MENU_SKILLS) {
				panels.put(p, new SkillsMenuPanel(PANEL_MENU_SKILLS,this));
			}
			else if(p == PANEL_MENU_EQUIP) {
				panels.put(p, new EquipmentMenuPanel(PANEL_MENU_EQUIP,this));
			}
			else if(p == PANEL_SAVE_GAME) {
				panels.put(p, new SaveGamePanel(PANEL_SAVE_GAME,this));
			}
			else if(p == PANEL_LOAD_GAME) {
				panels.put(p, new LoadGamePanel(PANEL_LOAD_GAME,this));
			}
			else if(p == PANEL_SHOP) {
				panels.put(p, new ShopPanel(PANEL_SHOP,this));
			}
			else if(p == PANEL_BATTLE) {
				panels.put(p, new BattlePanel(PANEL_BATTLE,this));
			}
			
			this.add(panels.get(p));
		}
		
		//show screen
		hideAllPanels();
		activePanel = panels.get(p);
		panels.get(p).setEnabled(true);
		panels.get(p).setVisible(true);
		
		//change music based on screen
		MusicPlayer.getInstance().changeMusicBasedOnScreen(this);
	}
		
	
	private void hideAllPanels() {
		GenericPanel p = null;
		Enumeration<GenericPanel> e = panels.elements();
		
		while(e.hasMoreElements()) {
			p = e.nextElement();
			
			p.setEnabled(false);
			p.setVisible(false);
		}
	}


	@Override
	public void keyPressed(KeyEvent e) {
		if(activePanel == null) {
			return;
		}
		
		Date now = new Date();
		Long dif = now.getTime() - lastKeyStrokeTime.getTime();
		
		if(dif > 90) {
			lastKeyStrokeTime = now;
			
			activePanel.ProcessKeyEvent(e);
			activePanel.repaint();
		}
	}


	@Override
	public void keyReleased(KeyEvent e) {
		// do nothing
	}


	@Override
	public void keyTyped(KeyEvent e) {
		// do nothing
	}


	public Game getGame() {
		return game;
	}


	public void setGame(Game game) {
		this.game = game;
	}
	

	public GenericPanel getActivePanel() {
		return activePanel;
	}
	
}
