package de.heatup.ui.menu;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;

import de.heatup.ui.Main;
import de.heatup.ui.PanelBuilder;

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.StatusPanelAdder;
import de.heatup.ui.actionPanel.ActionControl;

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

public class MapChoosePanel {
	
	// dynamic panel
	private static JPanel mapChoosePanel = new JPanel();
	
	// default text panels and labels
	private static JPanel headingPanel, dividerPanel, map1Panel, map2Panel, map3Panel, map4Panel;
	private static JLabel headingLabel, dividerLabel, map1Label, map2Label, map3Label, map4Label;
	private static GridLayout mainGrid = new GridLayout(6, 1);
	
	// Font loader and font Settings
	private static FontLoader fl = new FontLoader();
	private static Font ttfbase, emulogicHeading, emulogicDivider, emulogicSmall;
	
	
	public MapChoosePanel() {
		
		ttfbase = FontLoader.getFont();
		emulogicHeading = ttfbase.deriveFont(Font.PLAIN, 30);
		emulogicDivider = ttfbase.deriveFont(Font.PLAIN, 20);
		emulogicSmall = ttfbase.deriveFont(Font.PLAIN, 18);
		
		initMapChoosePanel();
		
		mapChoosePanel.add(headingPanel);
		mapChoosePanel.add(dividerPanel);
		
		initContentPanel();
		mapChoosePanel.add(map1Panel);
		mapChoosePanel.add(map2Panel);
		mapChoosePanel.add(map3Panel);
		mapChoosePanel.add(map4Panel);
	}
	
