// use mlua::prelude::*;
use mlua::{FromLua, Lua, UserData, UserDataMethods};

use crate::renderer::prelude::*;

#[derive(Debug, Clone, Copy)]
pub struct Config {
    pub width: u32,
    pub height: u32,
    pub background_color: Color,
    pub fps: f64,
    pub num_layers: usize,
}

impl Default for Config {
    fn default() -> Self {
        Config {
            width: 1920,
            height: 1080,
            background_color: Color::new(0, 0, 0, 255),
            fps: 30.0,
            num_layers: 10,
        }
    }
}

impl FromLua for Config {
    fn from_lua(value: mlua::Value, _: &Lua) -> mlua::Result<Self> {
        match value {
            mlua::Value::UserData(ud) => Ok(*ud.borrow::<Self>()?),
            _ => unreachable!(),
        }
    }
}

impl UserData for Config {
    fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
        methods.add_method_mut("set_size", |_, config, (width, height): (u32, u32)| {
            config.width = width;
            config.height = height;
            Ok(())
        });

        methods.add_method_mut("set_background_color", |_, config, color: Color| {
            config.background_color = color;
            Ok(())
        });
        methods.add_method_mut("set_fps", |_, config, fps: f64| {
            config.fps = fps;
            Ok(())
        });
        methods.add_method_mut("set_num_layers", |_, config, num_layers: usize| {
            config.num_layers = num_layers;
            Ok(())
        });
    }
}

// pub fn add_constructor(lua: &Lua) -> mlua::Result<()> {
//     lua.globals().set("Config", Config::default())?;
//     Ok(())
// }
