tool
extends ConfirmationDialog

var templates_options = null
var ratio_box = null
var width_value = null
var height_value = null
var fill_color_node = null

onready var size_value = Vector2()

var DrawGD : Node = null

# Template Id identifier
enum Templates {
	TDefault = 0,
	T16 = 1,
	T32 = 2,
	T64 = 3,
	T128 = 4,
	GB = 5,
	GBA = 6,
	NES_NTSC = 7,
	NES_PAL = 8,
	SNES_NTSC = 9,
	SNES_PAL = 10
}
# Template actual value, without Default because we get it from DrawGD
var TResolutions = {
	Templates.T16: Vector2(16,16),
	Templates.T32: Vector2(32,32),
	Templates.T64: Vector2(64,64),
	Templates.T128: Vector2(128,128),

	Templates.GB: Vector2(160,144),
	Templates.GBA: Vector2(240,160),
	Templates.NES_NTSC: Vector2(256,224),
	Templates.NES_PAL: Vector2(256,240),
	Templates.SNES_NTSC: Vector2(512,448),
	Templates.SNES_PAL: Vector2(512,480),
}

var TStrings ={
	Templates.T16: "",
	Templates.T32: "",
	Templates.T64: "",
	Templates.T128: "",

	Templates.GB: "GB",
	Templates.GBA: "GBA",
	Templates.NES_NTSC: "NES (NTSC)",
	Templates.NES_PAL: "NES (PAL)",
	Templates.SNES_NTSC: "SNES (NTSC)",
	Templates.SNES_PAL: "SNES (PAL)"
	}


func _enter_tree() -> void:
	var n : Node = get_parent()
	while n:
		if n.name == "DrawGDSingleton":
			DrawGD = n
			break
		n = n.get_parent()
		
	if !n:
		return
		
	templates_options = get_node("VBoxContainer/OptionsContainer/TemplatesOptions")
	ratio_box = get_node("VBoxContainer/OptionsContainer/RatioCheckBox")
	width_value = get_node("VBoxContainer/OptionsContainer/WidthValue")
	height_value = get_node("VBoxContainer/OptionsContainer/HeightValue")
	fill_color_node = get_node("VBoxContainer/OptionsContainer/FillColor")
		
	width_value.value = DrawGD.default_image_width
	height_value.value = DrawGD.default_image_height
	fill_color_node.color = DrawGD.default_fill_color
	fill_color_node.get_picker().presets_visible = false

	ratio_box.connect("pressed", self, "_on_RatioCheckBox_toggled", [ratio_box.pressed])
	templates_options.connect("item_selected", self, "_on_TemplatesOptions_item_selected")

	_CreateOptionList()


func _CreateOptionList() -> void:
	for i in Templates.values():
		if i > 0:
			if TStrings[i] != "":
				templates_options.add_item("{width}x{height} - {name}".format({"width":TResolutions[i].x, "height":TResolutions[i].y, "name":TStrings[i]}), i)
			else:
				templates_options.add_item("{width}x{height}".format({"width":TResolutions[i].x, "height":TResolutions[i].y}), i)


func _on_CreateNewImage_confirmed() -> void:
	var width : int = width_value.value
	var height : int = height_value.value
	var fill_color : Color = fill_color_node.color
	DrawGD.canvas.fill_color = fill_color

	var frame : Frame = DrawGD.canvas.new_empty_frame(false, true, Vector2(width, height))
	var new_project := Project.new(DrawGD, [frame], tr("untitled"), Vector2(width, height).floor())
	new_project.layers.append(Layer.new())
	DrawGD.projects.append(new_project)
	DrawGD.tabs.current_tab = DrawGD.tabs.get_tab_count() - 1
	DrawGD.canvas.camera_zoom()


var aspect_ratio: float

func _on_RatioCheckBox_toggled(_button_pressed: bool) -> void:
	aspect_ratio = width_value.value / height_value.value
	for spin_box in [width_value, height_value]:
		if spin_box.is_connected("value_changed", self, "_on_SizeValue_value_changed"):
			spin_box.disconnect("value_changed", self, "_on_SizeValue_value_changed")
		else:
			spin_box.connect("value_changed", self, "_on_SizeValue_value_changed")


func _on_SizeValue_value_changed(value: float) -> void:
	if width_value.value == value:
		height_value.value = width_value.value / aspect_ratio
	if height_value.value == value:
		width_value.value = height_value.value * aspect_ratio


func _on_TemplatesOptions_item_selected(id: int) -> void:
	if id != Templates.TDefault:
		size_value = TResolutions[id]
	else:
		width_value.value = DrawGD.default_image_width
		height_value.value = DrawGD.default_image_height

	width_value.value = size_value.x
	height_value.value = size_value.y
