extends BaseInventory

var view:Spatial
var preview:Spatial


func _ready():
	view = get_node("view")
	preview = get_node("preview")
	var hud_track = get_node("../hud/inventory/track")
	hud_track.get_texture().set_viewport_path_in_scene(str(get_path()) + "/viewport")
	connect("item_changed",self,"_on_item_changed")
	connect("item_changed",preview,"_on_inventory_item_changed")


func _physics_process(delta):
	if is_network_master():
		if player != null:
			if not player.is_out && player.is_in_group("first"):
				get_node("../hud/inventory").show()
				if item == null:
					set_item()
				if preview.item:
					preview.item.show()
				if Input.is_action_just_pressed("controls_add_road"):
					route.rpc("add_road", roads[type][index], player.get_index())
				if Input.is_action_just_pressed("controls_prev_road_type"):
					type -= 1
					if type < 0:
						type = roads.size() -1
					index = 0
					set_item()
				if Input.is_action_just_pressed("controls_next_road_type"):
					type += 1
					if type > roads.size() - 1:
						type = 0
					index = 0
					set_item()
				if Input.is_action_just_pressed("controls_prev_road_variant"):
					index -= 1
					if index < 0:
						index = roads[type].size() -1
					set_item()
				if Input.is_action_just_pressed("controls_next_road_variant"):
					index += 1
					if index > roads[type].size() - 1:
						index = 0
					set_item()
			else:
				get_node("../hud/inventory").hide()
				if preview.item:
					preview.item.hide()


func _on_item_changed(road_identifier, player):
	if is_network_master():
		if view.get_child_count() > 0:
			view.remove_child(view.get_child(0))
			
		view.add_child(item)
		
		get_node("../hud/inventory/type").set_text(tr(road_identifier))