@tool

extends PanelContainer

func set_text(val: String): $button.set_text(val)
func set_disabled(val: bool): disabled = val
func set_rec_visible(val: bool): $recommend.visible = val
func get_text(): return $button.get_text()
func is_pressed(): return $button.is_pressed()
func is_toggle_mode(): return $button.is_toggle_mode()


var _pressed: = false

@export var texture_state0: Texture2D:
    set(val):
        texture_state0 = val


@export var texture_state1: Texture2D:
    set(val):
        texture_state1 = val


@export var strips_rotation: float = 0.0:
    set(val):
        strips_rotation = val
        if is_inside_tree():
            $back.strips_rotation = deg_to_rad(val)


@export var text: String = '':
    set(val):
        text = val
        if is_inside_tree():
            $button.set_text(val)


@export var btn_tooltip: String = '':
    set(val):
        btn_tooltip = val
        if is_inside_tree():
            $button.set_tooltip_text(val)


@export var disabled: bool = false:
    set(val):
        disabled = val
        if is_inside_tree():
            $button.set_disabled(val)
            $back.disabled = val
            $state.modulate.a = 0.5 if val else 1.0


@export var pressed: bool = false:
    set(val):
        pressed = val
        _pressed = val
        if is_inside_tree():
            $button.set_pressed(val)
            _on_button_toggled(val)


@export var toggle_mode: bool = true:
    set(val):
        toggle_mode = val
        if is_inside_tree():
            $button.set_toggle_mode(val)


@export var font_size: int = 15:
    set(val):
        font_size = val
        if is_inside_tree():
            $button.add_theme_font_size_override('theme_override_font_sizes/font_size', val)


func set_pressed(val: bool):
    pressed = val


func button_connect(button_signal: String, proc: Callable):
    var rc = $button.connect(button_signal, proc)
    if rc != Error.OK:
        push_error('возникла ошибка \"%s\" при подключении сигнала \"%s\" к обработчику \"%s\"' % [error_string(rc), button_signal, proc])


func _on_resized() -> void:
    $back.scale = size
    $back.material.set('shader_parameter/scale', size / Vector2(20.0, 20.0))


func _enter_tree() -> void:
    $back.scale = size
    $back.material.set('shader_parameter/scale', size / Vector2(20.0, 20.0))
    $button.connect('toggled', _on_button_toggled)
    $button.set_toggle_mode(toggle_mode)
    $button.add_theme_font_size_override('theme_override_font_sizes/font_size', font_size)
    pressed = _pressed
    $button.set_pressed(pressed)
    $button.set_disabled(disabled)
    $back.disabled = disabled
    $state.modulate.a = 0.5 if disabled else 1.0
    $button.set_tooltip_text(btn_tooltip)
    $button.set_text(text)


func _on_settings_changed():
    pass


func _exit_tree() -> void:
    $button.disconnect('toggled', _on_button_toggled)


func set_toggle_mode(val: bool):
    $button.set_toggle_mode(val)


func _on_button_toggled(val: bool):
    if val: $state.set_texture(texture_state1)
    else:   $state.set_texture(texture_state0)
