package speiger.src.ui;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.UUID;

import speiger.src.data.CollectableType;
import speiger.src.data.Registry;

public class Marker {
	UUID id;
	CollectableType type;
	Point point;
	Image[] display;
	String name;
	boolean disabled;
	int size = 32;
	boolean hovered;
	
	public Marker(UUID id, CollectableType type, String name, Point point, Image[] display) {
		this.id = id;
		this.type = type;
		this.point = point;
		this.display = display;
		this.name = name;
		this.disabled = Registry.INSTANCE.isCompleted(id);
	}
	
	public void toggle() {
		setCompletion(!disabled);
	}
	
	public void setCompletion(boolean value) {
		disabled = value;
		if(disabled) Registry.INSTANCE.markComplete(id); 
		else Registry.INSTANCE.unmarkComplete(id);
	}
	
	public String name() { return name; }
	public UUID id() { return id; }
	public CollectableType type() { return type;}
	
	public boolean isHovered(Point mouse, float scale) {
		int rad = Math.clamp((int)(32 / scale), 5, 32)>>1;
		return mouse.x >= point.x - rad && mouse.x <= point.x + rad && mouse.y >= point.y - rad && mouse.y <= point.y + rad;
	}
	
	public boolean collides(Rectangle rect, float scale) {
		int rad = Math.clamp((int)(32 / scale), 5, 32)>>1;
		return rect.intersects(point.x-rad, point.y-rad, rad*2, rad*2);
	}
	
	public boolean isHovered() {
		return hovered;
	}
	
	public void setHovered(boolean value) {
		this.hovered = value;
	}
	
	public void updateCompletion() {
		disabled = Registry.INSTANCE.isCompleted(id);
	}
	
	public void draw(Graphics2D g, float scale) {
		int realSize = Math.clamp((int)(32 / scale), 5, 32);
		g.drawImage(display[disabled ? 1 : 0], point.x-(realSize>>1), point.y-(realSize>>1), realSize, realSize, null);
	}
	
	public void drawHover(Graphics2D g, float scale) {
		int realSize = Math.clamp((int)(32 / scale), 5, 32);
		if(hovered) {
			g.setColor(Color.BLACK);
			g.setStroke(new BasicStroke(1F / scale *2F));
			g.drawRect(point.x-(realSize>>1), point.y-(realSize>>1), realSize, realSize);
		}
	}
}
