extends Node2D

const MODE_NAME = "name"
const MODE_SELECT = "select"
const MODE_BROWSE = "browse"

const PAGE_SIZE = 10

@onready var animations = $"animations"


@onready var tabs_bar = $"widgets/tabs"
@onready var name_group = $"widgets/name"
@onready var level_name_field = $"widgets/name/level_name"
@onready var save_button = $"widgets/name/save_button"
@onready var load_button = $"widgets/name/load_button"
@onready var load_button_label = $"widgets/name/load_button/label"
@onready var cancel_button = $"widgets/cancel_button"
@onready var next_button = $"widgets/list_next"
@onready var prev_button = $"widgets/list_prev"


@onready var level_manager = $"/root/LevelManager"


@onready var level_selection_buttons = [
	$"widgets/level_list/level0",
	$"widgets/level_list/level1",
	$"widgets/level_list/level2",
	$"widgets/level_list/level3",
	$"widgets/level_list/level4",
	$"widgets/level_list/level5",
	$"widgets/level_list/level6",
	$"widgets/level_list/level7",
	$"widgets/level_list/level8",
	$"widgets/level_list/level9",
]


var bound_success_object = null
var bound_success_method = null
var bound_success_args = []

var bound_cancel_object = null
var bound_cancel_method = null
var bound_cancel_args = []

var operation_mode = "name"
var current_page = 0


func _ready():
	self.connect_buttons()
	self.set_process_input(false)


func _input(event):
	if event.is_action_pressed("ui_cancel"):
		self.execute_cancel()
	if event.is_action_pressed("ui_page_down"):
		self.switch_to_next_page()
		if self.level_selection_buttons[0].is_visible():
			self.level_selection_buttons[0].grab_focus()
	if event.is_action_pressed("ui_page_up"):
		self.switch_to_prev_page()
		if self.level_selection_buttons[0].is_visible():
			self.level_selection_buttons[0].grab_focus()

func set_name_mode():
	self.operation_mode = self.MODE_NAME
	self.name_group.show()


func clear_binds():
	self.bound_success_object = null
	self.bound_success_method = null
	self.bound_success_args = []
	self.bound_cancel_object = null
	self.bound_cancel_method = null
	self.bound_cancel_args = []

func bind_success(success_object, success_method, success_args=[]):
	self.bound_success_object = success_object
	self.bound_success_method = success_method
	self.bound_success_args = success_args

func bind_cancel(cancel_object, cancel_method, cancel_args=[]):
	self.bound_cancel_object = cancel_object
	self.bound_cancel_method = cancel_method
	self.bound_cancel_args = cancel_args


func execute_success(level_name, context=null):
	if self.bound_success_object != null:
		var args = [] + self.bound_success_args

		args.append(level_name)
		args.append(context)
		
		self.bound_success_object.call_deferred(self.bound_success_method, args)
		
		

func execute_load():
	var level_name = self.level_name_field.get_text()

	if level_name == "" || level_name == null:
		return

	if not self.level_manager.level_exists(level_name):
		return

	self.execute_success(level_name, "load")


func execute_save():
	var level_name = self.level_name_field.get_text()

	if level_name == "" || level_name == null:
		return

	self.execute_success(level_name, "save")

func execute_cancel():
	if self.bound_cancel_object != null:
		if self.bound_cancel_args.size() > 0:
			self.bound_cancel_object.call_deferred(self.bound_cancel_method, self.bound_cancel_args)
		else:
			self.bound_cancel_object.call_deferred(self.bound_cancel_method)

func show_picker():
	self.animations.play("show")
	self.set_process_input(true)


	self.refresh_current_levels_page()
	
	await self.get_tree().create_timer(0.1).timeout
	if self.level_selection_buttons[0].is_visible():
		self.level_selection_buttons[0].grab_focus()
	elif self.name_group.is_visible():
		self.save_button.grab_focus()
	else:
		self.cancel_button.grab_focus()



func hide_picker():
	self.animations.play("hide")
	self.set_process_input(false)

func set_level_name(level_name):
	self.level_name_field.set_text(level_name)



func refresh_current_levels_page():
	print ("refresh the levels page")
	var pages_count = self.level_manager.get_pages_count(self.PAGE_SIZE)

	self.manage_pagination_buttons(pages_count)
	for level_button in self.level_selection_buttons:
		level_button.hide()

	var levels_page = self.level_manager.get_levels_page(self.current_page, self.PAGE_SIZE)
	var level_details

	for index in range(levels_page.size()):
		level_details = self.level_manager.get_level_details(levels_page[index])
		self.level_selection_buttons[index].fill_data(level_details["name"])
		self.level_selection_buttons[index].show()

func manage_pagination_buttons(pages_count):
	if self.current_page == 0 || pages_count < 2:
		self.prev_button.hide()
	elif self.current_page > 0:
		self.prev_button.show()

	if self.current_page == pages_count - 1 || pages_count < 2:
		self.next_button.hide()
	elif self.current_page < pages_count - 1:
		self.next_button.show()

func switch_to_prev_page():
	if self.current_page > 0:
		self.current_page -= 1

	self.refresh_current_levels_page()

	if self.current_page == 0:
		if self.next_button.is_visible():
			self.next_button.grab_focus()


func switch_to_next_page():
	var pages_count
	if self.operation_mode == self.MODE_BROWSE:
		pages_count = self.online_service.get_pages_count(self.PAGE_SIZE)
	else:
		pages_count = self.level_manager.get_pages_count(self.PAGE_SIZE)

	if self.current_page < pages_count - 1:
		self.current_page += 1

	self.refresh_current_levels_page()

	if self.current_page == pages_count - 1:
		if self.prev_button.is_visible():
			self.prev_button.grab_focus()


func level_button_pressed(level_name):
	if self.operation_mode == self.MODE_NAME:
		self.set_level_name(level_name)
		self.load_button.grab_focus()
	elif self.operation_mode == self.MODE_SELECT:
		self.execute_success(level_name, "select")
	

func level_button_focused(level_name):
	pass


func connect_buttons():
	self.cancel_button.connect("pressed", Callable(self, "execute_cancel"))

	self.save_button.connect("pressed", Callable(self, "execute_save"))
	self.load_button.connect("pressed", Callable(self, "execute_load"))

	self.next_button.connect("pressed", Callable(self, "switch_to_next_page"))
	self.prev_button.connect("pressed", Callable(self, "switch_to_prev_page"))

	for button in self.level_selection_buttons:
		button.bind_method(self, "level_button_pressed")
		button.bind_focus(self, "level_button_focused")










