extends Control

const QueryObject = preload("queries.gd")



var ItemListContent = []
#			'nomo':
#			'priskribo':
#			'uuid':
#			'koordinatoX':
#			'koordinatoY':
#			'koordinatoZ':
#			distance
#_to = $"../ship".translation.distance_to(Vector3(koordX,koordY,koordZ))


func _on_Close_button_pressed():
	$"canvas/MarginContainer".set_visible(false)
	
	
#func _input(event):
#	if Input.is_action_just_pressed("right_click"):
#		if event is InputEventMouseButton and $"canvas/MarginContainer".visible:
#			var x = $".".get_global_mouse_position().x
#			var y = $".".get_global_mouse_position().y
#			if($"canvas/MarginContainer/VBoxContainer/ItemList".margin_left<x) and (x<$"canvas/MarginContainer/VBoxContainer/ItemList".margin_right):
#				$"canvas/MarginContainer/VBoxContainer/ItemList".select(1,true)
#			print('margin_left=',$"canvas/MarginContainer/VBoxContainer/ItemList".margin_left,' x=',x,' margin_right=',$"canvas/MarginContainer/VBoxContainer/ItemList".margin_right)


func _resize(event: InputEvent) -> void:
	if event is InputEventMouseMotion and Input.is_mouse_button_pressed(BUTTON_LEFT):
		$"canvas/MarginContainer".rect_size += event.relative


func _drag(event: InputEvent) -> void:
	if event is InputEventMouseMotion and Input.is_mouse_button_pressed(BUTTON_LEFT):
		$"canvas/MarginContainer".rect_position += event.relative
	

func FillItemList():
	if $canvas/MarginContainer/VBoxContainer/Label/check.pressed:
		var ls = ItemListContent
		ItemListContent=[]
		for Item in ls:
			if Item['inCosmo']:
				ItemListContent.append(Item)
	# Заполняет список найдеными продуктами
	for Item in ItemListContent:
		get_node("canvas/MarginContainer/VBoxContainer/scroll/ItemList").add_item(Item['nomo'], null, true)


# Вызывается перед созданием окна
func _on_Objekto_draw():
	var q = QueryObject.new()

	# Делаем запрос к бэкэнду
	$HTTPObjectoRequestFind.request(q.URL, Global.backend_headers, true, 2, q.objecto_query())


func _on_reload_pressed():
	_on_Objekto_draw()
	pass # Replace with function body.


func _on_ItemList_item_selected(index):
#	print('выбран = ',index,' nomo=',ItemListContent[index]['nomo'],
#		', uuid=',ItemListContent[index]['uuid'])
	pass # Replace with function body.


func _on_check_pressed():
	_on_Objekto_draw()
	pass # Replace with function body.
