package engine;

import java.awt.Graphics2D;
import java.awt.Rectangle;

import _texture.loader.Textures;
import engine.Game.STATE;

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

public class MenuButton extends Rectangle {
	private static final long serialVersionUID = 1337;

	public float x, y;
	public int width, height;

	public int type;
	public volatile boolean isHovering, isVisible, isAtTop, otherContextMenuIsOpen;

	// main buttons
	public MenuButton(float x, float y, int width, int height, int type, boolean isHovering, boolean isVisible) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.type = type;
		this.isHovering = isHovering;
		this.isVisible = isVisible;
		setBounds((int) x, (int) y, width, height);
	}

	// settings buttons
	public MenuButton(float x, float y, int width, int height, int type, boolean isHovering, boolean isVisible, boolean isAtTop,
			boolean otherContextMenuIsOpen) {
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.type = type;
		this.isHovering = isHovering;
		this.isVisible = isVisible;
		this.isAtTop = isAtTop;
		this.otherContextMenuIsOpen = otherContextMenuIsOpen;
		setBounds((int) x, (int) y, width, height);
	}

	public void tick() {
		setBounds((int) x, (int) y, width, (int) (height * 0.9f));
		calcIsHovering();
	}

	public void calcIsHovering() {
		if (Game.state == STATE.MENU) {
			// Menu
			for (int i = 0; i < (Menu.menuButtons.size()); i++) {
				try {
					if (Menu.menuButtons.get(i).contains(MouseInput.mouseXY)) {
						Menu.menuButtons.get(i).isHovering = true;
					} else Menu.menuButtons.get(i).isHovering = false;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		else if (Game.state == STATE.PAUSED) {
			// Paused
			for (int i = 0; i < (PauseMenu.pauseButtons.size()); i++) {
				try {
					if (PauseMenu.pauseButtons.get(i).contains(MouseInput.mouseXY)) {
						PauseMenu.pauseButtons.get(i).isHovering = true;
					} else PauseMenu.pauseButtons.get(i).isHovering = false;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}

		else if (Game.state == STATE.SETTINGS) {
			// settings
			for (int i = 0; i < (Game.settings.mainButtons.size()); i++) {
				try {
					if (Game.settings.mainButtons.get(i).contains(MouseInput.mouseXY)) {
						Game.settings.mainButtons.get(i).isHovering = true;
					} else Game.settings.mainButtons.get(i).isHovering = false;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			for (int i = 0; i < (Game.settings.resolutionContextMenu.size()); i++) {
				try {
					if (Game.settings.resolutionContextMenu.get(i).contains(MouseInput.mouseXY)) {
						Game.settings.resolutionContextMenu.get(i).isHovering = true;
					} else Game.settings.resolutionContextMenu.get(i).isHovering = false;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			for (int i = 0; i < Game.settings.resizeableContextMenu.size(); i++) {
				try {
					if (Game.settings.resizeableContextMenu.get(i).contains(MouseInput.mouseXY)) {
						Game.settings.resizeableContextMenu.get(i).isHovering = true;
					} else Game.settings.resizeableContextMenu.get(i).isHovering = false;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			for (int i = 0; i < Game.settings.displayFPSContextMenu.size(); i++) {
				try {
					if (Game.settings.displayFPSContextMenu.get(i).contains(MouseInput.mouseXY)) {
						Game.settings.displayFPSContextMenu.get(i).isHovering = true;
					} else Game.settings.displayFPSContextMenu.get(i).isHovering = false;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			for (

			int i = 0; i < Game.settings.showEnemyHealthContextMenu.size(); i++) {
				try {
					if (Game.settings.showEnemyHealthContextMenu.get(i).contains(MouseInput.mouseXY)) {
						Game.settings.showEnemyHealthContextMenu.get(i).isHovering = true;
					} else Game.settings.showEnemyHealthContextMenu.get(i).isHovering = false;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			for (int i = 0; i < (Game.settings.fpsContextMenu.size()); i++) {
				try {
					if (Game.settings.fpsContextMenu.get(i).contains(MouseInput.mouseXY)) {
						Game.settings.fpsContextMenu.get(i).isHovering = true;
					} else Game.settings.fpsContextMenu.get(i).isHovering = false;
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void render(Graphics2D g2d) {
		try{
			if ((this == PauseMenu.pauseButtons.get(2))) {
				if (!isHovering && isVisible) g2d.drawImage(Textures.menuButtonSheet[8], (int) x, (int) y, null);
				else if (isVisible) g2d.drawImage(Textures.menuButtonSheet[9], (int) x, (int) y, null);
				return;
			}
		} catch (Exception e){ /* because pause menu isn't always init'd */ }
		
		if (!isHovering && isVisible) g2d.drawImage(Textures.menuButtonSheet[type], (int) x, (int) y, null);
		else if (isVisible) g2d.drawImage(Textures.menuButtonSheet[type + 1], (int) x, (int) y, null);
	}

	public synchronized void setIsVisible(boolean isVisible) {
		this.isVisible = isVisible;
	}
}