extends VehicleBody
export var parked: bool = false

# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
var front_radius = 0.36
var back_radius = 0.34
var wheel_travel = 0.08
var wheel_stifness = 40.0
var rest_length = 0.08
#var wheel_max_force = 24000
var wheel_compression_damping = 0.88
var wheel_relaxation_damping = 0.99
var marker_fl_seat
var marker_fr_seat

var marker_info = {
	"marker_front_left_door": {
		"method": "open_door",
		"door": "lf",
		"show": ["marker_front_left_vehicleseat"]
	},
	"marker_front_right_door": {
		"method": "open_door",
		"door": "rf",
		"show": ["marker_front_right_vehicleseat"]
	},
	"marker_trunk_door": {
		"method": "open_door",
		"door": "trunk"
	},
	"marker_front_left_vehicleseat": {
		"method": "sit_player_into_vehicle",
		"seat": "lf",
		"see_through": true,
		"show": ["marker_exit"],
		"hide": ["marker_trunk_door", "marker_front_left_vehicleseat"]
	},
	"marker_front_right_vehicleseat": {
		"method": "sit_player_into_vehicle",
		"seat": "rf",
		"see_through": true,
		"show": ["marker_exit"],
		"hide": ["marker_trunk_door", "marker_front_right_vehicleseat"]
	},
	"marker_exit": {
		"method": "get_player_out_of_vehicle",
		"seat": "rf",
		"see_through": true,
		"show": ["marker_trunk_door"]
	}
}
func _init():
	add_to_group("vehicles")
func _ready():
	mass = 1800
	brake = 1000
	engine_force = 0
	for e in get_children():
		if e is VehicleWheel:
			if e.name.begins_with("front") || e.name.ends_with("front"):
				e.wheel_radius = front_radius
				e.use_as_steering = true
				e.use_as_traction = false
			elif e.name.begins_with("back") || e.name.ends_with("back"):
				e.wheel_radius = back_radius
				e.use_as_steering = false
				e.use_as_traction = true
			e.suspension_travel = wheel_travel
			e.suspension_stiffness = wheel_stifness
			e.suspension_max_force = 32.0 * mass / 4.0
			e.damping_compression = wheel_compression_damping
			e.damping_relaxation = wheel_relaxation_damping
			e.wheel_rest_length = rest_length
			e.wheel_friction_slip = 1.5
	if parked:
		mode = RigidBody.MODE_KINEMATIC
	doors.register_door(self, "lf", $"car_base/front_left_rot", Vector3.UP, 0, -PI/2.0, 2.0)
	doors.register_door(self, "rf", $"car_base/front_right_rot", Vector3.UP, 0, PI/2.0, 2.0)
	doors.register_door(self, "trunk", $"car_base/trunk_door_rot", Vector3.RIGHT, 0, PI/2.0, 2.0)
	markers.init_markers(self, marker_info)
#	var marker_lf = $"car_base/front_left_rot/left-front-door/marker_front_left_door"
#	var marker_data_lf = {}
#	marker_data_lf.obj = self
#	marker_data_lf.method = "open_door"
#	marker_data_lf.door = "lf"
#	marker_lf.set_meta("marker_data", marker_data_lf)
#	var marker_rf = $"car_base/front_right_rot/right-front-door/marker_front_right_door"
#	var marker_data_rf = {}
#	marker_data_rf.obj = self
#	marker_data_rf.method = "open_door"
#	marker_data_rf.door = "rf"
#	marker_rf.set_meta("marker_data", marker_data_rf)
#	var marker_trunk = $"car_base/trunk_door_rot/trunk_door/marker_trunk_door"
#	var marker_data_trunk = {}
#	marker_data_trunk.obj = self
#	marker_data_trunk.method = "open_door"
#	marker_data_trunk.door = "trunk"
#	marker_trunk.set_meta("marker_data", marker_data_trunk)
#
#	marker_fl_seat = $"car_base/seat-frame/seats/marker_front_left_vehicleseat"
#	var marker_data_fl_seat = {}
#	marker_data_fl_seat.obj = self
#	marker_data_fl_seat.method = "sit_player_into_vehicle"
#	marker_data_fl_seat.seat = "lf"
#	marker_data_fl_seat.see_through = true
#	marker_fl_seat.set_meta("marker_data", marker_data_fl_seat)
#	marker_fl_seat.hide()
#
#	marker_fr_seat = $"car_base/seat-frame/seats/marker_front_right_vehicleseat"
#	var marker_data_fr_seat = {}
#	marker_data_fr_seat.obj = self
#	marker_data_fr_seat.method = "sit_player_into_vehicle"
#	marker_data_fr_seat.seat = "rf"
#	marker_data_fr_seat.see_through = true
#	marker_fr_seat.set_meta("marker_data", marker_data_fr_seat)
#	marker_fr_seat.hide()
##	for e in [$marker_exit1_exit, $marker_exit2_exit]:
#		var md = {}
#		md.obj = self
#		md.method = "player_leave_vehicle"
#		md.see_through = true
#		e.set_meta("marker_data", md)
#		e.hide()

