extends Control

@onready var day_night: EnvironmentManagerRoot = get_node_or_null("../DayNight") as EnvironmentManagerRoot
@onready var day_time_label: Label = $DayTimeLabel
@onready var day_time_slider: HSlider = $DayTimeSlider


func _ready() -> void:
	UIEvents.day_time_changed.connect(_on_day_time_changed)
	_sync_day_time_ui()

func _on_rain_toggled(toggled_on: bool) -> void:
	UIEvents.toggle_rain.emit(toggled_on)

func _on_snow_toggled(toggled_on: bool) -> void:
	UIEvents.toggle_snow.emit(toggled_on)

func _on_wind_toggled(toggled_on: bool) -> void:
	UIEvents.toggle_wind.emit(toggled_on)

func _on_fireflies_toggled(toggled_on: bool) -> void:
	UIEvents.toggle_fireflies.emit(toggled_on)

func _on_storm_toggled(toggled_on: bool) -> void:
	UIEvents.toggle_storm.emit(toggled_on)

func _on_day_time_slider_value_changed(value: float) -> void:
	UIEvents.set_day_time.emit(value)

func _on_day_time_changed(value: float, time_string: String) -> void:
	day_time_slider.set_value_no_signal(value)
	var label_text: String = "Time %s" % time_string
	if (
		day_night != null
		and day_night.environment_config != null
		and day_night.environment_config.show_day_time_debug
	):
		label_text = "Time %s | nt %.3f | dt %.3f" % [time_string, value, day_night.day_time]
	day_time_label.text = label_text

func _on_fog_overlay_toggled(toggled_on: bool) -> void:
	UIEvents.toggle_fog_overlay.emit(toggled_on)

func _on_dust_toggled(toggled_on: bool) -> void:
	UIEvents.toggle_dust.emit(toggled_on)

func _on_blur_toggled(toggled_on: bool) -> void:
	UIEvents.toggle_blur.emit(toggled_on)

func _on_pause_toggled(toggled_on: bool) -> void:
	UIEvents.toggle_pause_daytime.emit(toggled_on)

func _on_shadows_toggled(toggled_on: bool) -> void:
	UIEvents.toggle_shadows.emit(toggled_on)

func _sync_day_time_ui() -> void:
	if day_night == null:
		return

	var day_night_controller: DayNightController = (
		day_night.get_node_or_null("DayNightController") as DayNightController
	)
	if day_night_controller == null:
		call_deferred("_sync_day_time_ui")
		return

	_on_day_time_changed(
		day_night_controller.current_time,
		day_night_controller.get_time_string()
	)
