import template from './project-manage.html';

export default function projectManage() {
  return {
    restrict: 'E',
    template: template,
    controller: ProjectManageController,
    controllerAs: '$ctrl',
    scope: {},
    bindToController: true
  };
}

// @ngInject
class ProjectManageController {
  constructor(projectsService,
              currentStateService,
              customersService,
              $rootScope,
              $state,
              $q) {
    this.projectsService = projectsService;
    this.currentStateService = currentStateService;
    this.customersService = customersService;
    this.$rootScope = $rootScope;
    this.$state = $state;
    this.$q = $q;
    this.activate();
  }

  activate() {
    this.canManage = false;
    this.projectModel = {};
    this.currentStateService.getProject().then(project => {
      this.project = project;
      this.projectModel = angular.copy(project);
    });
    this.customersService.isOwnerOrStaff().then(canManage => {
      this.canManage = canManage;
    });
  }

  saveProject() {
    if (this.ProjectForm.$invalid) {
      return this.$q.reject();
    }
    return this.projectsService.$update(null, this.project.url, this.projectModel).then(project => {
      this.$rootScope.$broadcast('adjustCurrentProject', project);
    }, response => {
      this.errors = response.data;
    });
  }

  removeProject() {
    var confirmDelete = confirm('Confirm deletion?');
    if (!confirmDelete) {
      return this.$q.reject();
    }
    this.currentStateService.setProject(null);
    return this.project.$delete().then(() => {
      this.projectsService.clearAllCacheForCurrentEndpoint();
      return this.projectsService.getFirst().then(project => {
        this.currentStateService.setProject(project);
        this.$rootScope.$broadcast('refreshProjectList', {model: this.project, remove: true});
      });
    }, () => {
      this.currentStateService.setProject(this.project);
    }).then(() => {
      this.currentStateService.reloadCurrentCustomer(customer => {
        this.$rootScope.$broadcast('checkQuotas:refresh');
        this.$rootScope.$broadcast('customerBalance:refresh');
        this.$state.go('organization.projects', {uuid: customer.uuid});
      });
    });
  }
}

@@ -389,32 +389,92 @@
 stomersService,%0A
+              WorkspaceService,%0A              ncUtilsFlash,%0A
               $r
@@ -483,16 +483,16 @@
 tScope,%0A
-
         
@@ -659,32 +659,116 @@
 stomersService;%0A
+    this.WorkspaceService = WorkspaceService;%0A    this.ncUtilsFlash = ncUtilsFlash;%0A
     this.$rootSc
@@ -926,16 +926,118 @@
 l = %7B%7D;%0A
+    this.currentStateService.getCustomer().then(customer =%3E %7B%0A      this.customer = customer;%0A    %7D);%0A
     this
@@ -1493,61 +1493,445 @@
 his.
-$rootScope.$broadcast('adjustCurrentProject', project
+ncUtilsFlash.success('Project has been updated');%0A      // TODO: Migrate to Redux and make code DRY%0A      this.currentStateService.setProject(project);%0A      let item = this.customer.projects.filter(item =%3E item.uuid === project.uuid)%5B0%5D;%0A      item.name = project.name;%0A      this.WorkspaceService.setWorkspace(%7B%0A        customer: this.customer,%0A        project: project,%0A        hasCustomer: true,%0A        workspace: 'project',%0A      %7D
 );%0A 

Update project name in related components after it has been updated (WAL-382)