@tool
extends HBoxContainer

@export var show_celsius: bool = true:
	set = set_show_celsius
@export var show_fahrenheit: bool = true:
	set = set_show_fahrenheit
@export var degrees_celsius: float = 0.0:
	set = set_degrees_celsius,
	get = get_degrees_celsius
@export var degrees_fahrenheit: float = 0.0:
	set = set_degrees_fahrenheit,
	get = get_degrees_fahrenheit
@export var temperature_unknown: bool = true:
	set = set_temperature_unknown

var _c: float = 0.0
var _f: float = 0.0

func _ready():
	_check_slash()
	_update_text()

func set_temperature_unknown(new: bool):
	temperature_unknown = new
	_update_text()

func set_show_celsius(new: bool):
	show_celsius = new
	if has_node("celsius"):
		$celsius.visible = new
		$degrees.visible = new
	_check_slash()

func set_show_fahrenheit(new: bool):
	show_fahrenheit = new
	if has_node("fahrenheit"):
		$fahrenheit.visible = new
		$degrees2.visible = new
	_check_slash()

func set_degrees_celsius(new: float):
	_c = new
	_f = Constants.celsius_to_fahrenheit(new)
	_update_text()

func get_degrees_celsius() -> float:
	return _c

func set_degrees_fahrenheit(new: float):
	_f = new
	_c = Constants.fahrenheit_to_celsius(new)
	_update_text()

func get_degrees_fahrenheit() -> float:
	return _f

func _update_text():
	if !has_node("celsius"):
		return
	if temperature_unknown:
		$celsius.text = '?'
		$fahrenheit.text = '?'
	else:
		$celsius.text = str(snapped(_c, 1))
		$fahrenheit.text = str(snapped(_f, 1))

func _check_slash():
	if !has_node("slash"):
		return
	$slash.visible = show_celsius and show_fahrenheit
