extends Node

#var Palette: Dictionary = {
#	"yellow":	Color("061f1b"),
#	"green":	Color("303a63"),
#	"red":		Color("8a4b3b"),
#	"blue":		Color("529c66"),
#	"black":	Color("d4f5b0")
#}

const sound = preload("res://Sounds/Sound.tscn")
const SOUNDS: Dictionary = {
	"cheese_grab": preload("res://Sounds/cheese_grab.ogg"),
	"drawer_close": preload("res://Sounds/drawer_close.ogg"),
	"drawer_open": preload("res://Sounds/drawer_open.ogg"),
	"door_open": preload("res://Sounds/door_open.ogg"),
	"door_open2": preload("res://Sounds/door_open2.ogg"),
	"inventory_open": preload("res://Sounds/inventory_open.ogg"),
	"inventory_close": preload("res://Sounds/inventory_close.ogg"),
	"shovel_grab": preload("res://Sounds/shovel_grab.ogg"),
	"creak": preload("res://Sounds/creak.ogg"),
	#"stairs": preload("res://Sounds/stairs.ogg"),
	"bird_sing": preload("res://Sounds/bird_sing.ogg"),
	"item_grab": preload("res://Sounds/item_grab.ogg"),
	"sit": preload("res://Sounds/sit.ogg"),
	"door_closed": preload("res://Sounds/door_closed.ogg"),
	"bird_fly_away": preload("res://Sounds/bird_fly_away.ogg"),
	"shovel_dig": preload("res://Sounds/shovel_dig.ogg"),
	"button_click": preload("res://Sounds/button_click.ogg"),
	"key_grab": preload("res://Sounds/key_grab.ogg"),
	"cartridge_grab": preload("res://Sounds/cartridge_grab.ogg"),
	"crystal_grab": preload("res://Sounds/crystal_grab.ogg"),
	"winder_wind": preload("res://Sounds/winder_wind.ogg"),
	"blip": preload("res://Sounds/blip.ogg"),
}
const MUSIC: Dictionary = {
	"main": "res://Music/main.ogg",
	"finale": "res://Music/finale.ogg",
	"outdoors": "res://Music/outdoors.ogg",
	"small_house": "res://Music/small_house.ogg",
}
var current_music = "main"

onready var game = get_tree().root.get_node("Game")
onready var inventory = game.get_node("CanvasLayer/Inventory")
onready var transition_player = game.get_node("CanvasLayer/TransitionPlayer")
onready var cursor_sprite = game.get_node("CanvasLayer/Cursor/Sprite")
onready var music: AudioStreamPlayer = game.get_node("Music")
var inventory_input: bool = true
var use_item: bool = false setget set_use_item

var can_quit: bool = false

var player: Node2D
var game_vars: Dictionary
#var cursor_sprite = load("res://Other/cursor.png")

func _ready():
	pass
	#Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
	#Input.set_custom_mouse_cursor(cursor_sprite)

func _input(event):
	if !event is InputEventMouseButton:
		return
	if !event.pressed:
		return
	if event.button_index == BUTTON_RIGHT:
		if inventory_input && !inventory.animation_player.is_playing():
			if use_item:
				set_use_item(false)
				return
			if inventory.visible:
				inventory.animation_player.play("Hide")
				play_sound("inventory_close")
			else:
				inventory.animation_player.play("Show")
				play_sound("inventory_open")
			return
		
		if can_quit:
			return_to_room()
			inventory_input = true
			can_quit = false

func set_use_item(value: bool):
	use_item = value
	cursor_sprite.frame = int(use_item)
	print(value)

func load_room_scene(s: String) -> void:
	if s == "":
		return
	var scene: PackedScene = load(s)
	if scene == null:
		return
	print(scene)
	var inst = scene.instance()
	game.get_node("RoomInside").call_deferred("add_child", inst)
	
	player.hide()
	game.get_node("Room").hide()
	inventory_input = false
	if inventory.visible:
		inventory.animation_player.play("Hide")
	set_use_item(false)

func return_to_room() -> void:
	var room_inside = game.get_node("RoomInside")
	var c = room_inside.get_child(0)
	c.queue_free()
	
	player.show()
	game.get_node("Room").show()

func change_room(s: String) -> void:
	var scene: PackedScene = load(s)
	if scene == null:
		return
	
	var room = game.get_node("Room")
	var c = room.get_child(0)
	c.queue_free()
	
	var inst = scene.instance()
	room.add_child(inst)
	#room.call_deferred("add_child", inst)

func move_player(pos: Vector2, obj) -> void:
	if player.is_inactive:
		return
	player.obj_action = obj
	player.move_to_point(pos)

func play_sound(snd: String):
	if !SOUNDS.has(snd):
		print("No such sound (", snd, ")")
		return
	var inst = sound.instance()
	inst.stream = SOUNDS[snd]
	inst.play()
	game.add_child(inst)

func change_music(msc: String):
	if current_music == msc:
		return
	current_music = msc
	
	if msc == "":
		music.playing = false
		return
	else:
		music.playing = true
	
	var new_music = load(MUSIC[msc])
	if !MUSIC.has(msc):
		print("No such music (", msc, ")")
		return
	print(new_music)
	music.stream = new_music
	music.play()
	print(msc)
	
