import socket
import time
import random

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

header = [
    0,      0, # buffer length in 16bit words (1, 0) == 1, (0, 1) == 256
    0,   0x80, # buffer type (probably should be 0)
   21,      0, # header length
    0,      0, # buffer number
    0,      0, # run id
  0x3,         # status
    0,         # id of sending module
    0,      0, # timestamp low
    0,      0, # timestamp mid
    0,      0, # timestamp high
] + [0, 0] * 12 # parameters

data = [
    0,
    0,
    0,
    0,
    0,
    0
]

start_time = time.time_ns() // 100

buffer_ids = {
    i: (0, 0) for i in range(10)
}

while True:

    # update timestamp
    base_timestamp = time.time_ns() // 100 - start_time
    t_low  = base_timestamp & 0xffff
    t_mid  = (base_timestamp >> 16) & 0xffff
    t_high = (base_timestamp >> 32) & 0xffff
    header[12] = t_low & 0xff
    header[13] = t_low >> 8
    header[14] = t_mid & 0xff
    header[15] = t_mid >> 8
    header[16] = t_high & 0xff
    header[17] = t_high >> 8

    num_events = random.randint(0, 243)
    # num_events = 243
    # num_events = 1

    # update buffer length
    buffer_length = 21 + num_events * 3
    header[0] = buffer_length & 0xff
    header[1] = (buffer_length >> 8) & 0xff

    # I believe, that in our case we never mix monitor and detector events as
    # the monitors should have id 0 and the detector events 1-9 so I have
    # excluded that posibility here. That would, however, if true mean we could
    # reduce also the number of checks on the parsing side of things...

    is_monitor = random.randint(0, 9)
    # is_monitor = 4

    header[11] = 0 if is_monitor > 3 else random.randint(1,9)

    # update buffer number (each mcpdid has its own buffer number count)
    header[6], header[7] = buffer_ids[header[11]]
    header[6] = (header[6] + 1) % (0xff + 1)
    header[7] = (header[7] + (header[6] == 0)) % (0xff + 1)
    buffer_ids[header[11]] = header[6], header[7]

    tosend = []

    if is_monitor > 3:

        for i in range(num_events):
            d = list(data)

            monitor = random.randint(0,3)
            # monitor = 0

            d[5] = (1 << 7) | monitor

            # update trigger timestamp
            event_timestamp = (time.time_ns() // 100) - base_timestamp
            d[0] = event_timestamp & 0xff
            d[1] = (event_timestamp >> 8) & 0xff
            d[2] = (event_timestamp >> 16) & 0x07

            # completely reversed sorting
            tosend = d + tosend

    else:

        for i in range(num_events):
            d = list(data)

            amplitude = random.randint(0, 255)
            x_pos = random.randint(0, 1023)
            y_pos = random.randint(0, 1023)
            event_timestamp = (time.time_ns() // 100) - base_timestamp

            d[5] = (0 << 7) | (amplitude >> 1)
            d[4] = ((amplitude & 0x01) << 7) | (y_pos >> 3)
            d[3] = ((y_pos << 5) & 0xE0) | (x_pos >> 5)
            d[2] = ((x_pos << 3) & 0xF8)

            d[0] = event_timestamp & 0xff
            d[1] = (event_timestamp >> 8) & 0xff
            d[2] |= (event_timestamp >> 16) & 0x07

            # completely reversed sorting
            tosend = d + tosend

    sock.sendto(bytes(header + tosend), ('127.0.0.1', 54321))
    mv = memoryview(bytes(header)).cast('H')
    print(f'Sent packet {mv[3]} with {num_events} events {base_timestamp}')
    # time.sleep(.1)
