import ScreenSizeDetection from '../ScreenSizeDetection';

interface DownloadFile {
  name: string;
  url: string;
  size: string;
  extension: string;
  downloadType: string;
  target: string;
}

interface Download {
  name: string;
  files: Array<DownloadFile>;
}

export class DownloadsController {

  public itemsPerPage: number = 7;
  public queryMinLength: number = 3;
  public initialGroups: any = 0;
  public query: string = '';
  public loading: boolean = false;
  public items: Array<Download> = [];
  public controlsMaxSize: number = 9;

  constructor(
    private DownloadsService,
    private $timeout
  ) {
    this.subscribeObservables();
  }

  private subscribeObservables(): void {
    ScreenSizeDetection.get().subscribe(screensize => {
      console.log(screensize)
      let controlsMaxSize;
      if (screensize === 'xs' || screensize === 'sm') {
        controlsMaxSize = 5;
      } else {
        controlsMaxSize = 9;
      }
      this.$timeout(() => {
        this.controlsMaxSize = controlsMaxSize;
      })
    });
  }

  private getData() {
    this.loading = true;
    this.DownloadsService.get().then(response => {
      this.loading = false;
      this.items = response.data;
    });
  }

  public get downloads(): Array<Download> {
    if (this.query.length < this.queryMinLength) {
      return this.items.slice(0, this.initialGroups);
    } else {
      return this.items;
    }
  }

  $onInit() {
    this.getData();
  }
}
