extends PanelContainer

signal action(index: int, action: String)
signal value_change(index: int, data: Dictionary)

@onready var index_button: Button = $HBoxContainer/VBoxContainer/IndexButton
@onready var up_button: Button = $HBoxContainer/VBoxContainer/UpButton
@onready var down_button: Button = $HBoxContainer/VBoxContainer/DownButton
@onready var remove_button: Button = $HBoxContainer/VBoxContainer/RemoveButton

@onready var assets_combo: OptionButton = $HBoxContainer/GridContainer/AssetsCombo
@onready var basic_color_picker: ColorPickerButton = $HBoxContainer/GridContainer/BasicColorPicker
@onready var tolerance_slider: HSlider = $HBoxContainer/GridContainer/HBoxContainer/ToleranceSlider
@onready var tolerance_spin_box: SpinBox = $HBoxContainer/GridContainer/HBoxContainer/ToleranceSpinBox
@onready var modulate_color_picker: ColorPickerButton = $HBoxContainer/GridContainer/ModulateColorPicker
@onready var blend_mode_combo: OptionButton = $HBoxContainer/GridContainer/BlendModeCombo


var _block_signal;
var palette_index = -1
var assets_index = {}

func _ready() -> void:
	_block_signal = false;
	
	up_button.pressed.connect(_on_up_button_pressed)
	down_button.pressed.connect(_on_down_button_pressed)
	remove_button.pressed.connect(_on_remove_button_pressed)
	
	assets_combo.item_selected.connect(_on_assets_changed)
	basic_color_picker.color_changed.connect(_on_basic_color_changed)
	tolerance_slider.value_changed.connect(_on_tolerance_changed)
	tolerance_spin_box.value_changed.connect(_on_tolerance_changed)
	modulate_color_picker.color_changed.connect(_on_modulate_color_changed)
	blend_mode_combo.item_selected.connect(_on_blend_mode_changed)
	reset_parameters()

func setup_data(index, assets, is_first: bool, is_final: bool):
	palette_index = index
	index_button.text = "%02d" % (index + 1)
	
	assets_combo.clear()
	assets_index.clear()
	for key in assets:
		var assets_id = int(key) + 1
		assets_combo.add_item("%d - %s" % [key, assets[key]], assets_id)
		assets_index[assets_id] = assets_index.size()
	
	up_button.disabled = true if is_first else false
	down_button.disabled = true if is_final else false
	
func reset_parameters():
	set_block_signal(true)
	assets_combo.selected = 0
	basic_color_picker.color = Color.WHITE
	modulate_color_picker.color = Color.WHITE
	tolerance_slider.value = 1.0
	tolerance_spin_box.value = 1.0
	blend_mode_combo.selected = 0
	set_block_signal(false)
	_value_change()

func load_parameters(asset_selected, basic_color, tolerance, modulate_color, blend_mode):
	set_block_signal(true)
	assets_combo.selected = assets_index[int(asset_selected + 1)]
	basic_color_picker.color = basic_color
	modulate_color_picker.color = modulate_color
	tolerance_slider.value = tolerance
	tolerance_spin_box.value = tolerance
	blend_mode_combo.selected = blend_mode_combo.get_item_index(blend_mode)
	set_block_signal(false)
	
func get_paramters():
	return {
		"ElementIndex": int(assets_combo.get_item_id(assets_combo.selected) - 1),
		"Tolerance": tolerance_spin_box.value,
		"BlendMode": blend_mode_combo.get_item_id(blend_mode_combo.selected),
		"BasicColor": { 
			"R": basic_color_picker.color.r,
			"G": basic_color_picker.color.g,
			"B": basic_color_picker.color.b,
			"A": basic_color_picker.color.a,
		},
		"ModulateColor": { 
			"R": modulate_color_picker.color.r,
			"G": modulate_color_picker.color.g,
			"B": modulate_color_picker.color.b,
			"A": modulate_color_picker.color.a,
		},
	}

func set_block_signal(block_signal: bool):
	_block_signal = block_signal;

func set_selected(_selected: bool):
	grab_focus()
	
func _on_up_button_pressed():
	action.emit(palette_index, "UP")
	
func _on_down_button_pressed():
	action.emit(palette_index, "DOWN")
	
func _on_remove_button_pressed():
	action.emit(palette_index, "REMOVE")
	
func _value_change():
	if _block_signal: 
		return
	value_change.emit(palette_index, get_paramters())

func _on_assets_changed(_index: int):
	_value_change()

func _on_basic_color_changed(_color: Color):
	_value_change()

func _on_modulate_color_changed(_color: Color):
	_value_change()

func _on_tolerance_changed(value: float):
	tolerance_slider.value = value
	tolerance_spin_box.value = value
	_value_change()

func _on_blend_mode_changed(_index: int):
	_value_change()
