import { Component, Inject, OnInit } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { marker } from '@biesbjerg/ngx-translate-extract-marker';
import { NanoDialogBaseComponent } from 'src/app/shared/dialogs/nano-base-dialog';
import { NewFolderDialogComponent } from 'src/app/shared/dialogs/new-folder-dialog/new-folder-dialog.component';
import { NanoService } from 'src/app/shared/nano/nano.service';
import { NanoSlotRecord } from 'src/app/shared/server-services/query-records/nano-records';
import { DialogService } from 'src/app/shared/services/dialog.service';
import { SnackBarService } from 'src/app/shared/services/snackbar.service';

@Component({
  selector: 'app-create-drive-dialog',
  templateUrl: './create-drive-dialog.component.html',
  styleUrls: ['./create-drive-dialog.component.scss'],
})
export class CreateDriveDialogComponent
  extends NanoDialogBaseComponent<CreateDriveDialogComponent>
  implements OnInit
{
  constructor(
    public dialogRef: MatDialogRef<CreateDriveDialogComponent>,
    @Inject(MAT_DIALOG_DATA)
    public createDriveDialogData: CreateDriveDialogData,
    private nanoService: NanoService,
    private dialogService: DialogService,
    private dialog: MatDialog,
    private snackbarService: SnackBarService
  ) {
    super(dialogRef);
  }

  folders: Array<Folder>;
  path: Array<string> = new Array<string>();
  isRootFolder: boolean = true;
  isLoading: boolean = true;

  ngOnInit(): void {
    this.getFolders('');
  }

  public listFolders(folder: string = null) {
    this.buildPath(folder);
    this.getFolders(this.path.join(''));
  }

  public stepBack() {
    this.path.pop();
    this.getFolders(this.path.join(''));
  }

  public newFolderDialog() {
    var dialogRef = this.dialog.open(NewFolderDialogComponent, {
      data: { name: 'New Nano Drive' },
    });

    dialogRef.afterClosed().subscribe((folderName) => {
      if (!folderName) return;

      this.nanoService.adminMakeDir(
        this.createDriveDialogData.nanoSlot.id,
        this.path.join(''),
        folderName,
        this.createDriveDialogData.adminPw,
        (res) => {
          this.snackbarService.showSnackbar(marker('Folder created successfully!'));
          this.listFolders(folderName);
        },
        (err) => {
          this.snackbarService.showSnackbar(marker('Error during folder creation.'));
        }
      );
    });
  }

  private buildPath(folderName: string = null) {
    if (folderName && !folderName.endsWith('/')) folderName += '/';

    this.path.push(folderName);
    this.isRootFolder = this.path.length == 1;
  }

  private getFolders(path: string) {
    this.isLoading = true;
    this.nanoService.adminGetListing(
      this.createDriveDialogData.nanoSlot.id,
      path,
      this.createDriveDialogData.adminPw,
      (res) => {
        this.folders = res.map((r) => {
          return { name: r.name, unresolved: r.unresolved ? r.unresolved : false };
        });

        this.folders = this.folders.sort((a, b) =>
          a.name.toUpperCase() > b.name.toUpperCase() ? 1 : -1
        );
        this.isLoading = false;
      },
      (err) => {
        this.path.pop();
        this.dialogService.openAlertDialog(
          marker('Unreachable folder'),
          marker('Sorry, but Nano is not able to access this folder. Please choose an other.')
        );
        this.isLoading = false;
      }
    );
  }

  public chooseCurrentPath() {
    this.dialogRef.close(this.path.join(''));
  }

  public createFolder() {
    this.nanoService.adminMakeDir(
      this.createDriveDialogData.nanoSlot.id,
      this.path.join(''),
      'Folder',
      this.createDriveDialogData.adminPw,
      () => {}
    );
  }
}

interface CreateDriveDialogData {
  nanoSlot: NanoSlotRecord;
  adminPw: string;
}

interface Folder {
  name: string;
  unresolved: boolean;
}
