tool
extends EditorPlugin

signal document_changed(new_doc)

var MainScreen
var Timeline
var Document

var canvas_instance
var main_screen_instance
var timeline_instance


func _enter_tree():
	_initilize()
	
	main_screen_instance = MainScreen.instance()
	canvas_instance = main_screen_instance.get_node("Canvas")
	self.connect("document_changed",
		canvas_instance, "_on_SummerDay_document_changed")
	canvas_instance.connect("resized",
		canvas_instance, "_on_Canvas_resized")
	# Add the main panel to the editor's main viewport.
	get_editor_interface().get_editor_viewport().add_child(main_screen_instance)
	# Hide the main panel. Very much required.
	make_visible(false)
	
	
	timeline_instance = Timeline.instance()
	add_control_to_bottom_panel(timeline_instance, "SummerDay Timeline")
	
	
	add_custom_type("SummerDayDocument", "Resource",
	load("res://addons/summer_day/data/document/document.gd"),
	get_editor_interface().get_base_control().get_icon("Animation", "EditorIcons"))
	
	add_custom_type("SummerDayDisplay", "Control",
	load("res://addons/summer_day/display/display.gd"),
	get_editor_interface().get_base_control().get_icon("TextureRect", "EditorIcons"))


func _exit_tree():
	if main_screen_instance:
		main_screen_instance.queue_free()
	remove_custom_type("SummerDayDocument")
	remove_custom_type("SummerDayDisplay")
	remove_control_from_bottom_panel(timeline_instance)
#	$"/root/SummerDay".canvas_state.free()
#	$"/root/SummerDay".z_range_manager.free()


func enable_plugin():
	add_autoload_singleton(
		"SummerDay",
		"res://addons/summer_day/utilities/summer_day_globals.gd"
	)


func disable_plugin():
	remove_autoload_singleton("SummerDay")


func has_main_screen():
	return true


func make_visible(visible):
	if main_screen_instance:
		main_screen_instance.visible = visible
		
#	if visible:
#		make_bottom_panel_item_visible(timeline_instance)
#	else:
#		hide_bottom_panel()


func handles(object):
	if object is Document:
		return true


func edit(object):
	var _SummerDay = $"/root/SummerDay"
	if object != _SummerDay.document:
		_SummerDay.document = object
		emit_signal("document_changed", object)


func get_plugin_name():
	return "SummerDay"


func get_plugin_icon():
	return get_editor_interface().get_base_control().get_icon("AnimationPlayer", "EditorIcons")


func _initilize():
	add_autoload_singleton(
		"SummerDay",
		"res://addons/summer_day/utilities/summer_day_globals.gd"
	)
	
	MainScreen = load("res://addons/summer_day/main_screen.tscn")
	Timeline = load("res://addons/summer_day/user_interface/timeline/timeline.tscn")
	Document = load("res://addons/summer_day/data/document/document.gd")
