package ui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;

public class Button extends Pushable {

	protected Rectangle bounds;

	protected String text;
	protected Font font;

	protected Color bg_color;
	protected Color fnt_color;

	public Button(int x, int y, int w, int h) {
		bounds = new Rectangle(x, y, w, h);
		bg_color = Color.BLACK;
		fnt_color = Color.WHITE;
		text = "Button";
		font = new Font("System", Font.PLAIN, 16);
	}

	@Override
	public void update() {

	}

	@Override
	public void draw(Graphics2D g2) {
		Font old = g2.getFont();
		g2.setFont(font);
		if (isHovering()) {
			int s = 0;
			g2.setColor(bg_color);
			g2.fillRect(bounds.x - s / 2, bounds.y - s / 2, bounds.width + s, bounds.height + s);
			g2.setColor(fnt_color);
			g2.drawString(text, (bounds.x - s / 2) + (bounds.width + s) / 2 - g2.getFontMetrics().stringWidth(text) / 2, (bounds.y - s / 2) + (bounds.height + s) / 2 + (int) g2.getFontMetrics().getStringBounds(text, g2).getHeight() / 2);

			g2.setStroke(new BasicStroke(3));

			// left
			g2.setColor(new Color(20, 20, 20));
			g2.drawLine(bounds.x, bounds.y, bounds.x, bounds.y + bounds.height);

			// top
			g2.setColor(new Color(60, 60, 60));
			g2.drawLine(bounds.x, bounds.y, bounds.x + bounds.width, bounds.y);

			// right
			g2.setColor(new Color(255, 255, 255));
			g2.drawLine(bounds.x + bounds.width, bounds.y + bounds.height, bounds.x + bounds.width, bounds.y);

			// bottom
			g2.setColor(new Color(223, 223, 223));
			g2.drawLine(bounds.x, bounds.y + bounds.height, bounds.x + bounds.width, bounds.y + bounds.height);
			g2.setStroke(new BasicStroke(1));

		} else {
			g2.setColor(bg_color);
			g2.fillRect(bounds.x, bounds.y, bounds.width, bounds.height);
			g2.setColor(fnt_color);
			g2.drawString(text, bounds.x + bounds.width / 2 - g2.getFontMetrics().stringWidth(text) / 2, bounds.y + bounds.height / 2 + (int) (g2.getFontMetrics().getStringBounds(text, g2).getHeight() / 4));
			g2.setColor(bg_color.darker());

			g2.setStroke(new BasicStroke(3));
			// left
			g2.setColor(Color.WHITE);
			g2.drawLine(bounds.x, bounds.y, bounds.x, bounds.y + bounds.height);

			// top
			g2.setColor(new Color(223, 223, 223));
			g2.drawLine(bounds.x, bounds.y, bounds.x + bounds.width, bounds.y);

			// right
			g2.setColor(new Color(20, 20, 20));
			g2.drawLine(bounds.x + bounds.width, bounds.y + bounds.height, bounds.x + bounds.width, bounds.y);

			// bottom
			g2.setColor(new Color(60, 60, 60));
			g2.drawLine(bounds.x, bounds.y + bounds.height, bounds.x + bounds.width, bounds.y + bounds.height);

			g2.setStroke(new BasicStroke(1));
		}
		
		g2.setFont(old);
	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public Rectangle getBounds() {
		return bounds;
	}

	public Button setFnt_color(Color fnt_color) {
		this.fnt_color = fnt_color;
		return this;
	}

	public Button setBg_color(Color bg_color) {
		this.bg_color = bg_color;
		return this;
	}

	public Button setText(String text) {
		this.text = text;
		return this;
	}

	public Button setFont(Font font) {
		this.font = font;
		return this;
	}
}
