export const CostsReportsDetailComponent = {
  bindings: {
    transition: '<'
  },
  template: require('./costs-reports-detail.html'),
  controller: CostsReportsDetailController
};

function CostsReportsDetailController($mdDialog, $state, $q, CostsReports, Projects, roleCheckerService, logger) {
  'ngInject';

  const ctrl = this;

  const id = ctrl.transition.params().id;

  ctrl.loading = true;
  ctrl.isAdmin = false;
  ctrl.report = null;
  ctrl.excludedProjects = [];

  ctrl.deleteReport = deleteReport;

  init();

  function init() {
    loadAdminStatus()
      .then(loadCostReport);
  }

  function loadAdminStatus() {
    return roleCheckerService
      .hasHubRole('admin')
      .then(hasRole => {
        ctrl.isAdmin = hasRole;
      });
  }

  function loadCostReport() {
    return CostsReports
      .get(id)
      .then(report => {
        ctrl.report = report;

        return prepareExcludedProjects(report.config);
      })
      .finally(() => {
        ctrl.loading = false;
      });
  }

  function prepareExcludedProjects(config) {
    if (config.excluded_projects) {
      const promises = config.excluded_projects.map(Projects.get);
      return $q
        .all(promises)
        .then(projects => {
          angular.copy(projects, ctrl.excludedProjects);
        });
    }
    return $q.when([]);
  }

  function deleteReport(targetEvent) {
    if (!ctrl.isAdmin) {
      return;
    }

    const confirm = $mdDialog.confirm()
      .title('Are you sure?')
      .textContent(`This will delete the costs report '${id}'. You can still recreate it later as long as the source data is still available.`)
      .ariaLabel('Confirm deletion of costs report')
      .targetEvent(targetEvent)
      .ok('Do it')
      .cancel('Cancel');

    $mdDialog
      .show(confirm)
      .then(() => {
        ctrl.loading = true;

        CostsReports
          .delete(ctrl.report.id)
          .then(() => {
            logger.success('Costs report deleted');
            $state.go('costs-reports.list');
          })
          .finally(() => {
            ctrl.loading = false;
          });
      });
  }
}

export const CostsReportsDetailComponent = {
  bindings: {
    transition: '<'
  },
  template: require('./costs-reports-detail.html'),
  controller: CostsReportsDetailController
};

function CostsReportsDetailController($mdDialog, $state, CostsReports, Projects, roleCheckerService, logger) {
  'ngInject';

  const ctrl = this;

  const id = ctrl.transition.params().id;

  ctrl.loading = true;
  ctrl.isAdmin = false;
  ctrl.report = null;

  ctrl.deleteReport = deleteReport;

  init();

  function init() {
    loadAdminStatus()
      .then(loadCostReport);
  }

  function loadAdminStatus() {
    return roleCheckerService
      .hasHubRole('admin')
      .then(hasRole => {
        ctrl.isAdmin = hasRole;
      });
  }

  function loadCostReport() {
    return CostsReports
      .get(id)
      .then(report => {
        ctrl.report = report;
      })
      .finally(() => {
        ctrl.loading = false;
      });
  }

  function deleteReport(targetEvent) {
    if (!ctrl.isAdmin) {
      return;
    }

    const confirm = $mdDialog.confirm()
      .title('Are you sure?')
      .textContent(`This will delete the costs report '${id}'. You can still recreate it later as long as the source data is still available.`)
      .ariaLabel('Confirm deletion of costs report')
      .targetEvent(targetEvent)
      .ok('Do it')
      .cancel('Cancel');

    $mdDialog
      .show(confirm)
      .then(() => {
        ctrl.loading = true;

        CostsReports
          .delete(ctrl.report.id)
          .then(() => {
            logger.success('Costs report deleted');
            $state.go('costs-reports.list');
          })
          .finally(() => {
            ctrl.loading = false;
          });
      });
  }
}

remove unused code in costs report detail view