extends Node3D

enum revolver_state {UP, DOWN, LOADING}
var state: int = revolver_state.DOWN
var game_state: int
var bullet_target_pos = Vector3(1.95,0.03,4.612) #2.231
@export var up_pos: Vector3 = Vector3(0.354,4.248,0.425)
@export var down_pos: Vector3
var loaded_bullets = Array()
var max_bullets: int = 6
var current_round: int = 0
var timer: Timer

var target_angle: Vector3 = Vector3(0,0,90)
@export var target_angle_up: Vector3 = Vector3(0,0,90)
@export var target_angle_down: Vector3 = Vector3(0,0,90)
@export var is_player_revolver: bool = false
@export var cylinder: Node3D

var animationPlayer: AnimationPlayer

var is_cylinder_out: bool = false

var curr_load_bullet: Node3D
func _ready() -> void:
	animationPlayer = $AnimationPlayer
	game_state = GameManager.current_state
	GameManager.state_changed.connect(on_state_changed)
	loaded_bullets.resize(max_bullets)
	timer = $Timer
	if is_player_revolver:
		GameManager.set_player_revolver(self)
	else:
		GameManager.set_enemy_revolver(self)

func _process(delta: float) -> void:
	if is_cylinder_out:
		pass
		#cylinder.position = lerp(cylinder.position, Vector3(1.947,0.543,3.979), delta/ 0.1)
	else:
		pass
		#cylinder.position = lerp(cylinder.position, Vector3(0,0.543,3.979), delta/ 0.1)

	cylinder.rotation_degrees = lerp(cylinder.rotation_degrees,Vector3(30 + current_round*60,90,90), delta / 0.1 )
	
	rotation_degrees = lerp(rotation_degrees, target_angle, delta / 0.1)
	
	if state == revolver_state.UP:
		global_position = lerp(global_position, up_pos, delta/ 0.1)
		target_angle = target_angle_up
	elif state == revolver_state.DOWN:
		global_position = lerp(global_position, down_pos, delta / 0.1)
		target_angle = target_angle_down
	#cylinder.rotation_degrees = Vector3(30 + current_round*60,90,90)


func load_bullet(bullet_object: Node3D):
	curr_load_bullet = bullet_object
	is_cylinder_out = true
	animationPlayer.play("cylinder_out")
	timer.timeout.connect(on_load_anim_end)
	#cylinder.rotation.x = 90.0
	
func on_load_anim_end():
	is_cylinder_out = false
	animationPlayer.play_backwards("cylinder_out")
	timer.timeout.disconnect(on_load_anim_end)
	
func on_bullet_reached():
	current_round += 1
	timer.start()
func shoot():
	loaded_bullets[current_round].shoot(null)
	pass


func can_be_loaded():
	var free: int = 0
	for bullet in loaded_bullets:
		if bullet == null:
			free += 1
	if free > 0:
		return true
	else:
		return false
			
			
func on_state_changed():
	if is_player_revolver:
		if GameManager.current_state == GameManager.GameState.PLAYER1_GUN_LOAD:
			state = revolver_state.UP
		if GameManager.current_state == GameManager.GameState.PLAYER2_GUN_LOAD:
			state = revolver_state.DOWN
	else:
		if GameManager.current_state == GameManager.GameState.PLAYER2_GUN_LOAD:
			state = revolver_state.UP
		if GameManager.current_state == GameManager.GameState.PLAYER1_GUN_LOAD:
			state = revolver_state.DOWN


func _on_animation_player_animation_finished(anim_name: StringName) -> void:
	if anim_name == "cylinder_out" and is_cylinder_out:
		loaded_bullets[current_round] = curr_load_bullet
		curr_load_bullet.is_loaded = true
		curr_load_bullet.destination_reached.connect(on_bullet_reached)
		curr_load_bullet.moveTo(to_global(bullet_target_pos)) 
		curr_load_bullet.rotate_x(deg_to_rad(-90))
		curr_load_bullet.reparent(cylinder)
	elif anim_name == "cylinder_out" and is_cylinder_out == false:
		if !can_be_loaded():
			if is_player_revolver:
				GameManager.is_player1_loaded = true
			else:
				GameManager.is_player2_loaded = true
	
		if is_player_revolver:
			GameManager.end_player_turn()
		else:
			GameManager.end_enemy_turn()
