extends Node3D

var revolver: Node3D

func _ready() -> void:
	revolver = GameManager.get_enemy_revolver()
	GameManager.state_changed.connect(on_state_changed)
func on_state_changed():
	if GameManager.current_state == GameManager.GameState.PLAYER2_GUN_LOAD:
		make_turn()
	elif  GameManager.current_state == GameManager.GameState.PLAYER2_UNLOAD:
		unload()
func unload():
	print("Unload")
	var choices = GameManager.PlayerChoice.values()
	var choice = choices.pick_random()
	GameManager.player_phase2_choice = choice
	print(GameManager.player_phase2_choice)
	if GameManager.PlayerChoice.SHOOT_SELF == choice:
		print("I will shoot myself")
		revolver.rotation_degrees = Vector3(180,180,0)
	else:
		print("I will shoot opponent")
		revolver.rotation_degrees = Vector3(180,0,0)
	
	revolver.current_round = 0
	for i in range(0, 6, 1):
		revolver.shoot()
		revolver.current_round = i
func make_turn():
	if revolver == null:
		revolver = GameManager.get_enemy_revolver()
	var bullets = GameManager.bullets
	var avaible_bullets = Array()
	for bullet in bullets:
		if bullet.is_loaded == false:
			avaible_bullets.append(bullet)
	var randNumb = randi_range(0, avaible_bullets.size() -1)
	revolver.load_bullet(avaible_bullets[randNumb])
	
