@tool
class_name pribor_uf extends 'res://scenes/контроль/прибор.gd'

enum STATE_VAL {NONE = 0, GOOD = 1, ERROR = 2}
const DEV_LIST = ['00:1f.6', '07:00.0', '08:00.0', '0b:00.0']
const USB_TRACKERBALL = '13ba:0018'
const REVFF = '(rev ff)'
var ctrl_pos = 100


func _ready() -> void:
    if Engine.is_editor_hint():
        return
    var unit_uf: = network.get_unit_instance('уарэп-эмс')
    unit_uf.connect('line_changed', on_line_changed)
    control_vk()
    on_line_changed(unit_uf)
    find_labels(self)


func  on_timer(unit_name: StringName):
    var unit_uf = network.get_unit_instance(unit_name)
    status_uf()  # отслеживание состояния прибора
    if unit_uf.online: # проверка ЯУ, внутреннего коммутатора и ИП-5Б
        control_ems(unit_uf.status)  # проверка ЭМС ячеек


func on_line_changed(unit_uf):
    if unit_uf.online: # проверка ЯУ, внутреннего коммутатора и ИП-5Б
        $ref_uf/ref_kassetav13/ref_A7_YAU07.state = STATE_VAL.GOOD
        $ref_uf/ref_kassetav13/ref_A3_IP5B.state = STATE_VAL.GOOD
        $ref_uf/ref_kassetav13/ref_A9_ESW.state = STATE_VAL.GOOD
    else:
        $ref_uf/ref_kassetav13/ref_A7_YAU07.state = STATE_VAL.ERROR
        $ref_uf/ref_kassetav13/ref_A4_KEMS.state = STATE_VAL.NONE
        $ref_uf/ref_kassetav13/ref_A5_EMS.state = STATE_VAL.NONE
        $ref_uf/ref_kassetav13/ref_A6_EMS.state = STATE_VAL.NONE
        $ref_uf/ref_kassetav13/ref_A3_IP5B.state = STATE_VAL.NONE
        $ref_uf/ref_kassetav13/ref_A9_ESW.state = STATE_VAL.NONE
        $ref_uf/ref_kassetav13/ref_A12_IP27B.state = STATE_VAL.NONE


func control_vk() -> void:
    var out_usb_str = ''
    var out_pci_str = ''
    var out_usb = []
    var out_pci = []

    OS.execute('lsusb', [], out_usb)  # TODO: Не переносимое, платформозависимое
    for element in out_usb:
        out_usb_str += element

    OS.execute('lspci', [], out_pci)  # TODO: Не переносимое, платформозависимое
    for element in out_pci:
        out_pci_str += element

    out_usb_str = out_usb_str.split('\n')
    out_pci_str = out_pci_str.split('\n')

    var i: int = 0

    for string in out_usb_str:
        if USB_TRACKERBALL in string:
            i += 1
            break
    for string in out_pci_str:
        for item in DEV_LIST:
            if item in string:
                i += 1
                if REVFF in string:
                    i -= 1
                    break

    if i == (DEV_LIST.size() + 1):
        $ref_uf/ref_bu1/ref_A8_BU.state = STATE_VAL.GOOD
    else:
        $ref_uf/ref_bu1/ref_A8_BU.state = STATE_VAL.ERROR

    $ref_uf/ref_bu1/ref_A8_BU.state = STATE_VAL.GOOD    ## заглушка для зеленения ВК-110


func control_ems(status: Array) -> void:
    $ref_uf/ref_kassetav13/ref_A4_KEMS.state = STATE_VAL.GOOD if ((status[0] >> 0) & 1) and ((status[0] >> 3) & 1)  else STATE_VAL.ERROR
    $ref_uf/ref_kassetav13/ref_A5_EMS.state = STATE_VAL.GOOD if ((status[0] >> 1) & 1) and ((status[0] >> 4) & 1)  else STATE_VAL.ERROR
    $ref_uf/ref_kassetav13/ref_A6_EMS.state = STATE_VAL.GOOD if ((status[0] >> 2) & 1) and ((status[0] >> 5) & 1)  else STATE_VAL.ERROR
    $ref_uf/ref_kassetav13/ref_A12_IP27B.state = STATE_VAL.GOOD if ((status[2] >> 6) & 1)  else STATE_VAL.ERROR


func status_uf() -> void:
    var status_bu: int = $ref_uf/ref_bu1/ref_A8_BU.state
    var status_yau: int = $ref_uf/ref_kassetav13/ref_A7_YAU07.state
    var status_kyems: int = $ref_uf/ref_kassetav13/ref_A4_KEMS.state
    var status_yems0: int = $ref_uf/ref_kassetav13/ref_A5_EMS.state
    var status_yems1: int = $ref_uf/ref_kassetav13/ref_A6_EMS.state
    var status_ip5: int = $ref_uf/ref_kassetav13/ref_A3_IP5B.state
    var status_ip27: int = $ref_uf/ref_kassetav13/ref_A12_IP27B.state

    if (status_yau == 1) and (status_bu == 1) and (status_kyems == 1) \
    and (status_yems0 == 1) and (status_yems1 == 1) and (status_ip5 == 1) and (status_ip27 == 1):
        $lbl_header2.text = 'Прибор исправен'
        $lbl_header2.modulate = Color.GREEN
    else:
        $lbl_header2.text = 'Прибор не исправен'
        $lbl_header2.modulate = Color.RED
    $lbl_header2.visible = true


func find_labels(node1: Node):
    if node1 is Label:
        if node1.name in ["lbl_rname", "rname"]:
            if not node1.has_meta("tooltip_set"):
                ctrl_pos += 1
                var box_number = ceili(float(ctrl_pos) / 10.0)
                var position_number = ctrl_pos % 10
                if position_number == 0:
                    position_number = 10
                node1.tooltip_text = "Ящик - %d, Позиция - %d" % [box_number, position_number]
                node1.mouse_filter = Control.MOUSE_FILTER_STOP
                node1.set_meta("tooltip_set", true)
    for child in node1.get_children():
        find_labels(child)
