//              ______    _____            _________       _____   _____
//            /     /_  /    /            \___    /      /    /__/    /
//           /        \/    /    ___        /    /      /            /    ___
//          /     / \      /    /\__\      /    /___   /    ___     /    /   \
//        _/____ /   \___ /    _\___     _/_______ / _/___ / _/___ /    _\___/\_
//        revised on 2/12/2016  All rights reserved by @NeZha

'use strict'

const THREE = require( './third-party/three/build/three.js' )
const Stats = require( './third-party/three/examples/js/libs/stats.min.js' )

let Loading = require( './example/kinect.js' )
let example = new Loading()
let renderer, stats


init();
animate();

function init() {

    renderer = new THREE.WebGLRenderer()
    renderer.setPixelRatio( window.devicePixelRatio )
    renderer.setSize( window.innerWidth, window.innerHeight )

    document.getElementById( 'webgl-output' ).appendChild( renderer.domElement )

    // Stats initialzation
    stats = new Stats()
    document.getElementById( 'stats-output' ).appendChild( stats.dom )

    // Window event listeners
    window.addEventListener( 'resize', onWindowResize, false )

    example = new Loading()
    example.init()
    document.getElementById( 'loading' ).style.display = 'none'
}



function animate() {

    requestAnimationFrame( animate )
    if ( !example ) return

    example.onRender()
    renderer.render( example.scene, example.camera )
    stats.update()

}

function onWindowResize() {

    renderer.setSize( window.innerWidth, window.innerHeight )
    if ( !example ) return

    example.camera.aspect = window.innerWidth / window.innerHeight
    example.camera.updateProjectionMatrix()

}


// Console script
var cnsl = new Console( {}, {
    hotkey: 192 // <kbd>ESC</kbd> ('~' for default)
    , welcome: 'Welcome back, Master.Nezha.'
    , caseSensitive: true
    , defaultHandler: function() {}
    , onShow: function() {}
    , onHide: function() {}
} )

cnsl.register( 'help', function () {
    let cmds = cnsl.commands
    for ( let name in cmds ) {
        if ( cmds.hasOwnProperty( name ) ) 
            cmds[ name ].desc && cnsl.log( ' -', cmds[name].usage + ':', cmds[name].desc )
    }
}, {
    usage: 'HELP'
    , desc: 'Show help messages.'
} ).register( 'command', function ( command ) {
    eval( command )
}, {
    usage: `COMMAND <command>`
    , desc: 'Execute javascript interactively.'
} ).register( 'clear', function() {
    example = new Loading()
    example.init()
    console.log( 'clear success' )
}, {
    usage: 'CLEAR'
    , desc: 'Clear current scene and pop back to defualt.'
} )
@@ -601,78 +601,15 @@
 let 
-Loading = require( './example/kinect.js' )%0Alet example = new Loading()
+example
 %0Alet
@@ -674,34 +674,14 @@
     
-renderer = new THREE.WebGL
+setNew
 Rend
@@ -691,128 +691,74 @@
 r()%0A
+%0A
     
-renderer.setPixelRatio( window.devicePixelR
+// Stats initialz
 atio
- )
+n
 %0A    
-renderer.setSize( window.innerWidth, window.innerHeight )%0A%0A   
+stats = new Stats()%0A    let node =
  doc
@@ -784,13 +784,13 @@
 d( '
-webgl
+stats
 -out
@@ -799,141 +799,81 @@
 t' )
-.appendChild( renderer.domElement )%0A%0A    // Stats initialzation%0A    stats = new Stats()%0A    document.getElementById( 'stats-output' )
+%0A    while ( node.firstChild ) node.removeChild(node.firstChild)%0A    node
 .app
@@ -985,24 +985,76 @@
 e, false )%0A%0A
+    let Loading = require( './example/loading.js' )%0A
     example 
@@ -1082,24 +1082,34 @@
 xample.init(
+ renderer 
 )%0A    docume
@@ -1165,16 +1165,461 @@
 one'%0A%7D%0A%0A
+function setNewRenderer() %7B%0A    // Open antialias cause a significant reduction in performance%0A    renderer = new THREE.WebGLRenderer( %7B antialias: false %7D )  %0A    renderer.setPixelRatio( window.devicePixelRatio )%0A    renderer.setSize( window.innerWidth, window.innerHeight )%0A%0A    let node = document.getElementById( 'webgl-output' )%0A    while ( node.firstChild ) node.removeChild(node.firstChild)%0A    node.appendChild( renderer.domElement )%0A%7D%0A%0A
 %0A%0Afuncti
@@ -2684,17 +2684,17 @@
 ge: 
-%60
+'
 COMMAND 
 %3Ccom
@@ -2693,18 +2693,24 @@
 AND 
-%3C
+&lt;
 command
-%3E%60
+&gt;'
 %0A   
@@ -2798,44 +2798,8 @@
     
-example = new Loading()%0A    example.
 init
@@ -2918,12 +2918,409 @@
 efualt.'%0A%7D )
+.register( 'load', function( name ) %7B%0A%0A    document.getElementById( 'loading' ).style.display = 'block'%0A    setNewRenderer()%0A    let Foo = require( %60./example/$%7Bname%7D.js%60 )%0A    example = new Foo()%0A    example.init( renderer )%0A    document.getElementById( 'loading' ).style.display = 'none'%0A%0A%7D, %7B%0A    usage: %60LOAD &lt;example&gt;%60%0A    , desc: 'Load example file under Crystal/example directory'%0A%7D )

add example