extends KinematicBody2D

var velocity: Vector2
var siding_left = false
var anim = ""
var selector = -1
var in_dialogue = false
var _atack = false
var speed = 100

var experience = 0 
var health = 100
var strength = 10

var armed = false
var idle_anim = "unarmed_idle"
var walk_anim = "unarmed_walk"

func _process(delta):
	
	var move_right = Input.is_action_pressed("ui_right")
	var move_left = Input.is_action_pressed("ui_left")
	var move_up = Input.is_action_pressed("ui_up")
	var move_down = Input.is_action_pressed("ui_down")
	var up = Input.is_action_just_pressed("ui_up")
	var down = Input.is_action_just_pressed("ui_down")
	var enter = Input.is_action_just_pressed("action")
	
	var new_anim = idle_anim
	var new_siding_left = siding_left
	velocity = Vector2()
	
	if enter:
		var overlapped = $interract.get_overlapping_areas()
		if overlapped.size() != 0:
			var area = overlapped[0]
			var object = area.get_parent()
			var type = object.type
			if  type == "actor":
				var dialogue_id = object.dialogue
				if dialogue_id >= 0:
					if !in_dialogue:
						in_dialogue = true
						Interaction.start_dialogue(dialogue_id, object)
					else:
						if Interaction.ready:
							Interaction.dialogue_handler()
						else:
							Global.gui.ui_force_text_anim()
			elif type == "mob" and armed and !_atack:
				atack(object)
			elif type == "item":
				pick_up_items([object._name])
				object.on_pick_up()
	if move_right:
		velocity.x = speed * delta
		new_anim = walk_anim
		new_siding_left = false 
	elif move_left:
		velocity.x = -speed * delta
		new_anim = walk_anim
		new_siding_left = true 
	if move_up:
		if in_dialogue:
			if up:
				Interaction.change_selector(-1)
		else:
			velocity.y = -speed/1.4 * delta
			new_anim = walk_anim
	elif move_down:
		if in_dialogue:
			if down:
				Interaction.change_selector(1)
		else:
			velocity.y = speed/1.4 * delta
			new_anim = walk_anim
	
	if _atack:
		velocity = Vector2()
		new_anim = "atack"
	
	move_and_slide(velocity * 9000 * delta)
	
	if new_anim != anim:
		$AnimationPlayer.play(new_anim)
		anim = new_anim
		
	if new_siding_left != siding_left:
		if (new_siding_left):
			$body.scale.x = -1
		else:
			$body.scale.x = 1
		siding_left = new_siding_left
		
func atack(target):
	_atack = true
	$weapon_time.start()
	var result = target.apply_damage(strength)
	if result:
		Interaction.check_objective("kill", target._name, -1)
		if strength < 100:
			strength += 1
			Global.gui.ui_update_strength(strength)
	Audio.play_sound("kick", false)

func _on_interract_area_entered(area):
	var type = area.get_parent().type
	if type == "actor":
		Global.gui.ui_show_message("нажмите действие, чтобы поговорить", Color("ffffff"))
	elif type == "mob" and armed:
		Global.gui.ui_show_message("нажмите действие, чтобы атаковать", Color("9e1515"))
	elif type == "item":
		Global.gui.ui_show_message("нажмите действие, чтобы подобрать", Color("ffffff"))

func _on_interract_area_exited(area):
	if in_dialogue:
		in_dialogue = false
		Interaction.cancel_dialogue()
	Global.gui.ui_show_message("", Color())

func _on_weapon_time_timeout():
	_atack = false
	
func pick_up_items(item_names):
	for i in item_names:
		Interaction.add_to_inventory(i)
		Audio.play_sound("pickup", false)
		
func use_item(item):
	var meta = item["meta"]
	for m in meta:
		if m == "weapon":
			get_weapon(item)
			return "keep"
		if m == "potion":
			get_intoxication()
			return "use_up"
	return ""
		
func get_weapon(weapon):
	var _texture = load("res://sprites/items/" + weapon["alias"] + ".png")
	$body/weapon.texture = _texture
	$body/weapon.visible = true
	idle_anim = "armed_idle"
	walk_anim = "armed_walk"
	armed = true
	Audio.play_sound("arm", false)
	
func get_intoxication():
	var intoxication_time = 20
	Global.game.get_node("effects/mirrage").visible = true
	speed /= 2
	Audio.play_sound("drink", false)
	yield(get_tree().create_timer(intoxication_time),"timeout")
	Global.game.get_node("effects/mirrage").visible = false
	speed *= 2
	
func add_exp(value):
	experience += value
	Global.gui.ui_update_exp(experience)
	
func apply_damage(damage):
	health -= damage
	Global.gui.ui_update_health(health)
	Global.gui.ui_fade_in()
	set_process(false)
	global_position = get_parent().get_node("respawn").global_position
	yield(get_tree().create_timer(2),"timeout")
	set_process(true)
	
func _ready():
	Global.player = self
	Global.gui.ui_update_health(health)
	Global.gui.ui_update_exp(experience)
	Global.gui.ui_update_strength(strength)
