use std::{
    fs::OpenOptions,
    os::{
        fd::AsRawFd,
        yggdrasil::{io::mapping::FileMapping, rt::io::device},
    },
    path::Path,
};

use crate::error::Error;

pub struct Display<'a> {}

pub struct Point<T>(pub T, pub T);

impl Display<'_> {
    pub fn fill(&mut self, color: u32) {
        self.data.fill(color);
    }

    pub fn row(&mut self, y: usize) -> &mut [u32] {
        &mut self.data[y * self.width..y * self.width + self.width]
    }

    pub fn copy_from_slice(&mut self, slice: &[u32]) {
        if slice.len() != self.data.len() {
            panic!("Invalid copy source size");
        }

        self.data.copy_from_slice(slice);
    }

    pub fn blit_buffer(
        &mut self,
        source: &[u32],
        dst: Point<usize>,
        src: Point<usize>,
        w: usize,
        h: usize,
        src_stride: usize,
    ) {
        for y in 0..h {
            let dst_offset = (y + src.1 + dst.1) * self.width + dst.0 + src.0;
            let src_offset = (y + src.1) * src_stride + src.0;

            let src_chunk = &source[src_offset..src_offset + w];
            let dst_chunk = &mut self.data[dst_offset..dst_offset + w];

            dst_chunk.copy_nonoverlapping(src_chunk);
        }

        self.flush();
    }
}
