extends Node2D
#constants
## initial width of the drawn track
@export var starting_width := 50.0
## amount of pixels that the width decreases every second
@export var shrink_factor := .1
## the diefference between the track's width and the maximum distance of the car from the center of the track. Should be about half the width of the car
@export var max_distance_offset := .5
## minimum distance traversed in a single frame to be counted as cheese
@export var cheese_distance := 500
## margin for error when checking for laps being driven
@export var lap_count_margin := 50

#references
## reference to the path describing the track
@export var path: Path2D
## reference to the line that draws the visual track
@export var line: Line2D
## reference to the player node
@export var player: StaticBody2D
## reference to the line node used to draw the finish line
@export var finish: Line2D

@export var menu: Control

signal distance_changed(new_distance)

#variables
var width
var running = true
var last_offset = 0.
var lap_distance = 0.
var start
var finish_right
var finish_left
var paused = false

# Called when the node enters the scene tree for the first time.
func _ready():
	width = starting_width
	var start_out = path.curve.get_point_out(0)
	path.curve.set_point_in(path.curve.point_count - 1, path.curve.get_point_in(0))
	path.curve.set_point_out(path.curve.point_count - 1, start_out)
	line.set_width(width)
	line.set_points(path.curve.get_baked_points())
	start = path.curve.get_point_position(0)
	finish_right = start_out.normalized().rotated(deg_to_rad(90)) * width/2
	finish_left = start_out.normalized().rotated(deg_to_rad(-90)) * width/2
	finish.add_point(start + finish_right)
	finish.add_point(start + finish_left)
#	print_debug("start: ", start, "start out: ", start_out.normalized(), " finish left: ", start + start_out.normalized().rotated(90) * width/2, "start right: ", start + start_out.normalized().rotated(-90) * width/2)
	
	player.position = start
	player.rotation = start_out.angle()


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	
	if Input.is_action_just_pressed("pause") && running:
		pause()
	
	if (running):
		# shrinking the track
		width -= shrink_factor * delta
		update_line()
		
		
		# checking whether the player is still on the track
		var p_pos = player.position
		var closest = path.curve.get_closest_point(p_pos)
		var p_dist = (p_pos - closest).length()
#		print_debug(" distance from track center: ", p_dist, " current track width: ", width)
		if p_dist > width/2 - max_distance_offset:
			game_over()
		
		# counting the distance driven
		var offset = path.curve.get_closest_offset(p_pos)
		var off_diff = offset - last_offset
		if off_diff > 0:
			if off_diff > path.curve.get_baked_length() - lap_count_margin && off_diff < path.curve.get_baked_length() + lap_count_margin:
				print_debug("subtracting a lap")
				lap_distance -= path.curve.get_baked_length()
			else: if off_diff > cheese_distance:
				print_debug("cheese detected")
				game_over()
			last_offset = offset
		if off_diff < 0:
			if -off_diff < path.curve.get_baked_length() + lap_count_margin && -off_diff > path.curve.get_baked_length() - lap_count_margin:
				print_debug("adding a lap")
				lap_distance += path.curve.get_baked_length()
			last_offset = offset
		distance_changed.emit(get_total_distance())
#		print_debug("player distance driven: ", get_total_distance(), " distance since last check: ", off_diff)

func pause():
	if !paused:
		Engine.time_scale = 0
		menu.show()
		paused = true
	else:
		Engine.time_scale = 1
		menu.hide()
		paused = false
	

func update_line():
	line.set_width(width)
	finish.points[0] = start + finish_right.normalized() * width/2
	finish.points[1] = start + finish_left.normalized() * width/2

func get_total_distance():
	return lap_distance + last_offset

func game_over():
	running = false
	player.dead = true
	$player/death_sound.play()
	print_debug("final score: ", get_total_distance())
	await get_tree().create_timer(3).timeout
	
	get_tree().reload_current_scene() #temp
