<? include_once 'auth.php'; ?>
<html>
  <head>
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1, user-scalable=no" />
    <title> 3D Viewer</title>
    <style>
        body {
            margin : 0 auto;
        }
    </style>
  </head>
  <body>
      
    <div id="MyViewerDiv" style = "height:100%; width=100%; position:relative;"></div>
    

    <!-- The Viewer JS -->
    <link rel="stylesheet" href="https://developer.api.autodesk.com/modelderivative/v2/viewers/7.*/style.min.css" type="text/css">
    <script src="https://developer.api.autodesk.com/modelderivative/v2/viewers/7.*/viewer3D.min.js"></script>

    <!-- Developer JS -->
    <script>
    window.scrollTo(0,1);
      var viewer;
      var options = {
        env: 'AutodeskProduction',
        accessToken: <? echo "'". json_decode($data,true)[access_token] . "'" ?>
      };
      var htmlElement = document.getElementById('MyViewerDiv');
      viewer = new Autodesk.Viewing.GuiViewer3D(htmlElement);
      var documentId = 'urn:' + btoa('urn:adsk.objects:os.object:grhdupihb3wabppjuh0h6l3aozeehgfd_fmtesting/Sohar%20International%20Bank.nwd').replace(/=/g, '');
    //   var documentId = 'urn:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6Z3JoZHVwaWhiM3dhYnBwanVoMGg2bDNhb3plZWhnZmRfZm10ZXN0aW5nL1NvaGFyJTIwQmFuay5ud2M=';
    //   var str = 'urn:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6Z3JoZHVwaWhiM3dhYnBwanVoMGg2bDNhb3plZWhnZmRfZm10ZXN0aW5nL1NvaGFyJTIwSW50ZXJuYXRpb25hbCUyMEJhbmtfU3RydWN0dXJlLm53ZA==';
    //   var facade = 'urn:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6Z3JoZHVwaWhiM3dhYnBwanVoMGg2bDNhb3plZWhnZmRfZm10ZXN0aW5nLzM4Mi1BVS0wNS1GQS1CTS1GQUNBREVfMjAxMjE0LTAubndj';
    //   var mashrabya = 'urn:dXJuOmFkc2sub2JqZWN0czpvcy5vYmplY3Q6Z3JoZHVwaWhiM3dhYnBwanVoMGg2bDNhb3plZWhnZmRfZm10ZXN0aW5nL1NvaGFyJTIwSW50ZXJuYXRpb25hbCUyMEJhbmtfTWFzaHJhYml5YXMubndj';
      
      Autodesk.Viewing.Initializer(options, onInitialized);

      function onInitialized() {
           
         viewer.start();
         Autodesk.Viewing.Document.load(documentId, onDocumentLoaded);
        setTimeout(function(){ 
           // Autodesk.Viewing.Document.load(str, onDocumentLoaded);
            console.log('loading data');
        }, 5000);

        
      }


function onDocumentLoaded(doc) {


var defaultModel = doc.getRoot().getDefaultGeometry();
viewer.loadDocumentNode(doc, defaultModel, {
      keepCurrentModels: true,
      globalOffset: { x: 0, y: 0, z: 0 }
    }).then(onLoadModelSuccess);



}

function onDocumentLoaded1(doc) {


var defaultModel = doc.getRoot().getDefaultGeometry();
viewer.loadDocumentNode(doc, defaultModel, {
      keepCurrentModels: true,
      globalOffset: { x: 0, y: 0, z: 0 },
      placementTransform: (new THREE.Matrix4()).makeScale(1)
    }).then(onLoadModelSuccess);



}


function onLoadModelSuccess(model) {
    //console.log('loaded');
    
    /*
    viewer.addEventListener(Autodesk.Viewing.EXTENSION_LOADED_EVENT, function( event ) {
        // remove toolbar
        viewer.toolbar.setVisible(false);
        if(event.extensionId == "Autodesk.BimWalk"){
            
    
    //apply bim walk 
    
    
    //full secren 
    viewer.loadExtension('Autodesk.FullScreen');
    viewer.getExtension('Autodesk.FullScreen').activate();
    
    //set view
    viewer.setViewFromArray([32.88218720911618, 55.742152427803816, 4.057462146799779, 24.617119873608203, 56.74246493363141, 3.8003855858942828, 0, 0, 1, 1.5962059620596205, 1.3089969389957472, 1, 0]);
    viewer.setLightPreset(15);
     viewer.impl.toggleEnvMapBackground(true);
    
    
    //change profile 
    viewer.profile.settings.ambientShadows = false;
    viewer.profile.settings.lightPreset = 15;
    viewer.profile.settings.bimWalkToolPopup = false;
    viewer.setProfile(viewer.profile);
   viewer.getExtension('Autodesk.BimWalk').activate();
    
        }
        //set fov to 75 
     viewer.setFOV(75);
    }
        );
 */

}

function onLoadModelError(viewerErrorCode) {
            console.error('onLoadModelError() - errorCode:' + viewerErrorCode);
            onInitialized();
        }

    </script>

  </body>
</html>