extends Control

@onready
var sync_panel: Panel = %SyncPanel

@onready
var sync_title_label: Label = %SyncTitleLabel

@onready
var no_changes_label: Label = %NoChangesLabel

@onready
var progress_hbox: HBoxContainer = %ProgressHBoxContainer

@onready
var sync_hbox: HBoxContainer = %SyncHBoxContainer

@onready
var sync_progress_bar: ProgressBar = %SyncProgressBar

@onready
var games_added_vbox: VBoxContainer = %GamesAddedVBox

@onready
var games_added_separator: VSeparator = %GamesAddedSeparator

@onready
var games_readded_vbox: VBoxContainer = %GamesReAddedVBox

@onready
var games_readded_separator: VSeparator = %GamesReAddedSeparator

@onready
var games_changed_title_vbox: VBoxContainer = %GamesChangedTitleVBox

@onready
var games_changed_title_separator: VSeparator = %GamesChangedTitleSeparator

@onready
var games_changed_content_vbox: VBoxContainer = %GamesChangedContentVBox

@onready
var games_changed_content_separator: VSeparator = %GamesChangedContentSeparator

@onready
var games_removed_vbox: VBoxContainer = %GamesRemovedVBox

@onready
var games_removed_separator: VSeparator = %GamesRemovedSeparator

@onready
var catched_errors_vbox: VBoxContainer = %CatchedErrorsVBox

@onready
var sync_time: Label = %SyncTime

@onready
var sync_timer: Timer = %SyncTimer

var changes: bool = false

signal sync_started
signal sync_finished

func _ready() -> void:
	sync_timer.timeout.connect(_on_sync_timeout)

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), sync_panel.size).has_point(evLocal.position):
				self.visible = false

func start_sync() -> void:
	print("start_sync")
	self.visible = true
	var sync_request_sent: Callable = func sync_request_sent() -> void:
		sync_timer.start(0.75)
		Settings.currently_syncing = true
		sync_started.emit()
		show_sync_progress()
	if !Settings.currently_syncing:
		Settings.make_request2("/sync", sync_request_sent, false)

func _on_sync_timeout() -> void:
	print("_on_sync_timeout!")
	var get_progress: Callable = func get_progress(data_received: Dictionary) -> void:
		print("get_progress")
		print("data_received: ", data_received)
		if data_received != null:
			if data_received.has("progress"):
				sync_progress_bar.value = int(data_received.progress)
				sync_time.text = data_received.time_spent
			else:
				on_sync_finished(data_received)
	Settings.make_request2("/sync/progress", get_progress, true)

func on_sync_finished(data_received: Dictionary) -> void:
	reset_sync_result()
	if data_received.games_added != null:
		changes = true
		list_visible(games_added_vbox, games_added_separator)
		make_list(data_received.games_added, games_added_vbox, "GamesAdded")
	if data_received.games_re_added != null:
		changes = true
		list_visible(games_readded_vbox, games_readded_separator)
		make_list(data_received.games_re_added, games_readded_vbox, "GamesReAdded")
	if data_received.games_changed_title != null:
		changes = true
		list_visible(games_changed_title_vbox, games_changed_title_separator)
		Settings.delete_children(games_changed_title_vbox)
		make_header_label(games_changed_title_vbox, "GamesChangedTitle")
		var row: int = 0
		for old_game_title: String in data_received.games_changed_title:
			var label: Label = make_game_label(old_game_title + " changed title to " + data_received.games_changed_title[old_game_title])
			if row % 2 != 0:
				label.add_theme_color_override("font_color", Color(1, 0.5, 0))
			games_changed_title_vbox.add_child(label)
	if data_received.games_changed_content != null:
		changes = true
		list_visible(games_changed_content_vbox, games_changed_content_separator)
		make_list(data_received.games_changed_content, games_changed_content_vbox, "GamesChangedContent")
	if data_received.games_removed != null:
		changes = true
		list_visible(games_removed_vbox, games_removed_separator)
		make_list(data_received.games_removed, games_removed_vbox, "GamesRemoved")
	if data_received.catched_errors != null:
		catched_errors_vbox.visible = true
		make_list(data_received.catched_errors, catched_errors_vbox, "CatchedErrors")
	if !changes:
		no_changes_label.visible = true
	sync_time.text = data_received.total_time
	Settings.currently_syncing = false
	sync_timer.stop()
	print("sync finished")
	show_sync_result()
	sync_finished.emit()

func list_visible(vbox: VBoxContainer, seperator: VSeparator) -> void:
	vbox.visible = true
	seperator.visible = true

func make_header_label(vbox: VBoxContainer, text: String) -> void:
	var label: Label = Label.new()
	label.set_texture_filter(TextureFilter.TEXTURE_FILTER_NEAREST)
	label.add_theme_font_size_override("font_size", 20)
	label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
	label.text = text
	vbox.add_child(label)

func make_game_label(text: String) -> Label:
	var label: Label = Label.new()
	label.set_texture_filter(TextureFilter.TEXTURE_FILTER_NEAREST)
	label.add_theme_font_size_override("font_size", 20)
	label.text = text
	label.autowrap_mode = TextServer.AUTOWRAP_WORD
	return label

func reset_sync_result() -> void:
	sync_progress_bar.value = 0
	sync_time.text = ""
	changes = false
	no_changes_label.visible = false
	games_added_vbox.visible = false
	games_added_separator.visible = false
	games_readded_vbox.visible = false
	games_readded_separator.visible = false
	games_changed_title_vbox.visible = false
	games_changed_title_separator.visible = false
	games_changed_content_vbox.visible = false
	games_changed_content_separator.visible = false
	games_removed_vbox.visible = false
	games_removed_separator.visible = false
	catched_errors_vbox.visible = false

func make_list(list: Array, vbox: VBoxContainer, text: String) -> void:
	Settings.delete_children(vbox)
	make_header_label(vbox, text)
	var row: int = 0
	for game: String in list:
		var label: Label = make_game_label(game)
		if row % 2 != 0:
			label.add_theme_color_override("font_color", Color(1, 0.5, 0))
		vbox.add_child(label)

func show_sync_result() -> void:
	sync_title_label.text = "Sync Result"
	progress_hbox.visible = false
	sync_hbox.visible = true
	
func show_sync_progress() -> void:
	sync_title_label.text = "Sync in Progress"
	progress_hbox.visible = true
	no_changes_label.visible = false
	sync_hbox.visible = false
	catched_errors_vbox.visible = false
