// src/software_uart/uart_emulation.rs
use heapless::Vec;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Parity {
    None,
    Even,
    Odd,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum StopBits {
    One,
    Two,
}

#[derive(Clone, Copy, Debug)]
pub struct UartConfig {
    pub data_bits: u8,
    pub parity: Parity,
    pub stop_bits: StopBits,
}

impl Default for UartConfig {
    fn default() -> Self {
        Self {
            data_bits: 8,
            parity: Parity::None,
            stop_bits: StopBits::One,
        }
    }
}

/// Encodes one byte into a sequence of GPIO BSRR words
pub fn encode_uart_byte_cfg(
    pin_bit: u8,
    data: u8,
    cfg: &UartConfig,
    out: &mut [u32; 12],
) -> usize {
    // GPIOx_BSRR register str. 636 kap. 13.4.7
    let set_high = |bit: u8| -> u32 { 1u32 << bit };
    // let set_low = |bit: u8| -> u32 { 0 }; // ODR
    let set_low = |bit: u8| -> u32 { 1u32 << (bit as u32 + 16) }; // BSRR

    let mut idx = 0usize;

    // START bit (LOW)
    out[idx] = set_low(pin_bit);
    idx += 1;

    // Data bits, LSB-first
    let nbits = cfg.data_bits.clamp(5, 8);
    for i in 0..nbits {
        let one = ((data >> i) & 1) != 0;
        out[idx] = if one { set_high(pin_bit) } else { set_low(pin_bit) };
        idx += 1;
    }

    // Parity
    match cfg.parity {
        Parity::None => {}
        Parity::Even | Parity::Odd => {
            let mask: u8 = if nbits == 8 { 0xFF } else { (1u16 << nbits) as u8 - 1 };
            let ones = (data & mask).count_ones() & 1;
            let par_bit_is_one = match cfg.parity {
                Parity::Even => ones == 1,
                Parity::Odd => ones == 0,
                _ => false,
            };
            out[idx] = if par_bit_is_one {
                set_high(pin_bit)
            } else {
                set_low(pin_bit)
            };
            idx += 1;
        }
    }

    // STOP bits (HIGH)
    let stop_ticks = match cfg.stop_bits {
        StopBits::One => 1usize,
        StopBits::Two => 2usize,
    };
    for _ in 0..stop_ticks {
        out[idx] = set_high(pin_bit);
        idx += 1;
    }

    idx
}

/// Decode an oversampled stream of logic levels into UART bytes.
/// Returns (decoded bytes, number of samples consumed/processed).
pub fn decode_uart_samples(
    samples: &[u8],
    oversample: u16,
    cfg: &UartConfig,
) -> (heapless::Vec<u8, 256>, usize) {
    let mut out = Vec::<u8, 256>::new();
    let mut idx = 0usize;
    let nbits = cfg.data_bits as usize;
    let ovs = oversample as usize;

    // Calculate total frame width in samples to ensure we have enough data
    // 1 start + n data + parity? + stops
    let parity_bits = match cfg.parity {
        Parity::None => 0,
        _ => 1,
    };
    let stop_bits_count = match cfg.stop_bits {
        StopBits::One => 1,
        StopBits::Two => 2,
    };
    let frame_bits = 1 + nbits + parity_bits + stop_bits_count;
    let frame_len = frame_bits * ovs;

    // We loop while we have enough remaining samples for a full frame
    while idx + frame_len <= samples.len() {
        // Wait for falling edge (High -> Low)
        // samples[idx] == 1 (Idle/Stop) && samples[idx+1] == 0 (Start)
        if samples[idx] != 0 && samples[idx + 1] == 0 {
            
            // Align to center of START bit
            // Start bit begins at idx+1. Center is at idx + 1 + (ovs/2)
            let center_offset = 1 + (ovs / 2);
            let mut scan_idx = idx + center_offset;

            // Sanity check: is the middle of the start bit actually 0?
            if samples.get(scan_idx).copied().unwrap_or(1) != 0 {
                idx += 1;
                continue;
            }

            // Move to center of first data bit
            scan_idx += ovs;

            // Sample data bits
            let mut data: u8 = 0;
            for bit in 0..nbits {
                let bit_val = samples
                    .get(scan_idx)
                    .map(|&b| if b != 0 { 1u8 } else { 0u8 })
                    .unwrap_or(1); // Default to high if out of bounds (shouldn't happen)
                data |= bit_val << bit;
                scan_idx += ovs;
            }

            // Parity: skip
            match cfg.parity {
                Parity::None => {}
                Parity::Even | Parity::Odd => {
                    scan_idx += ovs;
                }
            }

            // We successfully read a byte. 
            // Advance main `idx` past this frame.
            // We processed `frame_len` samples starting from `idx` (which was the idle bit before start).
            // Actually, to be precise, we should advance by roughly the frame length.
            idx += frame_len;
            
            // If overflow happens in push (unlikely given 256 limit), ignore
            let _ = out.push(data);
        } else {
            // No start bit detected here, move to next sample
            idx += 1;
        }
    }

    (out, idx)
}