	private static void initMapChoosePanel() {
		
		mapChoosePanel.setSize(new Dimension(800, 600));
		mapChoosePanel.setVisible(true);
		mapChoosePanel.setFocusable(false);
		mapChoosePanel.setLayout(mainGrid);
		mapChoosePanel.setBackground(Color.BLACK);
		
		headingPanel = new JPanel();
		headingPanel.setSize(new Dimension(800, 50));
		headingPanel.setVisible(true);
		headingPanel.setBackground(Color.BLACK);
		
		headingLabel = new JLabel();
		headingLabel.setBackground(Color.BLACK);
		headingLabel.setForeground(Color.YELLOW);
		headingLabel.setFont(emulogicHeading);
		headingLabel.setText("HeatUp! - Map Auswahl");
		
		headingPanel.add(headingLabel);
		
		dividerPanel = new JPanel();
		dividerPanel.setSize(new Dimension(800, 50));
		dividerPanel.setVisible(true);
		dividerPanel.setBackground(Color.BLACK);
		
		dividerLabel = new JLabel();
		dividerLabel.setBackground(Color.BLACK);
		dividerLabel.setForeground(Color.YELLOW);
		dividerLabel.setFont(emulogicDivider);
		dividerLabel.setText("--------------------------------");
		
		dividerPanel.add(dividerLabel);		
	}
	
	
	private static void initContentPanel() {
		
		map1Panel = new JPanel();
		map1Panel.setSize(new Dimension(800, 20));
		map1Panel.setVisible(true);
		map1Panel.setBackground(Color.BLACK);
		
		map1Label = new JLabel();
		map1Label.setBackground(Color.BLACK);
		map1Label.setForeground(Color.YELLOW);
		map1Label.setFont(emulogicSmall);
		map1Label.setText("Map -1-");
		map1Label.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				
				if(MenuPanel.gameExists == 1) {
					
					StatusPanelAdder.setPanel(PanelBuilder.statusPanel);
					StatusPanelAdder.createPanel();
					
					ActionControl.setPanel(PanelBuilder.actionPanel);
					
					PanelBuilder.createGame(1);
					MenuPanel.gameExists = 2;
					MapChoosePanel.getMapChoosePanel().setVisible(false);
					MenuPanel.getMenuPanel().setVisible(false);
					
					PanelBuilder.getOuterPanel().add(PanelBuilder.getGamePanel(), BorderLayout.CENTER);
					Logger.write("Game started with map 1");
					
				} else if(MenuPanel.gameExists == 2) {
					if(Main.getPause()) {
						
					}
					Logger.write("game already started");
				}
				Logger.write("Game started using map : " + map1Label.getText());
			}
		});
		map1Panel.add(map1Label);
		
		//
		
		map2Panel = new JPanel();
		map2Panel.setSize(new Dimension(800, 20));
		map2Panel.setVisible(true);
		map2Panel.setBackground(Color.BLACK);
		
		map2Label = new JLabel();
		map2Label.setBackground(Color.BLACK);
		map2Label.setForeground(Color.YELLOW);
		map2Label.setFont(emulogicSmall);
		map2Label.setText("Map -2-");
		map2Label.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				
				if(MenuPanel.gameExists == 1) {
					
					StatusPanelAdder.setPanel(PanelBuilder.statusPanel);
					StatusPanelAdder.createPanel();
					
					ActionControl.setPanel(PanelBuilder.actionPanel);
					
					PanelBuilder.createGame(2);
					MenuPanel.gameExists = 2;
					MapChoosePanel.getMapChoosePanel().setVisible(false);
					MenuPanel.getMenuPanel().setVisible(false);
					
					PanelBuilder.getOuterPanel().add(PanelBuilder.getGamePanel(), BorderLayout.CENTER);
					Logger.write("Game started with map 2");
					
				} else if(MenuPanel.gameExists == 2) {
					if(Main.getPause()) {
						
					}
					Logger.write("game already started");
				}
				Logger.write("Game started using map : " + map2Label.getText());
			}
		});
		map2Panel.add(map2Label);
		
		//
		
		map3Panel = new JPanel();
		map3Panel.setSize(new Dimension(800, 20));
		map3Panel.setVisible(true);
		map3Panel.setBackground(Color.BLACK);
		
		map3Label = new JLabel();
		map3Label.setBackground(Color.BLACK);
		map3Label.setForeground(Color.YELLOW);
		map3Label.setFont(emulogicSmall);
		map3Label.setText("Map -3-");
		map3Label.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				
				if(MenuPanel.gameExists == 1) {
					
					StatusPanelAdder.setPanel(PanelBuilder.statusPanel);
					StatusPanelAdder.createPanel();
					
					ActionControl.setPanel(PanelBuilder.actionPanel);
					
					PanelBuilder.createGame(3);
					MenuPanel.gameExists = 2;
					MapChoosePanel.getMapChoosePanel().setVisible(false);
					MenuPanel.getMenuPanel().setVisible(false);
					
					PanelBuilder.getOuterPanel().add(PanelBuilder.getGamePanel(), BorderLayout.CENTER);
					Logger.write("Game started with map 3");
					
				} else if(MenuPanel.gameExists == 2) {
					if(Main.getPause()) {
						
					}
					Logger.write("game already started");
				}
				Logger.write("Game started using map : " + map3Label.getText());
			}
		});
		map3Panel.add(map3Label);
		
		//
		
		map4Panel = new JPanel();
		map4Panel.setSize(new Dimension(800, 20));
		map4Panel.setVisible(true);
		map4Panel.setBackground(Color.BLACK);
		
		map4Label = new JLabel();
		map4Label.setBackground(Color.BLACK);
		map4Label.setForeground(Color.YELLOW);
		map4Label.setFont(emulogicSmall);
		map4Label.setText("Map -Custom-");
		map4Label.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				
				if(MenuPanel.gameExists == 1) {
					
					StatusPanelAdder.setPanel(PanelBuilder.statusPanel);
					StatusPanelAdder.createPanel();
					
					ActionControl.setPanel(PanelBuilder.actionPanel);
					
					PanelBuilder.createGame(0);
					MenuPanel.gameExists = 2;
					MapChoosePanel.getMapChoosePanel().setVisible(false);
					MenuPanel.getMenuPanel().setVisible(false);
					
					PanelBuilder.getOuterPanel().add(PanelBuilder.getGamePanel(), BorderLayout.CENTER);
					Logger.write("Game started with map 4");
					
				} else if(MenuPanel.gameExists == 2) {
					if(Main.getPause()) {
						
					}
					Logger.write("game already started");
				}
				Logger.write("Game started using map : " + map4Label.getText());
			}
		});
		map4Panel.add(map4Label);
		
	}

	public static JPanel getMapChoosePanel() {
		return mapChoosePanel;
	}
	
	public static void setMapChoosePanel(JPanel mapChoosePanel) {
		MapChoosePanel.mapChoosePanel = mapChoosePanel;
	}
	
}
