/*
 * LICENCE : CloudUnit is available under the Affero Gnu Public License GPL V3 : https://www.gnu.org/licenses/agpl-3.0.html
 *     but CloudUnit is licensed too under a standard commercial license.
 *     Please contact our sales team if you would like to discuss the specifics of our Enterprise license.
 *     If you are not sure whether the GPL is right for you,
 *     you can always test our software under the GPL and inspect the source code before you contact us
 *     about purchasing a commercial license.
 *
 *     LEGAL TERMS : "CloudUnit" is a registered trademark of Treeptik and can't be used to endorse
 *     or promote products derived from this project without prior written permission from Treeptik.
 *     Products or services derived from this software may not be called "CloudUnit"
 *     nor may "Treeptik" or similar confusing terms appear in their names without prior written permission.
 *     For any questions, contact us : contact@treeptik.fr
 */

(function () {
  'use strict';
  angular.module ( 'webuiApp' )
    .factory ( 'ExplorerService', ExplorerService );

  ExplorerService.$inject = [
    '$resource',
    '$http'
  ];


  function ExplorerService ( $resource, $http ) {

    return {
      buildTree: buildTree,
      //downloadFile: downloadFile,
      deleteFile: deleteFile,
      addDirectory: addDirectory,
      unzipFile: unzipFile,
      editFile: editFile,
      getFile: getFile
    };



    ////////////////////////////////////////////////////////////////////

    // Liste l'arborescence de fichier d'un container
    function buildTree ( containerId, path ) {
      var dir = $resource ( '/file/container/:containerId/path/:path' );
      return dir.query ( {
        containerId: containerId,
        path: path
      } ).$promise;
    }
       
   function unzipFile ( containerId, applicationName, path, item) {
      var file = $resource ( 'file/unzip/container/:containerId/application/:applicationName/path/:path/fileName/:item',
      {
        containerId: containerId,
        applicationName: applicationName,
        path: path, 
        item: item
      },
      { 'update': { method: 'PUT',
          transformResponse: function ( data, headers ) {
            var response = {};
            response.data = data;
            response.headers = headers ();
            return response;
          } }
      });

      return file.update ().$promise;
    }
     
    function getFile ( containerId, applicationName, path, fileName ) {     
      var file = $resource ( '/file/content/container/:containerId/application/:applicationName/path/:path/fileName/:fileName', {
        containerId: containerId,
        applicationName: applicationName,
        path: path,
        fileName: fileName
      }, {
        get: {
          method: 'GET',
          transformResponse: function ( data, headers ) {
            var response = {};
            response.data = data;
            response.headers = headers ();
            return response;
          }
        }
      } );

      return file.get ().$promise;
    }
     
    function editFile ( containerId, applicationName, path, fileName, fileContent ) { 
      console.log(fileContent);
      var data, headers;
            headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json, text/plain, */*'
      };
      data = "fileContent=" + fileContent;
      return $http.post('file/content/container/' + containerId + '/application/' + applicationName + '/path/'+path + '/fileName/' + fileName, data, {
        headers: headers
      });
      
      /*var file = $resource ( 'file/content/container/:containerId/application/:applicationName/path/:path/fileName/:fileName',
      {
        containerId: containerId,
        applicationName: applicationName,
        path: path,
        fileName: fileName
      },{ test: {method:'POST', params: { fileContent: fileContent}}});

      return file.test ( ).$promise;*/
    }
       
    function deleteFile ( containerId, applicationName, path ) {
      var file = $resource ( '/file/container/:containerId/application/:applicationName/path/:path' );

      return file.delete ( {
        containerId: containerId,
        applicationName: applicationName,
        path: path
      } ).$promise;
    }

    function addDirectory ( containerId, applicationName, path ) {
      var request = $resource ( '/file/container/:containerId/application/:applicationName/path/:path', {
        containerId: containerId,
        applicationName: applicationName,
        path: path
      } );
      
      return request.save ().$promise;
    }
    
    /*function downloadFile ( containerId, applicationName, path, fileName ) {
      var file = $resource ( '/file/container/:containerId/application/:applicationName/path/:path/fileName/:fileName', {
        containerId: containerId,
        applicationName: applicationName,
        path: path,
        fileName: fileName
      }, {
        get: {
          method: 'GET',
          transformResponse: function ( data, headers ) {
            var response = {};
            response.data = data;
            response.headers = headers ();
            return response;
          }
        }
      } );

      return file.get ().$promise;

    }*/
  }
}) ();







@@ -1140,21 +1140,8 @@
 rce'
-,%0A    '$http'
 %0A  %5D
@@ -1186,15 +1186,8 @@
 urce
-, $http
  ) %7B
@@ -3159,451 +3159,15 @@
     
-console.log(fileContent);%0A      var data, headers;%0A            headers = %7B%0A        'Content-Type': 'application/x-www-form-urlencoded',%0A        'Accept': 'application/json, text/plain, */*'%0A      %7D;%0A      data = %22fileContent=%22 + fileContent;%0A      return $http.post('file/content/container/' + containerId + '/application/' + applicationName + '/path/'+path + '/fileName/' + fileName, data, %7B%0A        headers: headers%0A      %7D);%0A      %0A      /*
+%0A      
 var 
@@ -3424,20 +3424,22 @@
     %7D,%7B 
+upda
 te
-st
 : %7Bmetho
@@ -3507,20 +3507,22 @@
 rn file.
+upda
 te
-st
  ( ).$pr
@@ -3527,18 +3527,16 @@
 promise;
-*/
 %0A    %7D%0A 

Replace with in explorer service