package engine;

import java.awt.Graphics2D;

import engine.Game.STATE;
import hud.inventory.Inventory;
import utils.Stack;

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

final class PauseMenu {

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

	public static void init() {
		MenuButton playButton = new MenuButton(0, 0, 155, 29, 0, false, true);
		MenuButton settingsButton = new MenuButton(0, 0, 155, 29, 2, false, true);
		MenuButton mainMenuButton = new MenuButton(0, 0, 155, 29, 4, false, true);
		MenuButton quitButton = new MenuButton(0, 0, 155, 29, 6, false, true);
		pauseButtons.add(playButton);
		pauseButtons.add(settingsButton);
		pauseButtons.add(mainMenuButton);
		pauseButtons.add(quitButton);
	}

	public static void tick() {

		try {
			// tick buttons x and y to adjust for screen resolutoin changes
			pauseButtons.get(0).x = (float) (Game.WIDTH / 2 - (155 / 2));
			pauseButtons.get(0).y = Game.HEIGHT * 0.6f;
			pauseButtons.get(1).x = (float) (Game.WIDTH / 2 - (155 / 2));
			pauseButtons.get(1).y = Game.HEIGHT * 0.65f;
			pauseButtons.get(2).x = (float) (Game.WIDTH / 2 - (155 / 2));
			pauseButtons.get(2).y = Game.HEIGHT * 0.7f;
			pauseButtons.get(3).x = (float) (Game.WIDTH / 2 - (155 / 2));
			pauseButtons.get(3).y = Game.HEIGHT * 0.75f;

			for (int i = 0; i < pauseButtons.size(); i++) {
				pauseButtons.get(i).tick();
			}
		} catch (ArrayIndexOutOfBoundsException e) {

		} catch (NullPointerException npe) {

		}
	}

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

	public synchronized static void onClick() {
		for (int i = 0; i < pauseButtons.size(); i++) {
			if (pauseButtons.get(i).contains(MouseInput.mouseXY) && pauseButtons.get(i).type == 0) {
				Game.state = STATE.GAME;
				Inventory.changeSettings();
				Game.settings.cleanUp();
				cleanUp();
			} else if (pauseButtons.get(i).contains(MouseInput.mouseXY) && pauseButtons.get(i).type == 2) {
				Game.settings.init();
				Game.state = STATE.SETTINGS;
				Game.settings.justCameFromPaused = true;
			} else if (pauseButtons.get(i).contains(MouseInput.mouseXY) && pauseButtons.get(i).type == 4) {
				Game.menu.cleanUp();
				Game.menu.init();
				Game.state = STATE.MENU;
				pauseButtons.clear();
			} else if (pauseButtons.get(i).contains(MouseInput.mouseXY) && pauseButtons.get(i).type == 6) Game.cleanUp();
		}
	}

	public synchronized static void cleanUp() {
		pauseButtons.clear();
	}
}
