import { Component, OnInit } from '@angular/core';
import { DetailsComponentBase } from '@objects-view/details-component-base';
import { IBaseFleioObjectModel } from '@fleio-api/base-model/base-fleio-object.model';
import { SettingsApiService } from '@fleio-api/core/settings-api.service';
import { UntypedFormBuilder, Validators } from '@angular/forms';
import { NotificationService } from '@shared/ui-api/notification.service';
import { ValidatorsHelper } from '@shared/ui-api/helpers/validators-helper';

@Component({
  selector: 'app-advanced-settings',
  templateUrl: './advanced-settings.component.html',
  styleUrls: ['./advanced-settings.component.scss']
})
export class AdvancedSettingsComponent extends DetailsComponentBase<IBaseFleioObjectModel> implements OnInit {
  loadingSettings = false;
  advancedSettingsForm = this.formBuilder.group({
    disable_periodic_tasks: [false],
    grant_all_permissions_implicitly: [true],
    delete_activity_log_entries: [true],
    delete_activity_log_entries_older_than_months: [24, [ValidatorsHelper.requiredIntegerValidator, Validators.min(1)]],
    delete_email_log_entries: [true],
    delete_email_log_entries_older_than_months: [24, [ValidatorsHelper.requiredIntegerValidator, Validators.min(1)]],
    delete_periodic_task_log_entries: [true],
    delete_periodic_task_log_entries_older_than_months: [24, [ValidatorsHelper.requiredIntegerValidator, Validators.min(1)]],
    cleanup_temporary_directory: [true],
    delete_temporary_files_older_than_months: [1, [ValidatorsHelper.requiredIntegerValidator, Validators.min(1)]],
    short_operation_step_timeout: [5, ValidatorsHelper.requiredIntegerValidator],
    long_operation_step_timeout: [60, ValidatorsHelper.requiredIntegerValidator],
    operations_run_interval: [1, [ValidatorsHelper.requiredIntegerValidator, Validators.min(1)]],
  });

  constructor(
    private settingsApi: SettingsApiService,
    private formBuilder: UntypedFormBuilder,
    private notificationService: NotificationService,
  ) {
    super();
  }

  ngOnInit(refreshTimerInterval?: number) {
    super.ngOnInit(refreshTimerInterval);
    this.advancedSettingsForm.controls.delete_activity_log_entries.valueChanges.subscribe(value => {
      if (value) {
        this.advancedSettingsForm.controls.delete_activity_log_entries_older_than_months.enable();
      } else {
        this.advancedSettingsForm.controls.delete_activity_log_entries_older_than_months.disable();
      }
    });
    this.advancedSettingsForm.controls.delete_email_log_entries.valueChanges.subscribe(value => {
      if (value) {
        this.advancedSettingsForm.controls.delete_email_log_entries_older_than_months.enable();
      } else {
        this.advancedSettingsForm.controls.delete_email_log_entries_older_than_months.disable();
      }
    });
    this.advancedSettingsForm.controls.delete_periodic_task_log_entries.valueChanges.subscribe(value => {
      if (value) {
        this.advancedSettingsForm.controls.delete_periodic_task_log_entries_older_than_months.enable();
      } else {
        this.advancedSettingsForm.controls.delete_periodic_task_log_entries_older_than_months.disable();
      }
    });
  }

  protected initTabData() {
    this.loadingSettings = true;
    this.settingsApi.getAdvancedSettings().subscribe({
      next: advancedSettings => {
        this.advancedSettingsForm.patchValue(advancedSettings);
      }, error: error => {
        if (error.status === 403) {
          this.advancedSettingsForm.disable();
        }
      }
    }).add(() => {
      this.loadingSettings = false;
    });
  }

  saveSettings() {
    const advancedSettings = this.advancedSettingsForm.getRawValue();
    this.objectController.loading = true;
    this.settingsApi.saveAdvancedSettings(advancedSettings).subscribe({
      next: () => {
        this.notificationService.showMessage($localize`Advanced settings saved`);
      }, error: () => {
        this.notificationService.showMessage($localize`Failed to save advanced settings, check logs for details`);
      }
    }).add(() => {
      this.objectController.loading = false;
    });
  }
}
