#include "pxl8_protocol.h"
#include "pxl8_bytes.h"

size_t pxl8_protocol_serialize_header(const pxl8_msg_header* msg, u8* buf, size_t len) {
    if (len < sizeof(pxl8_msg_header)) return 0;
    pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
    pxl8_write_u32_be(&s, msg->sequence);
    pxl8_write_u16_be(&s, msg->size);
    pxl8_write_u8(&s, msg->type);
    pxl8_write_u8(&s, msg->version);
    return s.offset;
}

size_t pxl8_protocol_deserialize_header(const u8* buf, size_t len, pxl8_msg_header* msg) {
    if (len < sizeof(pxl8_msg_header)) return 0;
    pxl8_stream s = pxl8_stream_create(buf, (u32)len);
    msg->sequence = pxl8_read_u32_be(&s);
    msg->size = pxl8_read_u16_be(&s);
    msg->type = pxl8_read_u8(&s);
    msg->version = pxl8_read_u8(&s);
    return s.offset;
}

size_t pxl8_protocol_serialize_input(const pxl8_input_msg* msg, u8* buf, size_t len) {
    if (len < sizeof(pxl8_input_msg)) return 0;
    pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
    pxl8_write_u32_be(&s, msg->buttons);
    pxl8_write_f32_be(&s, msg->look_dx);
    pxl8_write_f32_be(&s, msg->look_dy);
    pxl8_write_f32_be(&s, msg->move_x);
    pxl8_write_f32_be(&s, msg->move_y);
    pxl8_write_f32_be(&s, msg->yaw);
    pxl8_write_u64_be(&s, msg->tick);
    pxl8_write_u64_be(&s, msg->timestamp);
    return s.offset;
}

size_t pxl8_protocol_deserialize_input(const u8* buf, size_t len, pxl8_input_msg* msg) {
    if (len < sizeof(pxl8_input_msg)) return 0;
    pxl8_stream s = pxl8_stream_create(buf, (u32)len);
    msg->buttons = pxl8_read_u32_be(&s);
    msg->look_dx = pxl8_read_f32_be(&s);
    msg->look_dy = pxl8_read_f32_be(&s);
    msg->move_x = pxl8_read_f32_be(&s);
    msg->move_y = pxl8_read_f32_be(&s);
    msg->yaw = pxl8_read_f32_be(&s);
    msg->tick = pxl8_read_u64_be(&s);
    msg->timestamp = pxl8_read_u64_be(&s);
    return s.offset;
}

size_t pxl8_protocol_serialize_command(const pxl8_command_msg* msg, u8* buf, size_t len) {
    if (len < sizeof(pxl8_command_msg)) return 0;
    pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
    pxl8_write_u16_be(&s, msg->cmd_type);
    pxl8_write_bytes(&s, msg->payload, PXL8_COMMAND_PAYLOAD_SIZE);
    pxl8_write_u16_be(&s, msg->payload_size);
    pxl8_write_u64_be(&s, msg->tick);
    return s.offset;
}

size_t pxl8_protocol_deserialize_command(const u8* buf, size_t len, pxl8_command_msg* msg) {
    if (len < sizeof(pxl8_command_msg)) return 0;
    pxl8_stream s = pxl8_stream_create(buf, (u32)len);
    msg->cmd_type = pxl8_read_u16_be(&s);
    pxl8_read_bytes(&s, msg->payload, PXL8_COMMAND_PAYLOAD_SIZE);
    msg->payload_size = pxl8_read_u16_be(&s);
    msg->tick = pxl8_read_u64_be(&s);
    return s.offset;
}

size_t pxl8_protocol_serialize_entity_state(const pxl8_entity_state* state, u8* buf, size_t len) {
    if (len < sizeof(pxl8_entity_state)) return 0;
    pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
    pxl8_write_u64_be(&s, state->entity_id);
    pxl8_write_bytes(&s, state->userdata, 56);
    return s.offset;
}

size_t pxl8_protocol_deserialize_entity_state(const u8* buf, size_t len, pxl8_entity_state* state) {
    if (len < sizeof(pxl8_entity_state)) return 0;
    pxl8_stream s = pxl8_stream_create(buf, (u32)len);
    state->entity_id = pxl8_read_u64_be(&s);
    pxl8_read_bytes(&s, state->userdata, 56);
    return s.offset;
}

size_t pxl8_protocol_serialize_event(const pxl8_event_msg* msg, u8* buf, size_t len) {
    if (len < sizeof(pxl8_event_msg)) return 0;
    pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
    pxl8_write_u8(&s, msg->event_type);
    pxl8_write_bytes(&s, msg->payload, PXL8_EVENT_PAYLOAD_SIZE);
    return s.offset;
}

size_t pxl8_protocol_deserialize_event(const u8* buf, size_t len, pxl8_event_msg* msg) {
    if (len < sizeof(pxl8_event_msg)) return 0;
    pxl8_stream s = pxl8_stream_create(buf, (u32)len);
    msg->event_type = pxl8_read_u8(&s);
    pxl8_read_bytes(&s, msg->payload, PXL8_EVENT_PAYLOAD_SIZE);
    return s.offset;
}

size_t pxl8_protocol_serialize_snapshot_header(const pxl8_snapshot_header* hdr, u8* buf, size_t len) {
    if (len < sizeof(pxl8_snapshot_header)) return 0;
    pxl8_write_stream s = pxl8_write_stream_create(buf, (u32)len);
    pxl8_write_u16_be(&s, hdr->entity_count);
    pxl8_write_u16_be(&s, hdr->event_count);
    pxl8_write_u64_be(&s, hdr->player_id);
    pxl8_write_u64_be(&s, hdr->tick);
    pxl8_write_f32_be(&s, hdr->time);
    return s.offset;
}

size_t pxl8_protocol_deserialize_snapshot_header(const u8* buf, size_t len, pxl8_snapshot_header* hdr) {
    if (len < sizeof(pxl8_snapshot_header)) return 0;
    pxl8_stream s = pxl8_stream_create(buf, (u32)len);
    hdr->entity_count = pxl8_read_u16_be(&s);
    hdr->event_count = pxl8_read_u16_be(&s);
    hdr->player_id = pxl8_read_u64_be(&s);
    hdr->tick = pxl8_read_u64_be(&s);
    hdr->time = pxl8_read_f32_be(&s);
    return s.offset;
}
