import angular from 'angular';

import uiRouter from 'angular-ui-router';

import view from './view';

//====================================================================

export default angular.module('xoWebApp.pool', [
  uiRouter,
])
  .config(function ($stateProvider) {
    $stateProvider.state('pools_view', {
      url: '/pools/:id',
      controller: 'PoolCtrl',
      template: view,
    });
  })
  .controller('PoolCtrl', function ($scope, $stateParams, xoApi, xo, modal) {
    $scope.$watch(() => xoApi.get($stateParams.id), function (pool) {
      $scope.pool = pool;
    });

    $scope.savePool = function ($data) {
      let {pool} = $scope;
      let {name_label, name_description} = $data;

      $data = {
        id: pool.UUID,
      }
      if (name_label !== pool.name_label) {
        $data.name_label = name_label;
      }
      if (name_description !== pool.name_description) {
        $data.name_description = name_description;
      }

      xoApi.call('pool.set', $data);
    };

    $scope.deleteAllLog = function () {
      return modal.confirm({
        title: 'Log deletion',
        message: 'Are you sure you want to delete all the logs?',
      }).then(function () {
        // TODO: return all promises.
        for (let log in $scope.pool.messages) {
          console.log('Remove log', log);
          xo.log.delete(log);
        }
      });
    };

    $scope.deleteLog = function (id) {
      console.log('Remove log', id);
      return xo.log.delete(id);
    };

    // $scope.patchPool = ($files, id) ->
    //   file = $files[0]
    //   xo.pool.patch id
    //   .then ({ $sendTo: url }) ->
    //     return $upload.http {
    //       method: 'POST'
    //       url
    //       data: file
    //     }
    //     .progress throttle(
    //       (event) ->
    //         percentage = (100 * event.loaded / event.total)|0

    //         notify.info
    //           title: 'Upload patch'
    //           message: "#{percentage}%"
    //       6e3
    //     )
    //   .then (result) ->
    //     throw result.status if result.status isnt 200
    //     notify.info
    //       title: 'Upload patch'
    //       message: 'Success'

  })

  // A module exports its name.
  .name
;

@@ -1246,24 +1246,24 @@
     
-for (let log in 
+angular.forEach(
 $sco
@@ -1282,81 +1282,106 @@
 ages
-) %7B%0A          console.log('Remove log', log);%0A          xo.log.delete(log
+, function(value, key) %7B%0A          xo.log.delete(value);%0A          console.log('Remove log', value
 );%0A 
@@ -1384,24 +1384,26 @@
 );%0A        %7D
+);
 %0A      %7D);%0A 

fix remove all logs in Pool view