tool
extends Resource
class_name SummerDayDocument


signal resolution_changed(new_resolution)
signal track_added(track)
signal track_deleted(track) # Not used yet. Implement please.


export(Vector2) var resolution = Vector2(1920, 1080) setget _set_resolution
export(Array, Resource) var tracks

var raw_aspect_ratio setget , _get_raw_aspect_ratio

var selected_track


func setup():
	for track in tracks:
		selected_track = track
		emit_signal("track_added", track)
	update_interaction_hint()


func add_track(blueprint: SummerDayBlueprint):
	var track = SummerDayTrack.new()
	
	track.blueprint = blueprint
	
	var table := SummerDayTable.new()
	table.allocation_size = blueprint.get_allocation_size()
	table.create()
	track.table = table
	
	track.add_cell(0)
	tracks.append(track)
	selected_track = track
	emit_signal("track_added", track)


func create_scene() -> SummerDayScene:
	var _scene = SummerDayScene.new()
	_scene.resource_path = "res://production/test/scene_001.tres"
	_scene.resolution = Vector2(12.0, 9.0)
	return _scene


func update_interaction_hint():
	if selected_track:
		selected_track.update_interaction_hint()


func _set_resolution(new_resolution):
	var handled_resolution = Vector2()
	
	# Don't let the components drop below 0.01!
	if new_resolution.x <= 0.01:
		handled_resolution.x = 0.01
	else:
		handled_resolution.x = new_resolution.x
	
	if new_resolution.y <= 0.01:
		handled_resolution.y = 0.01
	else:
		handled_resolution.y = new_resolution.y
	
	resolution = handled_resolution
	
	emit_signal("resolution_changed", resolution)


func _get_raw_aspect_ratio():
	return resolution.x / resolution.y
