@tool
class_name ClubSelector extends Control

const UNSELECTED_SCALE := Vector2(0.618, 0.618)
const SELECTED_SCALE := Vector2(1.0, 1.0)

const LABEL_SCALE_TIME := 0.3
const HUB_ROTATION_TIME := 0.2

const CLUB_ROTATION := {
	T.Club.DRIVER: 0.0,
	T.Club.IRON: -16.0,
	T.Club.WEDGE: -32.0,
	T.Club.GOLD: -48.0,
	T.Club.PUTTER: -64.0,
}

@export var value: T.Club:
	set = _set_value

@onready var club_hub: Control = %ClubHub

@onready var driver_label: Label = %DriverLabel
@onready var iron_label: Label = %IronLabel
@onready var wedge_label: Label = %WedgeLabel
@onready var gold_label: Label = %GoldLabel
@onready var putter_label: Label = %PutterLabel


func _get_club_label(club: T.Club) -> Label:
	match club:
		T.Club.DRIVER:
			return driver_label
		T.Club.IRON:
			return iron_label
		T.Club.WEDGE:
			return wedge_label
		T.Club.GOLD:
			return gold_label
		T.Club.PUTTER:
			return putter_label
	assert(false, "Unknown club %d" % club)
	return


func _reset_label_scale() -> void:
	for c: Control in club_hub.get_children():
		c.scale = UNSELECTED_SCALE


func _set_value(new_value: T.Club) -> void:
	#_reset_label_scale()
	var old_label := _get_club_label(value)
	var new_label := _get_club_label(new_value)
	var hub_rotation: float = CLUB_ROTATION[new_value]

	var tween := get_tree().create_tween()
	tween.set_parallel()
	tween.tween_property(old_label, "scale", UNSELECTED_SCALE, LABEL_SCALE_TIME).set_trans(
		Tween.TRANS_SINE
	)
	tween.tween_property(new_label, "scale", SELECTED_SCALE, LABEL_SCALE_TIME).set_trans(
		Tween.TRANS_SINE
	)
	tween.tween_property(club_hub, "rotation_degrees", hub_rotation, HUB_ROTATION_TIME).set_trans(
		Tween.TRANS_QUAD
	)

	value = new_value
