extends Control

@onready var mode_option_button: OptionButton = $VBoxContainer/ToolBar/ModeOptionButton
@onready var open_button: Button = $VBoxContainer/ToolBar/OpenButton
@onready var open_file_dialog: FileDialog = $VBoxContainer/ToolBar/OpenButton/OpenFileDialog
@onready var save_button: Button = $VBoxContainer/ToolBar/SaveButton
@onready var save_as_button: Button = $VBoxContainer/ToolBar/SaveAsButton
@onready var save_file_dialog: FileDialog = $VBoxContainer/ToolBar/SaveAsButton/SaveFileDialog

@onready var setting_mode: TabContainer = $VBoxContainer/VBoxContainer/Content/SettingMode
@onready var asset_mode: Control = $VBoxContainer/VBoxContainer/Content/AssetMode
@onready var design_mode: PanelContainer = $VBoxContainer/VBoxContainer/Content/DesignMode
@onready var palette_mode: Control = $VBoxContainer/VBoxContainer/Content/PaletteMode
@onready var dressing_mode: PanelContainer = $VBoxContainer/VBoxContainer/Content/DressingMode

@onready var config_panel: PanelContainer = $VBoxContainer/VBoxContainer/Content/SettingMode/ConfigPanel


var mode_panels = []
var body_panels = []
var cloth_panels = []

func _ready() -> void:
	_init_gui()

func _init_gui():
	mode_panels.append(setting_mode)
	mode_panels.append(asset_mode)
	mode_panels.append(design_mode)
	mode_panels.append(palette_mode)
	mode_panels.append(dressing_mode)
	
	mode_option_button.item_selected.connect(_on_option_selected)
	open_button.pressed.connect(_on_open_button_pressed)
	open_file_dialog.file_selected.connect(_on_open_file_dialog_file_selected)
	save_as_button.pressed.connect(_on_save_as_button_pressed)
	save_file_dialog.file_selected.connect(_on_save_file_dialog_file_selected)
	save_button.pressed.connect(_on_save_button_pressed)
	
	asset_mode.goto_asset_using.connect(_on_asset_panel_goto_using)
	
	_on_option_selected(mode_option_button.selected)
	
func _on_option_selected(index):
	for i in range(mode_panels.size()):
		if i != index: mode_panels[i].visible = false
		else: mode_panels[i].visible = true

func _on_open_button_pressed():
	open_file_dialog.popup()

func _on_open_file_dialog_file_selected(path):
	if AvatarDollDataMgr.load_json(path):
		config_panel.load_data()
		asset_mode.load_data()
		design_mode.load_data()
		palette_mode.load_data()
		
func _on_save_as_button_pressed():
	save_file_dialog.popup()
	
func _on_save_file_dialog_file_selected(path):
	if path != "":
		AvatarDollDataMgr.save_json(path)
	
func _on_save_button_pressed():
	AvatarDollDataMgr.save_json()
	
func _on_asset_panel_goto_using(using_type: String, asset_id: int, avatar_type: String, avatar_part_index: int, state_code: String, element_index: int):
	if using_type == "icon":
		pass
	else:
		var index = mode_panels.find(design_mode)
		mode_option_button.selected = index
		_on_option_selected(index)
		
		design_mode.set_select(avatar_type, avatar_part_index, state_code, element_index)
	
func switch_mode(target_panel):
	for i in range(mode_panels.size()):
		if mode_panels[i] != target_panel: mode_panels[i].visible = false
		else: mode_panels[i].visible = true
