extends VBoxContainer

var alarm_res:Alarm 
onready var alarm_node = $"/root/main/alarm"

signal edit()

func _ready():
	Config.connect("changed", self ,"_update")
	self.modulate.a = 0
	var tween = get_tree().create_tween()
	tween.set_trans(Tween.TRANS_CUBIC)
	tween.tween_property(self, "modulate:a", 1.0, 0.5)
	tween.play()
	
	print(alarm_res)
	_update()


func _update():
	$"%hour".text = alarm_res.moment.padded_hour()
	$"%minute".text = alarm_res.moment.padded_minute()
	$"%active".pressed = alarm_res.active


func _on_active_toggled(button_pressed):
	if self.modulate.a != 1:
		$"%active".set_pressed_no_signal(!button_pressed)
		return
	alarm_res.active = button_pressed

func minute_timer_update(timedict):
	if alarm_res.active and timedict['hour'] == alarm_res.moment.hour and timedict['minute'] == alarm_res.moment.minute:
		alarm_node.popup(alarm_res)


func _on_delete_button_pressed():
	if self.modulate.a != 1:
		return
	var tween = get_tree().create_tween()
	tween.set_trans(Tween.TRANS_CUBIC)
	tween.tween_property(self, "modulate:a", 0.0, 0.5)
	tween.play()
	yield(tween, "finished")
	self.queue_free()
	if alarm_res:
		Config.flags.remove_alarm(alarm_res)


func _on_edit_button_pressed():
	if self.modulate.a != 1:
		return
	emit_signal("edit")
