#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <lasertag_utils.h>
#include <ir_recv.h>

LOG_MODULE_REGISTER(ir_recv_sim, LOG_LEVEL_INF);

int main(void)
{
    LOG_INF("Starting IR receive simulator application...");
    lasertag_utils_init();
    ir_recv_init();

    ir_packet_t test_packet = {0};

    /* Test 1: Perfektes Signal */
    LOG_INF("Sending perfect packet...");
    test_packet.data.fields.type = 1;
    test_packet.data.fields.id = 42;
    test_packet.data.fields.value = 15;
    ir_recv_sim_send_packet(&test_packet, NULL);
    
    k_msleep(500);

    LOG_INF("Sending 1. noisy packet with high jitter...");
    test_packet.data.fields.id = 255;
    
    ir_sim_error_t ext_error = {
        .noise_flips_per_8 = 1, /* Maximal 1 Fehl-Sample pro Block (meist im Space) */
        .jitter_mark = 1,       /* Mark: 7 bis 9 Samples */
        .jitter_space_0 = 2,    /* Space 0: 6 bis 10 Samples */
        .jitter_space_1 = 2     /* Space 1: 14 bis 18 Samples */
    };
    ir_recv_sim_send_packet(&test_packet, &ext_error);

    k_msleep(500);

    LOG_INF("Sending 2. noisy packet with high jitter...");
    test_packet.data.fields.id = 255;
    
    ext_error.noise_flips_per_8 = 2; 
    ext_error.jitter_mark = 1; 
    ext_error.jitter_space_0 = 2; 
    ext_error.jitter_space_1 = 2;
    ir_recv_sim_send_packet(&test_packet, &ext_error);

    k_msleep(500);

    LOG_INF("Sending 3. noisy packet with high jitter...");
    test_packet.data.fields.id = 255;
    
    ext_error.noise_flips_per_8 = 1;
    ext_error.jitter_mark = 2;
    ext_error.jitter_space_0 = 2;
    ext_error.jitter_space_1 = 4;
    ir_recv_sim_send_packet(&test_packet, &ext_error);

    return 0;
}