extends TimeShift

# changed everything to work with float vars instead of the timer to work with the current pausing functionality
@export var timeshift_timer:float = 5.0;
var _timeshift_timer : float = 0.0
var _saved_timeshift_timer : float = 0.0

var timeshift_allowed := true

# set up DisEL same as parent class, and then set the maximum timeshift length
# could be adaptable e.g. increasing through progression
#   => move the timers to the PlayerVariables autoload script?
func _ready() -> void:
	super._ready()
	SignalBus.set_timeshift_countdown_max(timeshift_timer)
	SignalBus.set_anchor.connect(set_anchor)
	SignalBus.reload_anchor.connect(reload_anchor)

# counts down timer and continuously updates the value
# this does mean that a signal is sent every frame which kinda sucks, 
# but idk if that can be changed unless we want to fix the timer to the progress bar
func _process(delta: float) -> void:
	if Globals.paused or SceneChanger.mid_change:
		return
	if _timeshift_timer >= 0:
		_timeshift_timer -= delta
		SignalBus.change_timeshift_countdown_absolute(_timeshift_timer)
		if _timeshift_timer <= 0:
			timer_run_out()
	

# move to extension script
func _unhandled_input(event: InputEvent) -> void:
	if not event.is_action_pressed("time_shift"):
		return;
	if Globals.paused or SceneChanger.mid_change or SceneChanger.mid_animation:
		return;
	_check_timeshift_allowed()
	if timeshift_allowed:
		trigger();
	else:
		AudioManager.create_audio(SoundEffect.SOUND_EFFECT_TYPE.TIME_SHIFT_NOT_POSSIBLE)

# trigger, timer_run_out, and timer_run_out functions copied over from the parent script,
# which now only has simplified functionality
# these functions having unnecessary params is just so that it can match the parent signature
func trigger(_time : Utils.TIME = Globals.current_time) -> void:
	if Globals.current_time == Utils.TIME.PRESENT:
		 # Go from present to past
		_timeshift_timer = timeshift_timer
		time_shift(Globals.current_time);
	elif Globals.current_time == Utils.TIME.PAST:
		time_shift(Globals.current_time); # Go from past to present

func timer_run_out() -> void:
	if SceneChanger.mid_change:
		return;
	if Globals.current_time == Utils.TIME.PRESENT:
		return;
	time_shift(Globals.current_time);

func time_shift(_time : Utils.TIME = Globals.current_time):
	AudioManager.create_audio(SoundEffect.SOUND_EFFECT_TYPE.TIME_SHIFT)
	match Globals.current_time:
		Utils.TIME.PRESENT: 
			Globals.current_time = Utils.TIME.PAST
		Utils.TIME.PAST:
			Globals.current_time = Utils.TIME.PRESENT
	SceneChanger.change_time()	
	await SignalBus.changed_time
	if Globals.current_time == Utils.TIME.PRESENT:
		SignalBus.change_timeshift_countdown_absolute(-1)
	super.time_shift()
	SignalBus.change_player_position()

#region validity check
func _check_timeshift_allowed():
	timeshift_allowed = timeshift_check.is_position_valid
#endregion

#region Anchored Remnant
func set_anchor():
	_saved_timeshift_timer = _timeshift_timer

func reload_anchor():
	_timeshift_timer = _saved_timeshift_timer
	SignalBus.change_timeshift_countdown_absolute(_timeshift_timer)
