use crate::{common::EmitIntensity, defined::float, error::AUTDInternalError};

use super::{FOCUS_STM_FIXED_NUM_LOWER, FOCUS_STM_FIXED_NUM_UNIT, FOCUS_STM_FIXED_NUM_UPPER};

#[repr(C)]
pub struct STMFocus {
    pub(crate) buf: [u16; 4],
}

impl STMFocus {
    fn to_fixed_num(x: float) -> Result<i32, AUTDInternalError> {
        let ix = (x / FOCUS_STM_FIXED_NUM_UNIT).round() as i32;
        if !(FOCUS_STM_FIXED_NUM_LOWER..=FOCUS_STM_FIXED_NUM_UPPER).contains(&ix) {
            return Err(AUTDInternalError::FocusSTMPointOutOfRange(x));
        }
        Ok(ix)
    }

    pub fn set(
        &mut self,
        x: float,
        y: float,
        z: float,
        intensity: EmitIntensity,
    ) -> Result<(), AUTDInternalError> {
        let ix = Self::to_fixed_num(x)?;
        let iy = Self::to_fixed_num(y)?;
        let iz = Self::to_fixed_num(z)?;
        self.buf[0] = (ix & 0xFFFF) as u16;
        self.buf[1] = ((iy << 2) & 0xFFFC) as u16
            | ((ix >> 30) & 0x0002) as u16
            | ((ix >> 16) & 0x0001) as u16;
        self.buf[2] = ((iz << 4) & 0xFFF0) as u16
            | ((iy >> 28) & 0x0008) as u16
            | ((iy >> 14) & 0x0007) as u16;
        self.buf[3] = (((intensity.value() as u16) << 6) & 0x3FC0)
            | ((iz >> 26) & 0x0020) as u16
            | ((iz >> 12) & 0x001F) as u16;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use crate::fpga::FOCUS_STM_FIXED_NUM_WIDTH;

    use super::*;

    #[test]
    fn stm_focus() {
        let mut p = STMFocus { buf: [0; 4] };

        let x = FOCUS_STM_FIXED_NUM_UNIT;
        let y = 2. * FOCUS_STM_FIXED_NUM_UNIT;
        let z = 3. * FOCUS_STM_FIXED_NUM_UNIT;
        let intensity = EmitIntensity::new(4);

        assert!(p.set(x, y, z, intensity).is_ok());

        assert_eq!(
            (p.buf[0] as u32) & ((1 << FOCUS_STM_FIXED_NUM_WIDTH) - 1),
            1
        );
        assert_eq!(
            ((p.buf[1] >> 2) as u32) & ((1 << FOCUS_STM_FIXED_NUM_WIDTH) - 1),
            2
        );
        assert_eq!(
            ((p.buf[2] >> 4) as u32) & ((1 << FOCUS_STM_FIXED_NUM_WIDTH) - 1),
            3
        );
        assert_eq!((p.buf[3] >> 6) & 0xFF, 4);

        let x = -FOCUS_STM_FIXED_NUM_UNIT;
        let y = -2. * FOCUS_STM_FIXED_NUM_UNIT;
        let z = -3. * FOCUS_STM_FIXED_NUM_UNIT;
        let intensity = EmitIntensity::new(0xFF);

        assert!(p.set(x, y, z, intensity).is_ok());

        assert_eq!(p.buf[0], 0xFFFF);
        assert_eq!(p.buf[1] & 0b01, 0b01);
        assert_eq!(p.buf[1] & 0b10, 0b10);

        assert_eq!(p.buf[1] & 0b1111111111111100, 0b1111111111111000);
        assert_eq!(p.buf[2] & 0b0111, 0b0111);
        assert_eq!(p.buf[2] & 0b1000, 0b1000);

        assert_eq!(p.buf[2] & 0b1111111111110000, 0b1111111111010000);
        assert_eq!(p.buf[3] & 0b011111, 0b011111);
        assert_eq!(p.buf[3] & 0b100000, 0b100000);

        assert_eq!((p.buf[3] >> 6) & 0xFF, 0xFF);

        let x = FOCUS_STM_FIXED_NUM_UNIT * ((1 << (FOCUS_STM_FIXED_NUM_WIDTH - 1)) - 1) as float;
        let y = FOCUS_STM_FIXED_NUM_UNIT * ((1 << (FOCUS_STM_FIXED_NUM_WIDTH - 1)) - 1) as float;
        let z = FOCUS_STM_FIXED_NUM_UNIT * ((1 << (FOCUS_STM_FIXED_NUM_WIDTH - 1)) - 1) as float;
        let intensity = EmitIntensity::new(0);

        assert!(p.set(x, y, z, intensity).is_ok());

        assert!(p
            .set(x + FOCUS_STM_FIXED_NUM_UNIT, y, z, intensity)
            .is_err());
        assert!(p
            .set(x, y + FOCUS_STM_FIXED_NUM_UNIT, z, intensity)
            .is_err());
        assert!(p
            .set(x, y, z + FOCUS_STM_FIXED_NUM_UNIT, intensity)
            .is_err());

        let x = -FOCUS_STM_FIXED_NUM_UNIT * (1 << (FOCUS_STM_FIXED_NUM_WIDTH - 1)) as float;
        let y = -FOCUS_STM_FIXED_NUM_UNIT * (1 << (FOCUS_STM_FIXED_NUM_WIDTH - 1)) as float;
        let z = -FOCUS_STM_FIXED_NUM_UNIT * (1 << (FOCUS_STM_FIXED_NUM_WIDTH - 1)) as float;
        let intensity = EmitIntensity::new(0);

        assert!(p.set(x, y, z, intensity).is_ok());

        assert!(p
            .set(x - FOCUS_STM_FIXED_NUM_UNIT, y, z, intensity)
            .is_err());
        assert!(p
            .set(x, y - FOCUS_STM_FIXED_NUM_UNIT, z, intensity)
            .is_err());
        assert!(p
            .set(x, y, z - FOCUS_STM_FIXED_NUM_UNIT, intensity)
            .is_err());
    }
}
