/*
 * Copyright (c) [2015] - [2017] Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Red Hat, Inc. - initial API and implementation
 */
'use strict';
import {CodenvyTeam} from '../../../../components/api/codenvy-team.factory';

/**
 * Controller for a team item.
 *
 * @author Ann Shumilova
 */
export class TeamItemController {
  /**
   * Service for displaying dialogs.
   */
  private confirmDialogService: any;
  /**
   * Location service.
   */
  private $location: ng.ILocationService;
  /**
   * Team API interaction.
   */
  private codenvyTeam: CodenvyTeam;
  /**
   * Service for displaying notifications.
   */
  private cheNotification: any;
  /**
   * Team details (the value is set in directive attributes).
   */
  private team: any;
  /**
   * Callback needed to react on teams updation (the value is set in directive attributes).
   */
  private onUpdate: Function;

  /**
   * Default constructor that is using resource injection
   * @ngInject for Dependency injection
   */
  constructor($location: ng.ILocationService, codenvyTeam: CodenvyTeam, confirmDialogService: any, cheNotification: any) {
    this.$location = $location;
    this.confirmDialogService = confirmDialogService;
    this.codenvyTeam = codenvyTeam;
    this.cheNotification = cheNotification;
  }

  /**
   * Redirect to team details.
   */
  
(tab: string) {
    this.$location.path('/team/' + this.team.qualifiedName).search(!tab ? {} : {tab: tab});
  }

  /**
   * Removes team after confirmation.
   */
  removeTeam(): void {
    this.confirmRemoval().then(() => {
      this.codenvyTeam.deleteTeam(this.team.id).then(() => {
        this.onUpdate();
      }, (error: any) => {
        this.cheNotification.showError(error && error.data && error.data.message ? error.data.message : 'Failed to delete team ' + this.team.name);
      });
    });
  }

  /**
   * Get team display name.
   *
   * @param team
   * @returns {string}
   */
  getTeamDisplayName(team): string {
    return this.codenvyTeam.getTeamDisplayName(team);
  }

  /**
   * Shows dialog to confirm the current team removal.
   *
   * @returns {angular.IPromise<any>}
   */
  confirmRemoval(): ng.IPromise<any> {
    let promise = this.confirmDialogService.showConfirmDialog('Delete team',
      'Would you like to delete team \'' + this.team.name + '\'?', 'Delete');
    return promise;
  }
}


redirectToTeamDetails