extends MarginContainer

var alarm:Alarm
var playing := false

onready var time_talker = $"../time talker"
onready var weather_api_node = $"../WeatherAPINode"
onready var snooze_timer = $"snooze timer"
onready var snooze_button = $VBoxContainer/HBoxContainer/snooze
onready var sleep_mode = $"../sleep_mode"

var _day
var _weather

func popup(alarm:Alarm):
	if !alarm.active:
		return
	self.alarm = alarm
	
	sleep_mode.prevent_sleep = true
	snooze_button.visible = alarm.snooze
	snooze_timer.stop()
	time_talker.stop()
	
	_weather = Constants.WEATHER.UNKNOWN
	var json = yield(weather_api_node.get_forecast(),"completed")
	var temperature = json['current']['temp_c']
	if json is Dictionary:
		_weather = Constants.code_to_weather(json['current']['condition']['code'])
	_day = Time.get_date_dict_from_system()['weekday']
	show()
	_play()


func _play():
	playing = true
	if !time_talker.is_playing():
		time_talker.play(alarm.moment, _day, _weather, alarm.weather_music if alarm.override_weather_music else null, alarm.play_music, alarm.speak_time)

func _on_snooze_timer_timeout():
	if alarm!=null and !alarm.active:
		_on_stop_pressed()
		return
	show()
	sleep_mode.prevent_sleep = true
	if alarm != null:
		_play()

func _on_stop_pressed():
	sleep_mode.prevent_sleep = false
	playing = false
	alarm = null
	time_talker.stop(1.5)
	snooze_timer.stop()
	self.hide()

func _on_snooze_pressed():
	sleep_mode.prevent_sleep = false
	print("Ya snooze...")
	playing = false
	time_talker.stop(2.5)
	snooze_timer.start(5*60)
	self.hide()
#	snooze_timer.start(5)


func _on_time_talker_finished():
	if playing and alarm != null:
		_play()
