import Login from "./Login"
import Vector3 from "./Vector3"
/**
 * @typedef {Object} User The user object
 * @property {string} Name Name of the user
 * @property Login Login user login info
 * @property Vector3 Vector3 last logout position of user
 */
class User {
    /**
     * Name of the user
     * @return {string} gets the value of Name
     */
    get Name() {
        return this._decoder.decode(new Uint8Array(this._ptr.slice(0, 24)));
    }
    /**
     * Name of the user
     * @param {string} v sets the value of Name
     */    
    set Name(v) {
        this._data.set(this._encoder.encode(v), 0);
    }
    /**
     * user login info
     * @return {Login} gets the value of Login
     */
    get Login() {
        return new Login({}, new Uint8Array(this._ptr.slice(24, 344)));
    }
    /**
     * user login info
     * @param {Login} sets the value of Login
     */
    set Login(v) {
        this._data.set(v.bytes(), 24);
    }
    /**
     * last logout position of user
     * @return {Vector3} gets the value of Position
     */
    get Position() {
        return new Vector3({}, new Uint8Array(this._ptr.slice(344, 356)));
    }
    /**
     * last logout position of user
     * @param {Vector3} sets the value of Position
     */
    set Position(v) {
        this._data.set(v.bytes(), 344);
    }
    /** 
     * Get the struct representation of the object
     * @return {Uint8Array} u8 array of the C struct.
     */
    get bytes() {
        return new Uint8Array(this._ptr);
    }
    /**
     * Constructs a new User
     * 
     * @param {{Name: string, Login: Login, Position: Vector3, }} init The arguments to construct the object.
     * @param {ArrayBuffer} ptr The pointer to the C struct.
     */
    constructor(init = {}, ptr = undefined) {
        this._size = 356;
        this._ptr = ptr?.buffer || new ArrayBuffer(this._size);
        this._data = new DataView(this._ptr);
        
        this._encoder = new TextEncoder(); 
        this._decoder = new TextDecoder();          
        for (const key of Object.keys(init)) {
          this[key] = init[key];
        }
    }
}

export default User