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 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;

public class HighscorePanel {
	
	private static JPanel highscorePanel = new JPanel();
	
	// panels and labels
	private static JPanel headingPanel, dividerPanel;
	private static JLabel headingLabel, dividerLabel;
	private static JPanel scores;
	
	
	// Font Loader and font Settings
	private static FontLoader fl = new FontLoader();
	private static Font ttfbase, emulogicHeading, emulogicDivider, emulogicSmall;
	
	private static int mapID;
	private static MapEngine me;
	private static int scoreEntries;
	
	public HighscorePanel(int mapID) {
		
		HighscorePanel.mapID = mapID;
		getHighscores();
		
		// initialize fonts
		ttfbase = FontLoader.getFont();
		emulogicHeading = ttfbase.deriveFont(Font.PLAIN, 20);
		emulogicDivider = ttfbase.deriveFont(Font.PLAIN, 10);
		emulogicSmall = ttfbase.deriveFont(Font.PLAIN, 12);
		
		// build panel
		highscorePanel.setLayout(new GridLayout(3, 1));
		highscorePanel.setSize(new Dimension(800, 600));
		highscorePanel.setBackground(Color.BLACK);
		highscorePanel.setFocusable(false);
		highscorePanel.setVisible(true);
		
		initHighscorePanelHeading();
		highscorePanel.add(headingPanel);
		highscorePanel.add(dividerPanel);
		
		initHighscorePanelContent();
		highscorePanel.add(scores, BorderLayout.CENTER);
				
	}
	
	private static void initHighscorePanelHeading() {
		headingPanel = new JPanel();
		headingPanel.setSize(new Dimension(800, 50));
		headingPanel.setBackground(Color.BLACK);
		headingPanel.setFocusable(false);
		headingPanel.setVisible(true);
		
		headingLabel = new JLabel();
		headingLabel.setForeground(Color.YELLOW);
		headingLabel.setBackground(Color.BLACK);
		headingLabel.setFocusable(false);
		headingLabel.setVisible(true);
		headingLabel.setFont(emulogicHeading);
		headingLabel.setText("HeatUP - Highscores");
		
		headingPanel.add(headingLabel);
		
		dividerPanel = new JPanel();
		dividerPanel.setSize(new Dimension(800, 50));
		dividerPanel.setBackground(Color.BLACK);
		dividerPanel.setFocusable(false);
		dividerPanel.setVisible(true);
		
		dividerLabel = new JLabel();
		dividerLabel.setForeground(Color.YELLOW);
		dividerLabel.setBackground(Color.BLACK);
		dividerLabel.setFocusable(false);
		dividerLabel.setVisible(true);
		dividerLabel.setFont(emulogicDivider);
		dividerLabel.setText("------------------------------");
		
		dividerPanel.add(dividerLabel);
	}
	
	private static void initHighscorePanelContent() {
		
		scores = new JPanel();
		scores.setSize(new Dimension(800, 500));
		scores.setLayout(new GridLayout(scoreEntries, 1));
		scores.setBackground(Color.BLACK);
		scores.setFocusable(false);
		scores.setVisible(true);
		
		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);
		}		
	}
	
	private static void getHighscores() {
		
		if(mapID != 0) {
			me = new MapEngine(mapID);
		} else if (mapID == 0){
			me  = new MapEngine();
		}
		
		// count Score entries for building the GridLayout
		for(ScoreEntry score : me.getHighscoreHandler().getHighscores(me.getMapHash())) {			
			scoreEntries++;
			Logger.write(score.getName() + " " + score.getScore());
		}
	}
	
	public static JPanel getHighscorePanel() {
		return highscorePanel;
	}
	
	public static int getMapID() {
		return mapID;
	}
	
	public static void setMapID(int mapID) {
		HighscorePanel.mapID = mapID;
	}
}
