class_name ShiftCycle extends Control

@onready var sounds : Dictionary = {
	"delete":preload("res://sounds/Various Sounds 06.wav"),
	"lift":preload("res://sounds/Turning Books Pages 52.wav"),
	"drop":preload("res://sounds/Paper_Scraping_1.wav")
}
@onready var panel : PanelContainer = %PanelContainer
@onready var working_edit : TextEdit = %WorkingEdit
@onready var open_edit : TextEdit = %OpenEdit
@onready var shadow : Panel = %Shadow
@onready var audio_player : AudioStreamPlayer = $AudioStreamPlayer

var last_working_text : String = ""
var last_open_text : String = ""


var work_shift : int = 25
var open_shift : int = 5
var regex : RegEx
var tween : Tween
var dragging : bool = false

func _init() -> void:
	regex = RegEx.new()
	regex.compile("^\\d{1,3}\\z")


func _input(event: InputEvent) -> void:
	if !dragging:
		return
	if event is not InputEventMouseMotion:
		return

func lift() -> void:
	if tween and tween.is_running():
		tween.stop()
	tween = create_tween()
	tween.tween_property(shadow, "position:y", 10.0, .25 * (10 - shadow.position.y) / 10.0)
	tween.parallel().tween_property(shadow, "self_modulate:a", 1.0, .25 * (1.0 - shadow.self_modulate.a))
	tween.parallel().tween_property(panel, "position:y", -10.0, .25 * (panel.position.y + 10.0) / 10.0)
	audio_player.stream = sounds.lift
	audio_player.play()

func drop() -> void:
	if tween and tween.is_running():
		tween.stop()
	tween = create_tween()
	tween.tween_property(shadow, "position:y", 0.0, .25 * shadow.position.y / 10.0)
	tween.parallel().tween_property(shadow, "self_modulate:a", 0.0, .25 * shadow.self_modulate.a)
	tween.parallel().tween_property(panel, "position:y", 0.0, .25 * panel.position.y / -10.0)
	audio_player.stream = sounds.drop
	audio_player.play()

func _on_working_edit_text_changed() -> void:
	if !regex.search(working_edit.text) and working_edit.text != "":
		working_edit.text = last_working_text
		working_edit.set_caret_column(len(last_working_text))
	else:
		last_working_text = working_edit.text


func _on_working_edit_text_set() -> void:
	if !regex.search(working_edit.text) and working_edit.text != "":
		working_edit.text = last_working_text
		working_edit.set_caret_column(len(last_working_text))
	else:
		last_working_text = working_edit.text


func _on_open_edit_text_changed() -> void:
	if !regex.search(open_edit.text) and open_edit.text != "":
		open_edit.text = last_open_text
		open_edit.set_caret_column(len(last_open_text))
	else:
		last_open_text = open_edit.text


func _on_open_edit_text_set() -> void:
	if !regex.search(open_edit.text) and open_edit.text != "":
		open_edit.text = last_open_text
		open_edit.set_caret_column(len(last_open_text))
	else:
		last_open_text = open_edit.text
