export default class ListMediaServerController {
  constructor($scope, mediaServers, $log, $window, toolbar) {
    this.mediaServers = mediaServers;
    this.$scope = $scope;
    this.$log = $log;
    this.$window = $window;
    this.toolbar = toolbar;

    this.$scope.list = [];
    this.$scope.headers = [];
    this.$scope.actions = [
      'edit',
      'delete'
    ];
    this.$scope.path = '/xivo/configuration/index.php/manage/mediaserver/';
    this.$scope.delete = (id) => {
      this.mediaServers.deleteMediaServer(id).then(
        (response) => {
          this.$window.location.href = '?act=list';
        },
        (error) => {
          $log.error(error);
        }
      );
    };

    mediaServers.listMediaServers()
      .then((response) => {
        this.$scope.headers = response.data.headers;
        this.$scope.list = response.data.data;
        for (var i = 0; i < response.data.data.length; i++) {
           if (response.data.data[i]['main']) {
              this.toolbar.setError('main_mediaserver_info_message');
           }
        }
      }, (error) => {
        $log.error(error);
      });
  }
}
