package de.heatup.ui.menu;

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

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

import de.heatup.highscore.ScoreEntry;
import de.heatup.logging.Logger;
import de.heatup.mapengine.MapEngine;
import de.heatup.ui.FontLoader;
import de.heatup.ui.PanelBuilder;

public class HighscorePanel {
	
	// dynamic panels 
	private static JPanel highscorePanel = new JPanel();
	private static JPanel headingPanel;
	private static JPanel contentPanel;
	
	// default text panels and labels
	private static JLabel headingLabel, dividerLabel;
	private static GridLayout grid = new GridLayout(2, 1);
	
	
	// Font Loader and font Settings
	private static FontLoader fl = new FontLoader();
	private static Font ttfbase, emulogicHeading, emulogicDivider, emulogicSmall;
	
	// MapEngine used to get and set the Highscore
	private static MapEngine me;
	private static int scoreEntries = 0;
	
	public HighscorePanel() {
		
		initHighscorePanel();
		getHighscores();
	}
	
	private static void getHighscores() {
		
		// TODO : Init the MapEngine before a new game is started for getting highscores
		me = PanelBuilder.getMapEngine();
		
		// only for testing purposes
//		me.getHighscoreHandler().setHighscore(me.getMapHash(), "Daniel", 3134);
//		me.getHighscoreHandler().setHighscore(me.getMapHash(), "Tim", 2010);
//		me.getHighscoreHandler().saveHighscores();
		
		// count Score entries for building the GridLayout
		for(ScoreEntry score : me.getHighscoreHandler().getHighscores(me.getMapHash())) {
			scoreEntries++;
			Logger.write(score.getName()+" "+score.getScore());
		}
	}
		
	private static void initHighscorePanel() {
		highscorePanel.setSize(new Dimension(800, 600));
		highscorePanel.setBackground(Color.BLACK);
		highscorePanel.setFocusable(false);
				
		// build full highscorePanel
		initHeadingPanel();
		initContentPanel();
		highscorePanel.add(headingPanel);
		highscorePanel.add(contentPanel);
		highscorePanel.setVisible(true);		
	}
	
	private static void initHeadingPanel() {
		headingPanel = new JPanel();
		
		headingPanel.setSize(new Dimension(800, 50));
		headingPanel.setBackground(Color.BLACK);
		headingPanel.setFocusable(false);
		headingPanel.setLayout(grid);
		headingPanel.setVisible(true);
		
		ttfbase = FontLoader.getFont();
		emulogicHeading = ttfbase.deriveFont(Font.PLAIN, 20);
		emulogicDivider = ttfbase.deriveFont(Font.PLAIN, 10);
		emulogicSmall = ttfbase.deriveFont(Font.PLAIN, 12);
		
		headingLabel = new JLabel();
		headingLabel.setText("HeatUP! - Highscores");
		headingLabel.setForeground(Color.YELLOW);
		headingLabel.setFont(emulogicHeading);
		
		dividerLabel = new JLabel();
		dividerLabel.setText("----------------------------------------");
		dividerLabel.setForeground(Color.YELLOW);
		dividerLabel.setFont(emulogicDivider);
		
		headingPanel.add(headingLabel);
		headingPanel.add(dividerLabel);
	}
	
	private static void initContentPanel() {
		contentPanel = new JPanel();
		
		contentPanel.setSize(new Dimension(800, 550));
		contentPanel.setBackground(Color.BLACK);
		contentPanel.setFocusable(false);
		
		contentPanel.setVisible(true);
		
		// GridLayout for Scores
		JPanel scores = new JPanel();
		scores.setBackground(Color.BLACK);
		scores.setFocusable(false);
		scores.setVisible(true);
		
		getHighscores();
		
		scores.setLayout(new GridLayout(0, scoreEntries));
		for(ScoreEntry score : me.getHighscoreHandler().getHighscores(me.getMapHash())) {
			JLabel l = new JLabel();
			l.setBackground(Color.BLACK);
			l.setForeground(Color.YELLOW);
			l.setFont(emulogicSmall);
			l.setVisible(true);
			
			l.setText(score.getName() + " ---> " + score.getScore());
			scores.add(l);
		}
		
		contentPanel.add(scores);
	}
		

	public static JPanel getHighscorePanel() {
		return highscorePanel;
	}
	

	public static void setHighscorePanel(JPanel highscorePanel) {
		HighscorePanel.highscorePanel = highscorePanel;
	}
	

	public static JPanel getContentPanel() {
		return contentPanel;
	}
	

	public static void setContentPanel(JPanel contentPanel) {
		HighscorePanel.contentPanel = contentPanel;
	}
}
