class_name PostItPanel
extends Panel

@export var minimum_size:Vector2 = Vector2(400, 100)
var attached_sticky_note: StickyNote
@onready var ancor = $"sticky-note_anchor"

func _ready():
	ancor.position = Vector2(ancor.position.x, 0)
	custom_minimum_size = Vector2(custom_minimum_size.x, 0)
	
func attatch_sticky_note(attatchment: StickyNote, tween:bool = true):
	attatchment.on_board = false
	if tween:
		var height_tween: Tween = create_tween()
		height_tween.tween_property(self, "custom_minimum_size", minimum_size, 0.3)
		height_tween.tween_property(ancor, "position", Vector2(ancor.position.x, minimum_size.y/2), 0.3)
		attatchment.tween_transform_to(ancor.global_position)
		await attatchment.transform_tween_finished
	else:
		custom_minimum_size = minimum_size
		ancor.position = Vector2(ancor.position.x, minimum_size.y/2)
	attatchment.reparent(self)
	attached_sticky_note = attatchment
	attatchment.owner = self.owner
	attatchment.attached_to = self
	attatchment.transform = ancor.transform
	
func reclaim_sticky_note():
	if is_empty() and attached_sticky_note.on_board == true:
		attached_sticky_note.on_board = false
		attached_sticky_note.tween_transform_to(ancor.global_transform)
		await attached_sticky_note.transform_tween_finished
		attached_sticky_note.reparent(self)
		attached_sticky_note.owner = self.owner
		
func clear_if_empty():
	if !is_empty(): return
	if attached_sticky_note.attached_to == self: attached_sticky_note.attached_to = null
	var height_tween: Tween = create_tween()
	height_tween.tween_property(self, "custom_minimum_size", Vector2.ZERO, 0.3)
	await height_tween.finished
	owner.on_sticky_panel_cleared()
	self.free()

func replace_sticky_note_with(new_sticky_note: StickyNote):
	if is_empty():
		attached_sticky_note = new_sticky_note

func is_empty() -> bool:
	return get_child_count() == 1
