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

    this.params = this.toolbar.parseParams($window.location.search);

    this.$scope.id = this.params.id;

    this.mediaServers.getMediaServer(this.params.id)
    .then((mds) => {
      this.$scope.id = mds.data.id;
      this.$scope.name_value = mds.data.name;
      this.$scope.display_name_value = mds.data.display_name;
      this.$scope.voip_ip_value = mds.data.voip_ip;
    }, (error) => {
      $log.error(error);
    });
  }

  validate() {
    this.mediaServers.updateMediaServer({
      "id": this.$scope.id,
      "name": this.$scope.name_value,
      "display_name": this.$scope.display_name_value,
      "voip_ip": this.$scope.voip_ip_value
    }).then((response) => {
      this.$window.location.href = '?act=list';
    }, (error) => {
      if (error.data.error === 'Duplicate') {
        this.toolbar.setError('mediaserver_duplicate_error');
      }
      this.$log.error(error);
    });
  }
}