import { Location } from '@angular/common';
import { Component, OnInit } from '@angular/core';
import { MatBottomSheetRef } from '@angular/material/bottom-sheet';
import {
  CollectionManagerType,
  DownloadManagerService,
  FileManagerType,
  fileRemovedObserver
} from 'src/app/shared/services/download-manager-service';
import { sideMenuActiveObserver } from 'src/app/shared/shared-observers';

@Component({
  selector: 'app-download-manager',
  templateUrl: './download-manager.component.html',
  styleUrls: ['./download-manager.component.scss'],
})
export class DownloadManagerComponent implements OnInit {
  fileList: FileManagerType[] = [];
  collectionList: CollectionManagerType[] = [];

  constructor(
    private downloadManagerService: DownloadManagerService,
    private location: Location,
    private bottomSheetRef: MatBottomSheetRef<DownloadManagerComponent>
  ) {
    this.fileList = this.downloadManagerService.getFileList();
    this.collectionList = this.downloadManagerService.getCollectionList();
  }

  ngOnInit(): void {
    sideMenuActiveObserver.next(false);

    fileRemovedObserver.subscribe(() => {
      if (this.fileList.length === 0 && this.collectionList.length === 0)
        this.bottomSheetRef.dismiss();
    });
  }
}
