extends Control

@onready
var search_list: VBoxContainer = $ScrollContainer/VBoxContainer

@onready
var search_panel: Panel = %SearchPanel

@onready
var clear_button: Button = $ClearButton

@onready
var close_button: Button = $CloseButton

@onready
var search_bar: TextEdit = $Searchbar

var games: Array = []
var regex: RegEx

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	clear_button.pressed.connect(clear)
	close_button.pressed.connect(close)
	search_bar.grab_focus()
	search_bar.text_changed.connect(search)
	visibility_changed.connect(focus)
	regex = RegEx.new()
	
func focus() -> void:
	if self.visible == true:
		search_bar.grab_focus()
		clear()

func close() -> void:
	clear()
	self.visible = false

func search() -> void:
	print(search_bar.text)
	Settings.delete_children(search_list)
	var search_text: String = search_bar.text.to_lower()
	for game: String in games:
		if is_match_exact(search_text, game):
			add_game(game)
	var clean_search_text: String = clean_search_term(search_text)
	for game: String in games:
		if is_match_contains(clean_search_text, clean_game(game)):
			add_game(game)
	for game: String in games:
		if is_match_regex(clean_search_text, clean_game(game)):
			add_game(game)

func clean_search_term(term: String) -> String:
	return term.replace(" ", "").replace("é", "e").replace("+", "plus").replace("&", "and").replace("'n", "and")

func clean_game(term: String) -> String:
	return term.replace(" ", "").replace("é", "e").replace("+", "plus").replace("&", "and").replace("'n", "and").to_lower()

func is_match_exact(search_term: String, game_name: String) -> bool:
	game_name = game_name.to_lower()
	
	if search_term == "":
		return true
	elif game_name.contains(search_term):
		return true
	else:
		return false

func is_match_contains(search_term: String, game_name: String) -> bool:
	if search_term == "":
		return true
	elif game_name.contains(search_term):
		return true
	else:
		return false	
			
func is_match_regex(search_term: String, game_name: String) -> bool:
	if search_term == "":
		return true
	elif compile_regex(search_term).search(game_name):
		return true
	else:
		return false
		
func add_game(game: String) -> void:
	var label: Label = Label.new()
	label.text = game
	print("game: " + game)
	label.autowrap_mode = TextServer.AUTOWRAP_WORD
	if !check_if_game_exists(game):
		search_list.add_child(label)

func check_if_game_exists(game: String) -> bool:
	var game_exists: bool = false
	for child: Label in search_list.get_children():
		if child.text == game:
			game_exists = true
	return game_exists

func compile_regex(search_term: String) -> RegEx:
	var regText: String = ".*"
	for letter: String in search_term:
		regText += letter + ".*"
	regex.compile(regText)
	return regex

func get_list_of_games() -> void:
	print("get_list_of_games")
	var handle_games: Callable = func handle_games(array: Array) -> void:
		if typeof(array) == TYPE_ARRAY:
			games = []
			Settings.delete_children(search_list)
			games.append_array(array)
			for game: String in games:
				var label: Label = Label.new()
				label.text = game
				label.autowrap_mode = TextServer.AUTOWRAP_WORD
				search_list.add_child(label)
		else:
			print("Unexpected data")
	Settings.make_request2("/music/all/order", handle_games, true)

func _input(event: InputEvent) -> void:
	if self.visible == true:
		if event is InputEventMouseButton && event.is_pressed():
			var evLocal: InputEvent = make_input_local(event)
			if !Rect2(Vector2(0, 0), search_panel.size).has_point(evLocal.position):
				self.visible = false

func clear() -> void:
	search_bar.text = ""
	search()
	search_bar.grab_focus()
	
func old_search() -> void:
	print(search_bar.text)
	Settings.delete_children(search_list)
	for game: String in games:
		if old_is_match_exact(search_bar.text, game):
			add_game(game)
	for game: String in games:
		if is_match_contains(clean_term(search_bar.text), clean_term(game)):
			add_game(game)
	for game: String in games:
		if old_is_match_regex(clean_term(search_bar.text), clean_term(game)):
			add_game(game)

func clean_term(term: String) -> String:
	return term.replace(" ", "").replace("é", "e").replace("+", "plus").replace("&", "and").replace("'n", "and").to_lower()


func old_is_match_exact(search_term: String, game_name: String) -> bool:
	search_term = search_term.to_lower()
	game_name = game_name.to_lower()
	
	if search_term == "":
		return true
	elif game_name.contains(search_term):
		return true
	else:
		return false

func old_is_match_regex(search_term: String, game_name: String) -> bool:
	if search_term == "":
		return true
	elif compile_regex(search_term).search(game_name):
		return true
	else:
		return false

func old_compile_regex(search_term: String) -> RegEx:
	var regex = RegEx.new()
	var regText: String = ".*"
	for letter in search_term:
		regText += letter + ".*"
	regex.compile(regText)
	return regex
