text: import angular from 'angular';
  
  import { buildOption } from '@/portainer/components/BoxSelector';
  import { FeatureId } from '@/portainer/feature-flags/enums';
  
  angular.module('portainer.app').controller('SettingsController', [
    '$scope',
    '$state',
    'Notifications',
    'SettingsService',
    'StateManager',
    'BackupService',
    'FileSaver',
    'Blob',
    function ($scope, $state, Notifications, SettingsService, StateManager, BackupService, FileSaver) {
      $scope.s3BackupFeatureId = FeatureId.S3_BACKUP_SETTING;
      $scope.backupOptions = [
        buildOption('backup_file', 'fa fa-download', 'Download backup file', '', 'file'),
        buildOption('backup_s3', 'fa fa-upload', 'Store in S3', 'Define a cron schedule', 's3', FeatureId.S3_BACKUP_SETTING),
      ];
  
      $scope.state = {
        actionInProgress: false,
        availableEdgeAgentCheckinOptions: [
          {
            key: '5 seconds',
            value: 5,
          },
          {
            key: '10 seconds',
            value: 10,
          },
          {
            key: '30 seconds',
            value: 30,
          },
        ],
        availableKubeconfigExpiryOptions: [
          {
            key: '1 day',
            value: '24h',
          },
          {
            key: '7 days',
            value: `${24 * 7}h`,
          },
          {
            key: '30 days',
            value: `${24 * 30}h`,
          },
          {
            key: '1 year',
            value: `${24 * 30 * 12}h`,
          },
          {
            key: 'No expiry',
            value: '0',
          },
        ],
        backupInProgress: false,
        featureLimited: false,
      };
  
      $scope.BACKUP_FORM_TYPES = { S3: 's3', FILE: 'file' };
  
      $scope.formValues = {
        customLogo: false,
        labelName: '',
        labelValue: '',
        enableEdgeComputeFeatures: false,
        enableTelemetry: false,
        passwordProtect: false,
        password: '',
        backupFormType: $scope.BACKUP_FORM_TYPES.FILE,
      };
  
      $scope.onToggleAutoBackups = function onToggleAutoBackups(checked) {
        $scope.$evalAsync(() => {
          $scope.formValues.scheduleAutomaticBackups = checked;
        });
      };
  
      $scope.onBackupOptionsChange = function (type, limited) {
        $scope.formValues.backupFormType = type;
        $scope.state.featureLimited = limited;
      };
  
      $scope.removeFilteredContainerLabel = function (index) {
        var settings = $scope.settings;
        settings.BlackListedLabels.splice(index, 1);
  
        updateSettings(settings);
      };
  
      $scope.addFilteredContainerLabel = function () {
        var settings = $scope.settings;
        var label = {
          name: $scope.formValues.labelName,
          value: $scope.formValues.labelValue,
        };
        settings.BlackListedLabels.push(label);
  
        updateSettings(settings);
      };
  
      $scope.downloadBackup = function () {
        const payload = {};
        if ($scope.formValues.passwordProtect) {
          payload.password = $scope.formValues.password;
        }
  
        $scope.state.backupInProgress = true;
  
        BackupService.downloadBackup(payload)
          .then(function success(data) {
            const downloadData = new Blob([data.file], { type: 'application/gzip' });
            FileSaver.saveAs(downloadData, data.name);
            Notifications.success('Backup successfully downloaded');
          })
          .catch(function error(err) {
            Notifications.error('Failure', err, 'Unable to download backup');
          })
          .finally(function final() {
            $scope.state.backupInProgress = false;
          });
      };
  
      $scope.saveApplicationSettings = function () {
        var settings = $scope.settings;
  
        if (!$scope.formValues.customLogo) {
          settings.LogoURL = '';
        }
  
        settings.EnableEdgeComputeFeatures = $scope.formValues.enableEdgeComputeFeatures;
        settings.EnableTelemetry = $scope.formValues.enableTelemetry;
  
        $scope.state.actionInProgress = true;
        updateSettings(settings);
      };
  
      function updateSettings(settings) {
        SettingsService.update(settings)
          .then(function success() {
            Notifications.success('Settings updated');
            StateManager.updateLogo(settings.LogoURL);
            StateManager.updateSnapshotInterval(settings.SnapshotInterval);
            StateManager.updateEnableEdgeComputeFeatures(settings.EnableEdgeComputeFeatures);
            StateManager.updateEnableTelemetry(settings.EnableTelemetry);
            $state.reload();
          })
          .catch(function error(err) {
            Notifications.error('Failure', err, 'Unable to update settings');
          })
          .finally(function final() {
            $scope.state.actionInProgress = false;
          });
      }
  
      function initView() {
        SettingsService.settings()
          .then(function success(data) {
            var settings = data;
            $scope.settings = settings;
  
            if (settings.LogoURL !== '') {
              $scope.formValues.customLogo = true;
            }
            $scope.formValues.enableEdgeComputeFeatures = settings.EnableEdgeComputeFeatures;
            $scope.formValues.enableTelemetry = settings.EnableTelemetry;
          })
          .catch(function error(err) {
            Notifications.error('Failure', err, 'Unable to retrieve application settings');
          });
      }
  
      initView();
    },
  ]);
  