@tool
extends TextureButton

@onready var tween: Tween
@export var state_colors: Array


func _ready():
    toggle_mode = true
    focus_mode = Control.FOCUS_ALL
    connect('focus_entered', _on_focus_entered)
    connect('focus_exited', _on_focus_exited)
    ProjectSettings.connect("settings_changed", Callable(self, "_on_settings_changed"))
    _on_settings_changed()


func _enter_tree() -> void:
    if is_inside_tree():
        $label.self_modulate = state_colors[state]


func update_progress_bar(target_value) -> void:
    if tween:
        tween.kill()
    tween = create_tween()
    tween.tween_property($control_progress, 'value', target_value, 0.3)


func _on_toggled(toggled_on):
    if has_node('frame'):
        $frame.visible = toggled_on


func _on_focus_entered():
    button_pressed = true
    #enter_press()  TODO: Пока что без выбора прибора


func _on_focus_exited():
    pass


func enter_press():
    var enter_event = InputEventAction.new()
    enter_event.action = 'ui_accept'
    enter_event.pressed = true
    Input.parse_input_event(enter_event)


func _on_settings_changed():
    if not is_inside_tree(): return

    var white_color = ProjectSettings.get_setting('application/config/%s' % 'Цвет фона схемы', Color.DIM_GRAY)
    var json_col_w = ProjectSettings.get_setting('application/config/%s' % 'Цвет фона схемы', '[0.0, 0.0, 0.0, 0.0]')
    var arr_col_w = JSON.parse_string(json_col_w)
    white_color = Color(arr_col_w[0], arr_col_w[1], arr_col_w[2], arr_col_w[3]) if arr_col_w else Color.DIM_GRAY
    
    var black_color = ProjectSettings.get_setting('application/config/%s' % 'Цвет схемы', Color.WHITE_SMOKE)
    var json_col_b = ProjectSettings.get_setting('application/config/%s' % 'Цвет схемы', '[1.0, 1.0, 1.0, 1.0]')
    var arr_col_b = JSON.parse_string(json_col_b)
    black_color = Color(arr_col_b[0], arr_col_b[1], arr_col_b[2], arr_col_b[3]) if arr_col_b else Color.WHITE_SMOKE
    
    $pic_functional.material.set('shader_parameter/white', white_color)
    $pic_functional.material.set('shader_parameter/black', black_color)


@export var state: int:
    set(v):
        v = 0 if v < 0 else v % state_colors.size()
        state = v
        if is_inside_tree():
            $label.self_modulate = state_colors[v]


@export var progress_value: float = 0.0:
    set(v):
        progress_value = v
        if is_inside_tree():
            update_progress_bar(v)
            var max_val = $control_progress.get('max_value')
            var percentage = (v / max_val) * 100.0
            $control_progress.tooltip_text = 'Процесс контроля %01d%%' % percentage


@export var progress_visible: bool:
    set(v):
        progress_visible = v
        if is_inside_tree():
            $control_progress.visible = v


@export var progress_max_value: int:
    set(v):
        progress_max_value = v
        if is_inside_tree():
            $control_progress.max_value = v


@export var progress_min_value: int:
    set(v):
        progress_min_value = v
        if is_inside_tree():
            $control_progress.min_value = v


@export var show_functional: bool:
    set(v):
        show_functional = v
        if is_inside_tree():
            $pic_functional.visible = v
