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

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


func _ready() -> void:
    if Engine.is_editor_hint(): return
    #control_vk()
    uf_control = get_parent().uf_control
    signaller.connect('update_uf_serviceability', on_update_uf_serviceability)
    var uf_status = uf_control.get_current_status()
    control_ems(uf_status)
    find_labels(self)


func on_update_uf_serviceability(_state: int):
    control_ems(uf_control.get_current_status())


#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


func control_ems(status: Dictionary) -> void:
    if status['УФ-онлайн']:
        $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_bu1/ref_A8_BU.state = STATE_VAL.GOOD
        $ref_uf/ref_kassetav13/ref_A9_ESW.state = STATE_VAL.GOOD
        $ref_uf/ref_kassetav13/ref_A4_KEMS.state = STATE_VAL.GOOD if status['КЭМС-Б1'] else STATE_VAL.ERROR
        $ref_uf/ref_kassetav13/ref_A5_EMS.state = STATE_VAL.GOOD if status['ЭМС-Б1']  else STATE_VAL.ERROR
        $ref_uf/ref_kassetav13/ref_A6_EMS.state = STATE_VAL.GOOD if status['ЭМС-Б2'] else STATE_VAL.ERROR
        $ref_uf/ref_kassetav13/ref_A12_IP27B.state = STATE_VAL.GOOD if status['ИП220-27Б'] else STATE_VAL.ERROR
    else:
        $ref_uf/ref_kassetav13/ref_A7_YAU07.state = STATE_VAL.ERROR
        $ref_uf/ref_kassetav13/ref_A3_IP5B.state = STATE_VAL.NONE
        $ref_uf/ref_bu1/ref_A8_BU.state = STATE_VAL.NONE
        $ref_uf/ref_kassetav13/ref_A9_ESW.state = STATE_VAL.NONE
        $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_A12_IP27B.state = STATE_VAL.NONE
    status_uf(status)


func status_uf(status_uf_dic: Dictionary) -> void:
    var is_ok: bool = true
    if not status_uf_dic['УФ-онлайн']:
        $lbl_header2.text = 'Ожидание связи с ЯУ-07Б'
        $lbl_header2.modulate = Color.WHITE
    elif status_uf_dic['УФ-онлайн']:
        for socket in status_uf_dic:
            if not status_uf_dic[socket]:
                is_ok = false
        if is_ok:
            $lbl_header2.text = 'Прибор исправен'
            $lbl_header2.modulate = Color.GREEN
        elif not is_ok:
            $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)
