#!/bin/js

// Function to handle getattr command
function getattr(path,opts){
  let size = 1
  if (path === '/' || opts.obj ){
    return "16877 2 0 0 4096 8\n"
  } else {
    return `33188 1 0 0 ${size} 1\n`
  } 
}

function getPath(str){
   const scene = document.querySelector('a-scene').object3D  
   const path = str.split("/") 
   const name = path.pop()
   const parent = path.pop()
   let obj =  name == "" 
              ? scene
              : scene.getObjectByName( name ) ||
                scene.getObjectById( parseInt(name.substr(1)) )
console.dir({parent,name,obj})
  
   return {obj,propname:name}
}

// Main function to handle commands
function main(args) {
  console.log( 'fs/THREE '+args.join(' ') )
  let children = ''
  let props = ['uuid','position','rotation']

  
  switch (args[0]) {
    case 'readdir':{
      const {obj} = getPath( args[1] )
      if( obj && obj.children ){
        children = obj.children.map( (n) => n.name || n.id )
          .join("\n")                   
      }
      return `${children}\n${props.join('\n')}\n`  
      break;
    }
      
    case 'getattr':{
      const opts = getPath( args[1] )
      return getattr(args[1],{...opts, args});
      break;
    }
      
    case 'read':{
      const {obj,propname} = getPath( args[1] )
      return obj[propname];
      break; 
    }
      
    default:
      return ''
  }
}

// Run the main function
return main(args.slice(1));