use super::udim::UDim;
use super::number::Number;

#[derive(Clone,Copy)]
pub struct UDim2(rbx_types::UDim2);
impl UDim2{
	pub fn new(sx:f32,ox:i32,sy:f32,oy:i32)->Self{
		UDim2(rbx_types::UDim2::new(
			rbx_types::UDim::new(sx,ox),
			rbx_types::UDim::new(sy,oy),
		))
	}
}
impl From<UDim2> for rbx_types::UDim2{
	fn from(UDim2(value):UDim2)->rbx_types::UDim2{
		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(|_,(sx,ox,sy,oy):(Number,i32,Number,i32)|
			Ok(UDim2::new(sx.into(),ox,sy.into(),oy))
		)?
	)?;

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

	Ok(())
}

impl mlua::UserData for UDim2{
	fn add_fields<F:mlua::UserDataFields<Self>>(fields:&mut F){
		fields.add_field_method_get("X",|_,UDim2(this)|Ok(UDim::from(this.x)));
		fields.add_field_method_get("Y",|_,UDim2(this)|Ok(UDim::from(this.y)));
	}
}
type_from_lua_userdata!(UDim2);
