/*global CryptoJS*/
'use strict';

angular.module('squareteam.api')
  .service('ApiCrypto', function Apicrypto(Currentuser, appConfig) {

    this.generateToken = function(login, password, salt1, salt2) {
      var pbkdf2  = CryptoJS.PBKDF2(password, salt1, { keySize: 256/32, iterations: 1000, hasher : CryptoJS.algo.SHA256 }),
          hmac    = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, salt2.concat(pbkdf2));

      hmac.update(login);
      return hmac.finalize(); // return token as WordArray
    };
      /**
       * Return secure headers
       * @param  {ApiAuth} auth
       * @param  {Object}  request
       * @return {Object}
       */
    this.generateHeaders = function(auth, url, method, data) {
      var headers,
          hmac,
          path,
          blob = [];

      path = url.replace(appConfig.api.host, '');

      if (path[0] !== '/') { // FIXME : remove ? better patch ?
        path = '/' + path;
      }

      if (data && angular.isObject(data)) {
        angular.forEach(Object.keys(data).sort(), function(key) {
          blob.push( key + '=' + encodeURIComponent(data[key]) );
        });
      }

      headers = {
        'St-Identifier' : auth.identifier,
        'St-Timestamp'  : Math.round(Date.now() / 1000)
      };

      hmac = CryptoJS.algo.HMAC.create(
       CryptoJS.algo.SHA256,
       auth.token
      );

      // console.group('-- generateHeaders --');

      // console.log('method',     method);
      // console.log('path',       path);
      // console.log('headers',    headers);
      // console.log('blob',       blob);
      // console.log('token',      auth.token.toString());
      // console.log('identifier', auth.identifier);

      // console.groupEnd();

      hmac.update(method + ':');
      hmac.update(path + ':');
      hmac.update(headers['St-Timestamp'] + ':');
      hmac.update(blob.join('&'));

      headers['St-Hash'] = hmac.finalize().toString();

      return headers;
    };

    this.transformRequest = function(config) {

      var auth = Currentuser.getAuth();

      if (!auth.isValid()) {
        throw new Error('Cannot load ' + config.url + ' without being logged in !');
      }

      angular.extend(config.headers, this.generateHeaders(auth, config.url, config.method, config.data));

      return config;
    };

  });

@@ -869,44 +869,9 @@
 ') %7B
- // FIXME : remove ? better patch ?
 %0A
+
     
@@ -1327,374 +1327,8 @@
 );%0A%0A
-      // console.group('-- generateHeaders --');%0A%0A      // console.log('method',     method);%0A      // console.log('path',       path);%0A      // console.log('headers',    headers);%0A      // console.log('blob',       blob);%0A      // console.log('token',      auth.token.toString());%0A      // console.log('identifier', auth.identifier);%0A%0A      // console.groupEnd();%0A%0A
     
@@ -1469,24 +1469,24 @@
 oin('&'));%0A%0A
-
       header
@@ -1525,24 +1525,79 @@
 oString();%0A%0A
+      headers%5B'X-Requested-With'%5D = 'XMLHttpRequest';%0A%0A
       return

add X-Requested-With header