package de.heatup.ui.menu;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JPanel;

import de.heatup.logging.Logger;
import de.heatup.ui.FontLoader;
import de.heatup.ui.Main;
import de.heatup.ui.PanelBuilder;

public class MenuPanel {
	
	// main menu panel
	private static JPanel menuPanel = new JPanel();
	
	// panels for menu buttons
	static JPanel start, end, highscore, credits, tutorial;
	
	// sub buttons for functions
	static JLabel startGame, endGame, showHighscore, showCredits, showTutorial;	
	
	// font settings
	private static FontLoader fl = new FontLoader();
	private static Font emulogic;
	private static Font ttfbase;
	
	// 1=GameNotInit 2=GameInit
	public static int gameExists = 1;
	
	
	public MenuPanel() { 
		/** not in usage yet */ 		
	}
	
	public static JLabel getStartGameLabel() {
		return startGame;
	}
	
	public static void setStartGameLabel(String text) {
		startGame.setText(text);
	}
	
	public static void setShowCreditsLabel(String text) {
		showCredits.setText(text);
	}
	
	public static void setShowTutorialLabel(String text) {
		showTutorial.setText(text);
	}
	
	public static void setShowHighscoreLabel(String text) {
		showHighscore.setText(text);
	}
	
	public static void setMenuPanel() {		
				
		menuPanel.setPreferredSize(new Dimension(800,600));
		menuPanel.setFocusable(false);
		
		GridLayout menuGrid = new GridLayout(5, 1);
		menuPanel.setBackground(Color.BLACK);
		menuPanel.setLayout(menuGrid);
		
		ttfbase = FontLoader.getFont();
		emulogic = ttfbase.deriveFont(Font.PLAIN, 28);
				
		createStart();
		createEnd();
		createHighscore();	
		createCredits();
		createTutorial();
				
		buildMenuPanel();
	}
	
	public static JPanel getMenuPanel() {		
		menuPanel.setVisible(true);
		return menuPanel;
	}
	
	/**
	 * Create a JPanel which contains full function for starting a new Game
	 */
	private static void createStart() {		
		start = new JPanel();
		start.setBackground(Color.BLACK);
		
		startGame = new JLabel();
		startGame.setText("[ Spiel starten ]");
		startGame.setForeground(Color.YELLOW);
		startGame.setFont(emulogic);
		
		startGame.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if(gameExists == 1) {
									
					// StatusPanel bauen
					//StatusPanelAdder.setPanel(PanelBuilder.statusPanel);
					//StatusPanelAdder.createPanel();

					// ActionPanel bauen
					//ActionControl.setPanel(PanelBuilder.actionPanel);
										
					//PanelBuilder.createGame(1);
					//gameExists = 2;
					//menuPanel.setVisible(false);
					
					//PanelBuilder.getOuterPanel().add(PanelBuilder.getGamePanel(),BorderLayout.CENTER);					
					
					//** game, action, status panel redraw and check visibility */
										
					//Logger.write("Game Started via menu");
					Logger.write("MapChoose Panel called via menu");
					
					// add new MapChoosePanel
					menuPanel.setVisible(false);

					MapChoosePanel p = new MapChoosePanel();
					PanelBuilder.getOuterPanel().add(MapChoosePanel.getMapChoosePanel(), BorderLayout.CENTER);
					
				} else if (gameExists == 2) {
					if(Main.getPause()) {
						
					}
					Logger.write("game already started");
				}
			}
		});
		
		start.add(startGame);		
	}
	
	/**
	 * Create a JPanel which contains full function for Ending the current game
	 */	
	private static void createEnd() {
		end = new JPanel();
		end.setBackground(Color.BLACK);
		
		endGame = new JLabel("[ Spiel beenden ]");
		endGame.setForeground(Color.YELLOW);
		endGame.setFont(emulogic);
		
		endGame.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}
		});
		
		end.add(endGame);
	}
	
	/**
	 * Create a JPanel which contains full function for showing the Highscores
	 */
	private static void createHighscore() {
		highscore = new JPanel();
		highscore.setBackground(Color.BLACK);
		
		showHighscore = new JLabel("[ Highscores anzeigen ]");
		showHighscore.setForeground(Color.YELLOW);
		showHighscore.setFont(emulogic);
		
		showHighscore.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				
				if(gameExists == 1) {
					
					menuPanel.setVisible(false);
					
					//HighscorePanel p = new HighscorePanel();
					//PanelBuilder.getOuterPanel().add(HighscorePanel.getHighscorePanel(), BorderLayout.CENTER);
					MapChooseHighschorePanel p = new MapChooseHighschorePanel();
					PanelBuilder.getOuterPanel().add(MapChooseHighschorePanel.getMapChooseHighscorePanel(), BorderLayout.CENTER);
					
					Logger.write("show highscore");	
				} else if (gameExists == 2) {
					Logger.write("cant show scores while game is running");
				}
			}
		});
		
		highscore.add(showHighscore);
	}

	/**
	 * Create a JPanel which contains full function for showing the Credits
	 */
	private static void createCredits() {
		credits = new JPanel();
		credits.setBackground(Color.BLACK);
		
		showCredits = new JLabel("[ Credits anzeigen ]");
		showCredits.setForeground(Color.YELLOW);
		showCredits.setFont(emulogic);
		
		showCredits.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				
				if(gameExists == 1) {
					
					menuPanel.setVisible(false);					
					PanelBuilder.getOuterPanel().add(CreditsPanel.getCreditsPanel(), BorderLayout.CENTER);
					
					Logger.write("show credits");					
				} else if(gameExists == 2) {
					Logger.write("cant show credits while game is running");
				}							
			}
		});
		
		credits.add(showCredits);
	}
	
	/**
	 * Create a JPanel which contains full function for showing the Tutorial
	 */
	private static void createTutorial() {
		tutorial = new JPanel();
		tutorial.setBackground(Color.BLACK);
		
		showTutorial = new JLabel("[ Tutorial starten ]");
		showTutorial.setForeground(Color.YELLOW);
		showTutorial.setFont(emulogic);
		
		showTutorial.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				// @TODO: implement show tutorial
				Logger.write("show tutorial");				
			}
		});
		
		tutorial.add(showTutorial);
	}
	
	
	/** 
	 * building the MenuPanel from all previous created panels
	 * */
	private static void buildMenuPanel() {		
		menuPanel.add(start);
		menuPanel.add(end);
		menuPanel.add(highscore);
		menuPanel.add(credits);
		menuPanel.add(tutorial);
		
	}	
}
