extends "res://scenes/контроль/кассета.gd"

enum STATE_VAL {
    NONE = 0,
    GOOD = 1,
    ERROR = 2 }


func _ready() -> void:
    var unit_name = get_parent().get_meta('unit_name')[0]
    var unit_prd = network.get_unit_instance(unit_name)
    unit_prd.connect('data_received', Callable(self, 'on_data_received'))
    unit_prd.connect('line_changed', Callable(self, 'on_line_changed'))
    set_default_state()


func set_default_state():
    $ref_A30.state = STATE_VAL.ERROR  # ЯУ-07Б
    $ref_A24.state = STATE_VAL.NONE  # ИП5-25
    $ref_A26.state = STATE_VAL.NONE  # ЭМС-Г
    $ref_A27.state = STATE_VAL.NONE  # УГ
    $ref_A28.state = STATE_VAL.NONE  # УКП
    $ref_A29.state = STATE_VAL.NONE  # УКП


func on_line_changed(unit_prd):
    if not unit_prd.online:
        set_default_state()


func on_data_received(unit_prd):
    $ref_A30.state = STATE_VAL.GOOD
    var ems_online = unit_prd.status[0] & 1
    var bit_load = 4
    var ems_load = (unit_prd.status[0] >> bit_load) & 1
    var ug_online = (unit_prd.status[0] >> 1) & 1
    $ref_A26.state = STATE_VAL.GOOD if ems_online and ems_load else STATE_VAL.ERROR
    $ref_A27.state = STATE_VAL.GOOD if ug_online else STATE_VAL.ERROR
    $ref_A28.state = STATE_VAL.GOOD if (unit_prd.status[0] >> 2) & 1 else STATE_VAL.ERROR
    $ref_A29.state = STATE_VAL.GOOD if (unit_prd.status[0] >> 3) & 1 else STATE_VAL.ERROR
    if ug_online:
        var byte_dry_contact: int = 27
        var bit_ip5_25: int = 7
        var ip5_25 = unit_prd.status[byte_dry_contact] >> bit_ip5_25 & 1
        $ref_A24.state = STATE_VAL.ERROR if ip5_25 else STATE_VAL.GOOD
    else:
        $ref_A24.state = STATE_VAL.NONE