func show_markers(data):
	if data.has("show"):
		for e in data.show:
			if marker_info[e].has("marker_obj"):
				marker_info[e].marker_obj.show()
	if data.has("hide"):
		for e in data.hide:
			if marker_info[e].has("marker_obj"):
				marker_info[e].marker_obj.hide()
func hide_markers(data):
	if data.has("show"):
		print("show", data.show)
		for e in data.show:
			if marker_info[e].has("marker_obj"):
				marker_info[e].marker_obj.hide()
	if data.has("hide"):
		print("hide ", data.hide)
		for e in data.hide:
			if marker_info[e].has("marker_obj"):
				marker_info[e].marker_obj.show()


func open_door(data):
	var door = data.door
	if doors.get_state(self, door) == 0:
		print("CLOSED, openning")
		show_markers(data)
		doors.open_door(self, door)
#		if door == "lf":
#			marker_fl_seat.show()
#		elif door == "rf":
#			marker_fr_seat.show()
	elif doors.get_state(self, door) == 1:
		print("OPEN, closing")
		hide_markers(data)
		doors.close_door(self, door)
#		if door == "lf":
#			marker_fl_seat.hide()
#		elif door == "rf":
#			marker_fr_seat.hide()
func sit_to_vehicle(ch: KinematicBody, seat:Spatial, drive: bool = true):
#	ch.add_collision_exception_with(self)
#	add_collision_exception_with(ch)
	var cmdqueue
	if drive:
		cmdqueue = [["vehicle_mode_on"], ["jumpto", seat.global_transform], ["parent_to_vehicle"], ["anim_state", "drive"]]
	else:
		cmdqueue = [["vehicle_mode_on"], ["jumpto", seat.global_transform], ["parent_to_vehicle"], ["anim_state", "passenger"]]
	ch.set_meta("vehicle", self)
	ch.set_meta("cmdqueue", cmdqueue)
	ch.set_meta("seat", seat)
	if drive:
		set_meta("driver", ch)
	var ch_parent = ch.get_parent()
	ch.set_meta("orig_parent", ch_parent)
func leave_vehicle(ch):
	ch.remove_meta("orig_parent")
	var cmdqueue = [["anim_state", "locomotion"], ["vehicle_mode_off"]]
	ch.set_meta("cmdqueue", cmdqueue)
	

func sit_player_into_vehicle(data):
	print("SIT: ", data.seat)
	show_markers(data)
	var ch: KinematicBody = get_viewport().get_camera().get_meta("player")
	var seat: Spatial
	var drive = false
	match data.seat:
		"lf":
			drive = true
			seat = $"car_base/seat-frame/seats/marker_front_left_vehicleseat"
		"rf":
			seat = $"car_base/seat-frame/seats/marker_front_right_vehicleseat"
#	for e in [$marker_exit1_exit, $marker_exit2_exit]:
#		e.show()
	sit_to_vehicle(ch, seat, drive)
func get_player_out_of_vehicle(data):
	print("EXIT: ", data.seat)
	hide_markers(data)
	var ch: KinematicBody = get_viewport().get_camera().get_meta("player")
	leave_vehicle(ch)



# Called every frame. 'delta' is the elapsed time since the previous frame.
#var d = 1.0
#var state = 0
func _process(delta):
	pass
#	if parked:
#		mode = RigidBody.MODE_KINEMATIC
#		return
#	if engine_force > 1000.0 && linear_velocity.length() < 0.1:
#		apply_central_impulse(global_transform.basis.y * mass * 1 + global_transform.basis.z * mass * 5)
#	if d > 0:
#		engine_force = 3000.0
#		brake = 0
#		d -= delta
#	else:
#		state = (state + 1) % 4
#		d = 1.0
#		match state:
#			0:
#				engine_force = 6000
#				steering = 0
#			1:
#				steering = -0.4
#				engine_force = 6000
#			2:
#				engine_force = 6000
#				steering = 0
#			3:
#				engine_force = 6000
#				steering = -0.4
func is_upright():
	var v = global_transform.basis.y
	if v.y > v.x && v.y > v.z:
		return true
	return false
#func _physics_process(delta):
#	if is_upright():
#		if abs(engine_force) > 0 && linear_velocity.y * delta > 0.05:
#			add_central_force(Vector3(0, -linear_velocity.y * delta * mass, 0))
#		var l = linear_velocity.length() * delta
#		add_central_force(Vector3(0, -l * 0.5 * mass, 0))
func set_lod(lod: int):
	pass
