extends TextureRect

class_name UIItemBox

signal selected(hand)
signal used_in_hand()

enum CurrentHand {
	None,
	Left,
	Right
}

var hovering := false
export var selected := false setget set_selected
export(CurrentHand) var current_hand := CurrentHand.None setget set_hand

const LEFT_Y_OFFSET := 0
const RIGHT_Y_OFFSET := 10

onready var hand_icon := $HandIcon

func _ready():
	hand_icon.texture = hand_icon.texture.duplicate()
	set_hand(current_hand)

func _mouse_hover(inside: bool) -> void:
	hovering = inside
	if hovering:
		self_modulate.a = 1
	else:
		self_modulate.a = 0.86

func set_selected(val: bool) -> void:
	selected = val
	$SelectedOverlay.visible = selected

func set_hand(hand):
	current_hand = hand
	if hand_icon != null:
		match hand:
			CurrentHand.None:
				hand_icon.visible = false
			CurrentHand.Left:
				hand_icon.visible = true
				(hand_icon.texture as AtlasTexture).region.position.y = LEFT_Y_OFFSET
			CurrentHand.Right:
				hand_icon.visible = true
				(hand_icon.texture as AtlasTexture).region.position.y = RIGHT_Y_OFFSET

func _gui_input(event):
	if event is InputEventMouseButton:
		var mouse_event := event as InputEventMouseButton
		if mouse_event.pressed:
			if mouse_event.button_index == BUTTON_LEFT:
				if current_hand != CurrentHand.Left:
					emit_signal("selected", CurrentHand.Left)
				else:
					emit_signal("used_in_hand")
			elif mouse_event.button_index == BUTTON_RIGHT:
				if current_hand != CurrentHand.Right:
					emit_signal("selected", CurrentHand.Right)
				else:
					emit_signal("used_in_hand")
