import { Component } from '@angular/core';
import { Subject } from 'rxjs';
import { workerHandler } from 'src/app/shared/worker-handler/worker-handler';
import { NanoService } from 'src/app/shared/nano/nano.service';
import { DownloadManagerService } from 'src/app/shared/services/download-manager-service';
import { ThumbnailCache } from 'src/app/shared/nano/thumbnail-cache';
import { AppStorage } from 'src/app/shared/app-storage';

export const enum AdvancedSettingsEnum {
  WORKERS = 'workers',
  FILE_STREAM_SIZE_LIMIT = 'file-stream-size-limit',
  HOLD_FILE_IN_CACHE = 'hold-file-in-cache',
  MAX_THUMBNAIL_CACHE = 'max-thumbnail-cache',
  EXP_IDENTITY_SIGNATURE = 'exp-identity-signature',
}

export let showWorkerGraph = new Subject<boolean>();
@Component({
  selector: 'app-advanced-settings',
  templateUrl: './advanced-settings.component.html',
  styleUrls: ['./advanced-settings.component.scss'],
})
export class AdvancedSettingsComponent {
  public maxWorker;
  public workerInput;
  public devOn = false;
  public fileStreamSizeLimit = NanoService.FILE_STREAM_MIN_SIZE / 1000000;
  public holdFileInCache = DownloadManagerService.STORE_TIME_DOWNLOADED_DATA_IN_CACHE / 1000;
  public maxCachedElements = ThumbnailCache.MAX_CACHED_ELEMENTS;
  public isIdentitySignatureOn =
    AppStorage.getItem(AdvancedSettingsEnum.EXP_IDENTITY_SIGNATURE) == '1';

  constructor() {
    this.maxWorker = workerHandler.getMaxProcess();
  }

  changeMaxWorker(max: number) {
    workerHandler.setMaxProcess(max);
    AppStorage.setItem(AdvancedSettingsEnum.WORKERS, '' + max);
    this.maxWorker = workerHandler.getMaxProcess();
  }

  toggleWorkerGraph(show: boolean) {
    showWorkerGraph.next(show);
  }

  changeFileStreamSizeLimit(limit: number) {
    AppStorage.setItem(AdvancedSettingsEnum.FILE_STREAM_SIZE_LIMIT, '' + limit * 1000000);
    NanoService.FILE_STREAM_MIN_SIZE = limit * 1000000;
  }

  changeHoldFileInCache(sec: number) {
    AppStorage.setItem(AdvancedSettingsEnum.HOLD_FILE_IN_CACHE, '' + sec * 1000);
    DownloadManagerService.STORE_TIME_DOWNLOADED_DATA_IN_CACHE = sec * 1000;
  }

  changeMaxThumbnailCache(amount: number) {
    AppStorage.setItem(AdvancedSettingsEnum.MAX_THUMBNAIL_CACHE, '' + amount);
    ThumbnailCache.MAX_CACHED_ELEMENTS = amount;
  }

  changeIdentitySignature(value: boolean) {
    AppStorage.setItem(AdvancedSettingsEnum.EXP_IDENTITY_SIGNATURE, '' + (value ? 1 : 0));
  }
}
