package engine;

import java.awt.Desktop;
import java.awt.Graphics2D;

import engine.Game.STATE;
import utils.Stack;

/*  ------------- Nihilne ------------
    * Created by Mitch Weaver - 2016 *
      ~~~~ All Rights Reserved ~~~~ */

public class Menu {

    public static volatile Stack<MenuButton> menuButtons = new Stack<MenuButton>();

    public synchronized void init() {
	// PARAMETERS: x, y, width, height, type, isHovering
	MenuButton playButton = new MenuButton(0, 0, 155, 29, 0, false, true);
	MenuButton settingsButton = new MenuButton(0, 0, 155, 29, 2, false, true);
	MenuButton aboutButton = new MenuButton(0, 0, 155, 29, 4, false, true);
	MenuButton quitButton = new MenuButton(0, 0, 155, 29, 6, false, true);

	menuButtons.add(playButton);
	menuButtons.add(settingsButton);
	menuButtons.add(aboutButton);
	menuButtons.add(quitButton);
    }

    public synchronized void onClick() {
	for (int i = 0; i < menuButtons.size(); i++) {
			if (menuButtons.get(i).contains(MouseInput.mouseXY) && menuButtons.get(i).type == 0) { // PLAY BUTTON
		Game.state = STATE.GAME;
		Handler.cleanUp();
		Handler.init();
		Game.settings.cleanUp();
	    } else if (menuButtons.get(i).contains(MouseInput.mouseXY) && menuButtons.get(i).type == 2) { // SETTINGS BUTTON
		Game.settings.init();
		Game.state = STATE.SETTINGS;
	    } else if (menuButtons.get(i).contains(MouseInput.mouseXY) && menuButtons.get(i).type == 4) { // ABOUT BUTTON
	    	try { Desktop.getDesktop().browse(java.net.URI.create("https://www.steamcommunity.com/sharedfiles/filedetails/?id=668438834")); } catch (Exception e) { e.printStackTrace(); }
	    } else if (menuButtons.get(i).contains(MouseInput.mouseXY) && menuButtons.get(i).type == 6) { // QUIT BUTTON
		Game.cleanUp();
	    }
	}
    }

    public void render(Graphics2D g2d) {
	for (int i = 0; i < menuButtons.size(); i++) 
	    menuButtons.get(i).render(g2d);
	
    }

    public void tick() {
    	try {

		// tick buttons x and y to adjust for screen resolutoin changes
		menuButtons.get(0).x = (Game.WIDTH - Game.WIDTH / 3.5f);
		menuButtons.get(0).y = Game.HEIGHT * 0.65f;
		menuButtons.get(1).x = (Game.WIDTH - Game.WIDTH / 3.5f);
		menuButtons.get(1).y = Game.HEIGHT * 0.7f;
		menuButtons.get(2).x = (Game.WIDTH - Game.WIDTH / 3.5f);
		menuButtons.get(2).y = Game.HEIGHT * 0.75f;
		menuButtons.get(3).x = (Game.WIDTH - Game.WIDTH / 3.5f);
		menuButtons.get(3).y = Game.HEIGHT * 0.8f;
	
		for (int i = 0; i < menuButtons.size(); i++) 
		    menuButtons.get(i).tick();
	
    	} catch (Exception e) {}
    }

    public void cleanUp() {
	menuButtons.clear();
    }

}
