/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/

use crate::{Object, ObjectBuffer};

pub trait Value<'a>: ValueStore + Sized {
    const ALIGNED: bool;
    fn load_aligned(buf: &'a [u32]) -> Option<Self> {
        let _ = buf;
        None
    }
    fn load_unaligned(buf: &'a [u8]) -> Option<Self> {
        let _ = buf;
        None
    }
}
pub trait ValueStore {
    fn is_aligned(&self) -> bool;
    fn store_aligned(&self, _buf: &mut Vec<u32>) {}
    fn store_unaligned(&self, _buf: &mut Vec<u8>) {}
    fn size(&self) -> usize;
}
impl<'a> Value<'a> for &'a str {
    const ALIGNED: bool = false;
    fn load_unaligned(buf: &'a [u8]) -> Option<Self> {
        str::from_utf8(buf).ok()
    }
}
impl ValueStore for &str {
    fn is_aligned(&self) -> bool {
        false
    }
    fn store_unaligned(&self, buf: &mut Vec<u8>) {
        buf.extend(self.as_bytes());
    }
    fn size(&self) -> usize {
        self.len()
    }
}
impl Value<'_> for u32 {
    const ALIGNED: bool = true;
    fn load_aligned(buf: &[u32]) -> Option<Self> {
        buf.get(0).copied().map(u32::from_be)
    }
}
impl ValueStore for u32 {
    fn is_aligned(&self) -> bool {
        true
    }
    fn store_aligned(&self, buf: &mut Vec<u32>) {
        buf.push(self.to_be());
    }
    fn size(&self) -> usize {
        4
    }
}
impl Value<'_> for u64 {
    const ALIGNED: bool = false;
    fn load_aligned(buf: &[u32]) -> Option<Self> {
        let hi = u32::from_be(*buf.get(0)?) as u64;
        let lo = u32::from_be(*buf.get(1)?) as u64;
        Some(hi << 32 | lo)
    }
}
impl ValueStore for u64 {
    fn is_aligned(&self) -> bool {
        true
    }
    fn store_aligned(&self, buf: &mut Vec<u32>) {
        buf.push(((self >> 32) as u32).to_be());
        buf.push((*self as u32).to_be());
    }
    fn size(&self) -> usize {
        8
    }
}
impl<'a> Value<'a> for Object<'a> {
    const ALIGNED: bool = true;
    fn load_aligned(buf: &'a [u32]) -> Option<Self> {
        Self::load(buf)
    }
}
impl ValueStore for Object<'_> {
    fn is_aligned(&self) -> bool {
        true
    }
    fn store_aligned(&self, buf: &mut Vec<u32>) {
        buf.extend(self.tags);
        buf.extend(self.offsets);
        buf.extend(self.values);
    }
    fn size(&self) -> usize {
        (self.tags.len() + self.offsets.len() + self.values.len()) * size_of::<u32>()
    }
}
impl ValueStore for ObjectBuffer {
    fn is_aligned(&self) -> bool {
        true
    }
    fn store_aligned(&self, buf: &mut Vec<u32>) {
        buf.extend(&self.0);
    }
    fn size(&self) -> usize {
        self.0.len() * 4
    }
}
impl<'a> Value<'a> for &'a [u8] {
    const ALIGNED: bool = false;
    fn load_unaligned(buf: &'a [u8]) -> Option<Self> {
        Some(buf)
    }
}
impl ValueStore for &[u8] {
    fn is_aligned(&self) -> bool {
        false
    }
    fn store_unaligned(&self, buf: &mut Vec<u8>) {
        buf.extend(*self);
    }
    fn size(&self) -> usize {
        self.len()
    }
}
