class_name Player extends Node

var gameName: String = ""

# -- HEALTH --
const maxHealth: int = 3
const minHealth: int = 0
var health := maxHealth
# -- HEALTH --

# -- Item --
var itemInventory: Array[Item] = []
# -- Item --

var gun: Gun
var selectedPlayer: Player = self

var character: Character

var bot: bool = false

signal dead(player: Player)
signal hit(player: Player)
signal missed()
signal hurt()

func _init(_name: String, _gun: Gun, _character: Character) -> void:
	gameName = _name
	gun = _gun
	character = _character
	gun.setOwner(self)
	
# -- HEALTH --
func damage(amount: int) -> void:
	print("Hit")
	if health - amount < minHealth:
		kill()
		health = minHealth
	else:
		health -= amount
		for i in range(0,amount):
			character.removeShot()

func heal(amount: int) -> void:
	if health + amount > maxHealth:
		health = maxHealth
	else:
		health += amount

func getHealth () -> int:
	return health

func kill():
	dead.emit(self)
# -- HEALTH --


# -- Item --
func addItem(_item: Item) -> void:
	itemInventory.append(_item)

func removeItem(_item: Item) -> void:
	itemInventory.erase(_item)

func getItems() -> Array[Item]:
	return itemInventory
# -- Item --


# -- Gun --
func giveGun(_gun: Gun):
	gun = _gun

func takeGun():
	gun = null

func useItem(_item: Item):
	if _item is Bullet:
		loadBullet(_item)
	else:
		print("no bullet")

func loadBullet(_bullet: Bullet):
	gun.loadBullet(_bullet)
	itemInventory.erase(_bullet)

func shootAtSelectedPlayer():
	var randomBullet = gun.getRandomBullet()
	if randomBullet is StandardBullet or randomBullet is LuckyBullet:
		var bulletDamage = randomBullet.getDamage()
		if bulletDamage > 0:
			selectedPlayer.damage(bulletDamage)
		else:
			print("Missed")
	else:
		print("Empty")

# -- Gun --

func selectPlayer(_player: Player) -> void:
	selectedPlayer = _player

func getGameName() -> String:
	return gameName

func setCharacter(_character: Character) -> void:
	character = _character

func getItemAmount() -> int:
	return itemInventory.size()
