use super::number::Number;

#[derive(Clone,Copy)]
pub struct UDim(rbx_types::UDim);
impl UDim{
	pub fn new(scale:f32,offset:i32)->Self{
		UDim(rbx_types::UDim::new(scale,offset))
	}
}
impl From<rbx_types::UDim> for UDim{
	fn from(value:rbx_types::UDim)->Self{
		Self(value)
	}
}

pub fn set_globals(lua:&mlua::Lua,globals:&mlua::Table)->Result<(),mlua::Error>{
	let table=lua.create_table()?;

	table.raw_set("new",
		lua.create_function(|_,(scale,offset):(Number,i32)|
			Ok(UDim::new(scale.into(),offset))
		)?
	)?;

	globals.set("UDim",table)?;

	Ok(())
}

impl mlua::UserData for UDim{
	fn add_fields<F:mlua::UserDataFields<Self>>(fields:&mut F){
		fields.add_field_method_get("Scale",|_,UDim(this)|Ok(this.scale));
		fields.add_field_method_get("Offset",|_,UDim(this)|Ok(this.offset));
	}
}
type_from_lua_userdata!(UDim);
