/*
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
*/

(function(document) {
  'use strict';

  // Grab a reference to our auto-binding template
  // and give it some initial binding values
  // Learn more about auto-binding templates at http://goo.gl/Dx1u2g
  var app = document.querySelector('#app');
  
  // Sets app default base URL
  app.baseUrl = '/';
  if (window.location.port === '') {  // if production
    // Uncomment app.baseURL below and
    // set app.baseURL to '/your-pathname/' if running from folder in production
    // app.baseUrl = '/polymer-starter-kit/';
  }

  app.displayInstalledToast = function() {
    // Check to make sure caching is actually enabled—it won't be in the dev environment.
    if (!Polymer.dom(document).querySelector('platinum-sw-cache').disabled) {
      Polymer.dom(document).querySelector('#caching-complete').show();
    }
  };

  // Listen for template bound event to know when bindings
  // have resolved and content has been stamped to the page
  app.addEventListener('dom-change', function() {
    console.log('Our app is ready to rock!');
  });
  
  // See https://github.com/Polymer/polymer/issues/1381
  window.addEventListener('WebComponentsReady', function() {
    var setting = document.querySelector('#setting');
    if (setting){
      // setting.set('globals.baseUrl', 'http://192.168.35.96:8001/rest');
      setting.set('globals.baseUrl', 'http://factory.hatiolab.com:8001/rest');
    }
  });

  // Main area's paper-scroll-header-panel custom condensing transformation of
  // the appName in the middle-container and the bottom title in the bottom-container.
  // The appName is moved to top and shrunk on condensing. The bottom sub title
  // is shrunk to nothing on condensing.
  window.addEventListener('paper-header-transform', function(e) {
    var appName = Polymer.dom(document).querySelector('#mainToolbar .app-name');
    var middleContainer = Polymer.dom(document).querySelector('#mainToolbar .middle-container');
    var bottomContainer = Polymer.dom(document).querySelector('#mainToolbar .bottom-container');
    var detail = e.detail;
    var heightDiff = detail.height - detail.condensedHeight;
    var yRatio = Math.min(1, detail.y / heightDiff);
    // appName max size when condensed. The smaller the number the smaller the condensed size.
    var maxMiddleScale = 0.50;
    var auxHeight = heightDiff - detail.y;
    var auxScale = heightDiff / (1 - maxMiddleScale);
    var scaleMiddle = Math.max(maxMiddleScale, auxHeight / auxScale + maxMiddleScale);
    var scaleBottom = 1 - yRatio;

    // Move/translate middleContainer
    Polymer.Base.transform('translate3d(0,' + yRatio * 100 + '%,0)', middleContainer);


    // Scale middleContainer appName
    Polymer.Base.transform('scale(' + scaleMiddle + ') translateZ(0)', appName);
  });

  function startScan() {
    var unitHome = app.$["new-unit-home"];
    page("/unit-list");
    cordova.plugins.barcodeScanner.scan(
        function (result) {
            var prefix = result.text.split('/');
            if(prefix.length > 1) {
              this.sendAjax(prefix[1], prefix[2], result.text);
            } else {
              unitHome.open();
            }
        }, 
        function (error) {
            alert("Scanning failed: " + error);
        }
    );
  }

  function sendAjax(page, unitId, path) {
    var unitHome = this.$["new-unit-home"];
    var myRequest = new XMLHttpRequest();
    myRequest.onreadystatechange = function(){
      if(myRequest.readyState == 4 && (myRequest.status == 200 || myRequest.status == 201)){
        var res = myRequest.responseText;
        if(res == null) {
          unitHome.open();
        } else {
          location.href="/" + path;
          // document.querySelector("coffer-app").route.path = path;
          // document.querySelector("coffer-app").page = page;
        }
      } 
    };

    var setting = document.querySelector('#setting');
    myRequest.withCredentials = true;
    myRequest.open('get', setting.get('globals.baseUrl') + '/units/' + unitId,true);
    myRequest.send(null);
  }

})(document);
    // Scale bottomContainer and bottom sub title to nothing and back
    Polymer.Base.transform('scale(' + scaleBottom + ') translateZ(0)', bottomContainer);