use serde_json::{Map, Value, json};
use crate::{AgentId, Brightness, RgbColor, Speed};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
#[non_exhaustive]
pub enum LightingEffect {
Off = 0,
Solid = 1,
Snake = 2,
Rainbow = 3,
Breath = 4,
Gradient = 5,
ShallowBreath = 6,
}
impl LightingEffect {
pub(crate) const fn code(self) -> u8 {
self as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LightingZone {
effect: LightingEffect,
brightness: Brightness,
speed: Speed,
magic: u32,
color: RgbColor,
}
impl LightingZone {
#[must_use]
pub fn new(effect: LightingEffect, color: RgbColor) -> Self {
Self {
effect,
brightness: Brightness::default(),
speed: Speed::default(),
magic: 1,
color,
}
}
#[must_use]
pub const fn with_brightness(mut self, brightness: Brightness) -> Self {
self.brightness = brightness;
self
}
#[must_use]
pub const fn with_speed(mut self, speed: Speed) -> Self {
self.speed = speed;
self
}
#[must_use]
pub const fn with_magic(mut self, magic: u32) -> Self {
self.magic = magic;
self
}
pub(crate) fn short_json(self) -> Value {
json!({
"e": self.effect.code(),
"b": self.brightness.get(),
"s": self.speed.get(),
"m": self.magic,
"c": self.color.get(),
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct BaseLighting {
keys: Option<LightingZone>,
ambient: Option<LightingZone>,
}
impl BaseLighting {
#[must_use]
pub const fn new() -> Self {
Self {
keys: None,
ambient: None,
}
}
#[must_use]
pub const fn with_keys(mut self, zone: LightingZone) -> Self {
self.keys = Some(zone);
self
}
#[must_use]
pub const fn with_ambient(mut self, zone: LightingZone) -> Self {
self.ambient = Some(zone);
self
}
#[must_use]
pub const fn is_empty(self) -> bool {
self.keys.is_none() && self.ambient.is_none()
}
pub(crate) fn to_json(self) -> Value {
let mut object = Map::new();
if let Some(keys) = self.keys {
object.insert("keys".to_owned(), keys.short_json());
}
if let Some(ambient) = self.ambient {
object.insert("ambient".to_owned(), ambient.short_json());
}
Value::Object(object)
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ThreadLighting {
id: AgentId,
color: RgbColor,
brightness: Brightness,
effect: LightingEffect,
speed: Speed,
sync_keys: bool,
sync_ambient: bool,
}
impl ThreadLighting {
#[must_use]
pub fn new(id: AgentId, color: RgbColor) -> Self {
Self {
id,
color,
brightness: Brightness::default(),
effect: LightingEffect::Solid,
speed: Speed::default(),
sync_keys: false,
sync_ambient: false,
}
}
#[must_use]
pub const fn with_brightness(mut self, brightness: Brightness) -> Self {
self.brightness = brightness;
self
}
#[must_use]
pub const fn with_effect(mut self, effect: LightingEffect) -> Self {
self.effect = effect;
self
}
#[must_use]
pub const fn with_speed(mut self, speed: Speed) -> Self {
self.speed = speed;
self
}
#[must_use]
pub const fn with_key_sync(mut self, enabled: bool) -> Self {
self.sync_keys = enabled;
self
}
#[must_use]
pub const fn with_ambient_sync(mut self, enabled: bool) -> Self {
self.sync_ambient = enabled;
self
}
pub(crate) fn to_json(self) -> Value {
json!({
"id": self.id.get(),
"c": self.color.get(),
"b": self.brightness.get(),
"e": self.effect.code(),
"s": self.speed.get(),
"sk": u8::from(self.sync_keys),
"sa": u8::from(self.sync_ambient),
})
}
}