package de.heatup.ui;

import java.awt.EventQueue;
import java.awt.Point;
import java.util.ArrayList;

import javax.swing.JPanel;

import de.heatup.highscore.ScoreEntry;
import de.heatup.logging.Logger;
import de.heatup.mapengine.CollisionHandler;
import de.heatup.mapengine.MapEngine;
import de.heatup.mapengine.SpawnAssistant;
import de.heatup.objects.ImageLoader;
import de.heatup.objects.InstancedObjects;
import de.heatup.objects.Opponent;
import de.heatup.objects.Player;
import de.heatup.objects.PowerUpHandler;
import de.heatup.timers.HardwareResetTimers;
import de.heatup.ui.actionPanel.ActionControl;
import de.heatup.ui.actionPanel.ActionWatcher;
import de.heatup.ui.menu.MenuPanel;

public class PanelBuilder {
	public static JPanel actionPanel;
	private static JPanel outerPanel;
	public static JPanel statusPanel;
	private static JPanel gamePanel;
	
	public static KeyBindings kb;
	
	private static Player player01;
	
	private static MapEngine me;
	
	private static InstancedObjects instancedObjects;
	private static ImageLoader imageLoader;
	
	private static int gameState = 2; /* 1=menu; 2=game running */
	private static boolean gameLoaded = false;
	
	public static InstancedObjects getInstancedObjects() {
		return instancedObjects;
	}
	public static Player getPlayer() {
		return player01;
	}
	public static void toMenu() {
		
	}
	public static void tick() {
		switch (MenuPanel.gameExists) {
		case 1:
			//Logger.write("Menu tick");
			break;
		case 2:
			EventQueue.invokeLater(new Runnable() {
				@Override public void run() {
					ActionWatcher.tick();
				}
			});
			EventQueue.invokeLater(new Runnable() {
				@Override public void run() {
					ActionControl.tick();
				}
			});
			EventQueue.invokeLater(new Runnable() {
				@Override public void run() {
					HardwareResetTimers.tick();
				}
			});
			for (final Player object : PanelBuilder.getInstancedObjects().getObjects()) {
				EventQueue.invokeLater(new Runnable() {
					@Override public void run() {
						object.tick();
					}
				});
			}
			EventQueue.invokeLater(new Runnable() {
				@Override public void run() {
					CollisionHandler.tick();
				}
			});
			EventQueue.invokeLater(new Runnable() {
				@Override public void run() {
					StatusPanelAdder.tick();
				}
			});
			EventQueue.invokeLater(new Runnable() {
				@Override public void run() {
					PowerUpHandler.tick();
				}
			});
			break;
		default:
			Logger.write("Invalid gameState");
			System.exit(-1);
		}
	}

	public static void setPanels(JPanel actionPanel, JPanel outerPanel, JPanel statusPanel, JPanel gamePanel) {
		PanelBuilder.actionPanel = actionPanel;
		PanelBuilder.outerPanel = outerPanel;
		PanelBuilder.statusPanel = statusPanel;
		PanelBuilder.gamePanel = gamePanel;
	}
	public static void startMenu() {
		
		// StatusPanel bauen
		//StatusPanelAdder.setPanel(statusPanel);
		//StatusPanelAdder.createPanel();

		// ActionPanel bauen
		//ActionControl.setPanel(actionPanel);		
		
		//createGame(1); /* wird eigentlich vom menue aufgerufen */
		
	}
	
	public static JPanel getGamePanel() {
		return gamePanel;
	}

	public static JPanel getOuterPanel() {
		return outerPanel;
	}
	
	public static void createGame(int map) {
		
		instancedObjects = new InstancedObjects();
		
		imageLoader = new ImageLoader();
		
		player01 = new Player(); // !!! PLAYER IMMER ZUERST INSTANZIIEREN, DANACH OPPONENTS !!!
		gamePanel.add(player01);
		player01.setBounds(25,25,25, 25); // !!! MUSS DRIN BLEIBEN TROTZ NACHTRÄGLICHES SPAWN UMSETZEN !!!

		instancedObjects.getObjects().add(player01);
		
//		Gegner hinzufügen
		
		for (int i = 0; i<4; i++) {
			Opponent newOpponent = new Opponent();
			gamePanel.add(newOpponent);
			newOpponent.setBounds(50, 50, 25, 25);
			instancedObjects.getObjects().add(newOpponent);
		}
		
		
		
		kb = new KeyBindings(player01);
		outerPanel.addKeyListener(kb);
		
		//MapEngine me;
		if (map > 0) {
			me = new MapEngine(map);
		} else {
			me = new MapEngine();
		}
		me.addAllToPanel(gamePanel);
		
		/**
		 * Highscore tests
		 */
		
		/**
		* for (ScoreEntry score : me.getHighscoreHandler().getHighscores(me.getMapHash())) {
		* 	Logger.write(score.getName()+" "+score.getScore());
		* }
		* me.getHighscoreHandler().setHighscore(me.getMapHash(), "Daniel", 3134);
		* me.getHighscoreHandler().saveHighscores();		
		*/
		
		/**
		 * Ende Highscore tests
		 */
		
		
		for (Player movingObject : instancedObjects.getObjects()) {
			Point spawnPoint = SpawnAssistant.getPathLocationOnGrid();
			movingObject.spawnAt(spawnPoint);
		}

		gameLoaded = true;
		
	}
	
	public static MapEngine getMapEngine() {
		return me;
	}
	
	public static ImageLoader getImageLoader() {
		return imageLoader;
	}
}
