extends Node

var is_gui = false

var rot_cam = Vector3()
var mouse_sens = 0.3
var camera_anglev = 0

var fps_mode = false
var tmp_fps_mode = false
var focus_cam: Camera
var cam_focused: bool = false

var menu: Node
func display_menu():
	menu.popup()
	

func _ready():
	focus_cam = Camera.new()
	add_child(focus_cam)
	focus_cam.set_as_toplevel(true)
	var escape_menu = preload("res://ui/save_game.tscn")
	menu = escape_menu.instance()
	add_child(menu)

func is_fps_mode():
	return fps_mode || tmp_fps_mode

func _input(event):
	if !is_gui:
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	if event is InputEventMouseMotion && !is_gui:
		rot_cam.y = deg2rad(-event.relative.x * mouse_sens)
		var changev = -event.relative.y * mouse_sens
		var player
		if get_viewport().get_camera().has_meta("player"):
			player = get_viewport().get_camera().get_meta("player")
		if !player:
			return
		var cam = player.get_meta("cam")
		var fps_cam = player.get_meta("fps_cam")
		if cam.current:
			if camera_anglev + changev > -30 and camera_anglev + changev < 40:
				camera_anglev += changev
				rot_cam.x = deg2rad(changev)
		elif fps_cam.current:
			if camera_anglev + changev > -60 and camera_anglev + changev < 60:
				camera_anglev += changev
				rot_cam.x = deg2rad(changev)
func focus_camera(obj):
	if !obj:
		print("NOTHING TO FOCUS")
		return
	if cam_focused:
		return
	print("FOCUS")
	var cur_cam = get_viewport().get_camera()
	var player = cur_cam.get_meta("player")
	var cam = player.get_meta("cam")
	focus_cam.global_transform = cam.global_transform
	var do = obj.global_transform.origin
	focus_cam.look_at(do, Vector3.UP)
	cam_focused = true
	focus_cam.set_meta("camera", cur_cam)
	focus_cam.set_current(true)
	focus_cam.set_meta("player", player)
func unfocus_camera():
	cam_focused = false
	var ncam = focus_cam.get_meta("camera")
	ncam.set_current(true)
	
func fix_cam(state, _delta):
	var player = get_viewport().get_camera().get_meta("player")
	if !is_fps_mode():
		var cam = player.get_meta("cam")
		var cam_rot_y = cam.get_meta("cam_rot_y")
		var cam_rot_x = cam.get_meta("cam_rot_x")
		cam_rot_y.rotate_y(rot_cam.y)
		cam_rot_x.rotate_x(rot_cam.x)
	else:
		if !player.has_meta("vehicle") && !player.has_meta("cmdqueue"):
			var fps_cam = player.get_meta("fps_cam")
			var fps_cam_rot_x = fps_cam.get_meta("fps_cam_rot_x")
			var o: Transform = player.get_meta("orientation")
			o.basis = o.basis.rotated(Vector3(0, 1, 0), rot_cam.y)
			fps_cam_rot_x.rotate_x(rot_cam.x)
			player.set_meta("orientation", o)
		
	rot_cam.y = 0
	rot_cam.x = 0
func get_player():
	var c = get_viewport().get_camera()
	assert(c)
	if !c:
		return null
	assert(c.has_meta("player"))
	if !c.has_meta("player"):
		return null
	var player = c.get_meta("player")
	assert(player)
	if !player:
		return null
	assert(player)
	return player
	
func switch_fps_mode(value):
	var player = get_player()
	print(player)
	if !player:
		return
	assert(player != null)
	var cam = player.get_meta("cam")
	var fps_cam = player.get_meta("fps_cam")
	var cam_rot_y = cam.get_meta("cam_rot_y")
	if !value:
		cam.current = true
		player.show_face()
		#need to convert to globals
		cam_rot_y.rotation.y = player.rotation.y
	else:
		fps_cam.current = true
		player.hide_face()

var tmp_fps_cooldown = 0.0


func _process(delta):
	var c = get_viewport().get_camera()
	if !c:
		return

	if !c.has_meta("player"):
		return

	var player = get_player()
	if !player:
		return
	if !is_gui && Input.is_action_just_pressed("ui_cancel"):
		is_gui = true
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
		display_menu()
	if !is_gui && Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED:
		Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
	elif is_gui && Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
		Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
	var cam = player.get_meta("cam")
	var fps_cam = player.get_meta("fps_cam")
	var cam_pos = cam.get_meta("cam_pos")
	var cam_arm = cam.get_meta("cam_arm")
	cam_pos.global_transform.origin = player.global_transform.origin
	cam.look_at(player.global_transform.origin + Vector3(0, 1.9, 0), Vector3.UP)

	if Input.is_action_just_pressed("fps_mode") && !tmp_fps_mode:
		fps_mode = !fps_mode
		switch_fps_mode(fps_mode)
	if !fps_mode && !tmp_fps_mode && !player.has_meta("vehicle"):
		var s = cam_arm.global_transform.origin
		var t = cam.global_transform.origin
		if tmp_fps_cooldown <= 0.0:
			if s.distance_to(t) < 0.3:
				tmp_fps_mode = true
				switch_fps_mode(tmp_fps_mode)
				tmp_fps_cooldown = 1.0
	if !fps_mode && tmp_fps_mode:
		var s = cam_arm.global_transform.origin
		var t = cam.global_transform.origin
		if tmp_fps_cooldown <= 0.0:
			if s.distance_to(t) > 0.4:
				tmp_fps_mode = false
				switch_fps_mode(tmp_fps_mode)
				tmp_fps_cooldown = 3.0
	if tmp_fps_cooldown > 0.0:
		tmp_fps_cooldown -= delta

var spawn_delay = 0.5
func _physics_process(delta):
	if spawn_delay > 0.5:
		spawn_delay -= delta
		return
	var c = get_viewport().get_camera()
	if !c:
		return
	if !c.has_meta("player"):
		return
	var player = c.get_meta("player")
	if !player:
		return
	var space = player.get_world().space
	var state = PhysicsServer.space_get_direct_state(space)
	fix_cam(state, delta)

