use palette::{Pixel, Srgb};

// 9 Channel mode

pub struct MovingHead9CH {
    start_addr: usize,

    pub pan: u8,
    pub tilt: u8,
    pub dimmer: u8, // 0-7 off, 8-134 dim, 135-239 strobe, 240-255 "switch"?
    pub rgb: Srgb,
    pub w: u8,
    pub speed: u8, // reversed
    pub rst: u8,   // 150-200
}

impl MovingHead9CH {
    pub fn new(start_addr: usize) -> Self {
        Self {
            start_addr,
            pan: 0,
            tilt: 0,
            dimmer: 0,
            rgb: Srgb::new(0.0, 0.0, 0.0),
            w: 0,
            speed: 0,
            rst: 0,
        }
    }

    pub fn render(&self, destination: &mut [u8]) {
        let [r, g, b]: [u8; 3] = self.rgb.into_format().into_raw();

        destination[self.start_addr - 1..self.start_addr - 1 + 9].copy_from_slice(&[
            self.pan,
            self.tilt,
            self.dimmer,
            r,
            g,
            b,
            self.w,
            self.speed,
            self.rst,
        ])
    }
}

// 14 Channel mode

pub struct MovingHead14CH {
    start_addr: usize,

    pub pan: u8,
    pub pan_fine: u8,
    pub tilt: u8,
    pub tilt_fine: u8,
    pub speed: u8,
    pub dimmer: u8,
    pub rgb: Srgb,
    pub w: u8,
    pub color_mode: u8,
    pub color_auto_jump_speed: u8,
    pub control_mode: u8,
    pub rst: u8,
}

impl MovingHead14CH {
    pub fn new(start_addr: usize) -> Self {
        Self {
            start_addr,

            pan: 0,
            pan_fine: 0,
            tilt: 0,
            tilt_fine: 0,
            speed: 0,
            dimmer: 0, // 0-7 off, 8-134 dim, 135-239 strobe, 240-255 "switch"?
            rgb: Srgb::new(0.0, 0.0, 0.0),
            w: 0,
            color_mode: 0, // 0-7 "select color" -> dmx?, 8-231 "built in"?, 232-255 "seven colors jumping"
            color_auto_jump_speed: 0, // speed for "seven colors jumping" mode
            control_mode: 0, /* 0-7 "custom control" -> dmx?, 8-63 "working in fast", 64-127 "working in slow"
                             128-191 "Sound1Active", 192-255 "Sound2Active" */
            rst: 0, // 150-200
        }
    }

    pub fn render(&self, destination: &mut [u8]) {
        let [r, g, b]: [u8; 3] = self.rgb.into_format().into_raw();

        destination[self.start_addr - 1..self.start_addr - 1 + 14].copy_from_slice(&[
            self.pan,
            self.pan_fine,
            self.tilt,
            self.tilt_fine,
            self.speed,
            self.dimmer,
            r,
            g,
            b,
            self.w,
            self.color_mode,
            self.color_auto_jump_speed,
            self.control_mode,
            self.rst,
        ]);
    }
}
