## InteractiveObject — base class for all interactive room objects (flowers, equipment, etc.).
class_name InteractiveObject extends Node2D

signal object_interacted(object: InteractiveObject)

enum State { IDLE, ACTIVE, RETURNING }

@export var object_id: String = ""

var _current_state: State = State.IDLE


func _ready() -> void:
	var drag: DragDropComponent = get_node_or_null("DragDropComponent") as DragDropComponent
	if drag != null:
		drag.drag_picked_up.connect(_on_drag_picked_up)
		drag.drag_released.connect(_on_drag_released)
	var area: Area2D = get_node_or_null("CollisionArea") as Area2D
	if area != null:
		area.input_event.connect(_on_area_input_event)


func _on_drag_picked_up(_pos: Vector2) -> void:
	_set_state(State.ACTIVE)
	object_interacted.emit(self)


func _on_drag_released(_pos: Vector2) -> void:
	_set_state(State.RETURNING)
	GameState.set_object_state(object_id, "idle")
	_play_bounce_animation()


func _on_area_input_event(_viewport: Node, event: InputEvent, _shape_idx: int) -> void:
	if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
		_trigger_interaction()
	elif event is InputEventScreenTouch and event.pressed:
		_trigger_interaction()


func _trigger_interaction() -> void:
	_set_state(State.ACTIVE)
	object_interacted.emit(self)
	GameState.set_object_state(object_id, "active")
	_play_bounce_animation()


func _set_state(new_state: State) -> void:
	_current_state = new_state


func _play_bounce_animation() -> void:
	var tween: Tween = create_tween()
	tween.tween_property(self, "scale", Vector2(1.2, 1.2), 0.1)
	tween.tween_property(self, "scale", Vector2(0.9, 0.9), 0.1)
	tween.tween_property(self, "scale", Vector2(1.0, 1.0), 0.1)
	tween.tween_callback(func() -> void: _set_state(State.IDLE))
