extends Node2D

var sock = PacketPeerUDP.new()
@onready var table_threats = get_node('ColorRect/tabs/threats/table_t')
@onready var btn_add_threats = get_node('ColorRect/tabs/threats/btn_add')
@onready var btn_del_threats = get_node('ColorRect/tabs/threats/btn_del')
@onready var btn_save_threats = get_node('ColorRect/tabs/threats/btn_save')
@onready var btn_load_threats = get_node('ColorRect/tabs/threats/btn_load')
@onready var btn_rotate = get_node('ColorRect/tabs/threats/panel_rotate/btn_rotate')
@onready var entry_rotate = get_node('ColorRect/tabs/threats/panel_rotate/entry_rotate')
@onready var chkbx_rotate = get_node('ColorRect/tabs/threats/panel_rotate/chkbx_rotate')

@onready var table_pomehi = get_node('ColorRect/tabs/pomehi/table_p')

# Called when the node enters the scene tree for the first time.
func _ready():
    var nodes: Dictionary = {
        'btn_add': btn_add_threats,
        'btn_del': btn_del_threats,
        'btn_save': btn_save_threats,
        'btn_load': btn_load_threats,
        'btn_rt': btn_rotate,
        'entry_rt': entry_rotate,
        'chkbx_rt': chkbx_rotate}
    sock.bind(Consts.MAIN_ADDR[1], Consts.MAIN_ADDR[0])
    sock.set_dest_address(Consts.DST_ADDR[0], Consts.DST_ADDR[1])
    Threats.init_treats(table_threats, nodes)
    Interfers.init_pomehi(table_pomehi)
    fs.init_fs($ColorRect/tabs/pomehi, $ColorRect/tabs/pomehi/fs_rect)
    var timer = Timer.new()
    timer.connect('timeout', Callable(self, 'on_timeout').bind())
    add_child(timer) #to process
    timer.start(1) #to start


func on_timeout():
    sock.put_packet(Threats.get_threats_data())
    Interfers.check_interfers_state()


func on_receive(data):
    var json_conv: = JSON.new()
    var utf8_data = data.get_string_from_utf8()
    if Error.OK == json_conv.parse(utf8_data):
        var json_data = json_conv.get_data()
        if not (json_data is Dictionary):                                    return
        if not json_data.has('ts'):                                          return
        if not ((json_data['ts'] is int) or (json_data['ts'] is float)):     return
        if json_data['ts'] == 6:
            if not json_data.has('ispp'):                                        return
            if not ((json_data['ispp'] is int) or (json_data['ispp'] is float)): return
            if not json_data.has('krp'):                                         return
            if not json_data.has('ss'):                                          return
            if not ((json_data['ss'] is int) or (json_data['ss'] is float)):     return
            if json_data['krp'] == 'off':
                Interfers.off_interfer(json_data)
                sock.put_packet(Interfers.get_msg7_data())
                return
            if not json_data.has('powp'):                                        return
            if not json_data.has('kni'):                                         return
            if not json_data.has('params'):                                      return
            if not json_data.has('get'):                                         return
            Interfers.set_interfer(json_data)
            sock.put_packet(Interfers.get_msg7_data())
        elif json_data['ts'] == 8:
            fs.on_receive_msg8(json_data)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    while sock.get_available_packet_count() > 0:
        var data: = sock.get_packet()
        var addr: = sock.get_packet_ip()
        var port: = sock.get_packet_port()
        var addr_port: = [addr, port]
        on_receive(data)
