import { Component, Inject, OnInit, ViewEncapsulation } from "@angular/core";
import {
  MatDialog,
  MatDialogRef,
  MAT_DIALOG_DATA,
} from "@angular/material/dialog";
import { marker } from "@biesbjerg/ngx-translate-extract-marker";
import { NanoDialogBase } 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 { NanoClientRecord } from "src/app/shared/server-services/query-records/nano-records";
import { DialogService } from "src/app/shared/services/dialog.service";

@Component({
  selector: "app-create-drive-dialog",
  templateUrl: "./create-drive-dialog.component.html",
  styleUrls: ["./create-drive-dialog.component.scss"],
})
export class CreateDriveDialogComponent
  extends NanoDialogBase<CreateDriveDialogComponent>
  implements OnInit
{
  constructor(
    public dialogRef: MatDialogRef<CreateDriveDialogComponent>,
    @Inject(MAT_DIALOG_DATA)
    public createDriveDialogData: CreateDriveDialogData,
    private nanoService: NanoService,
    private dialogService: DialogService,
    private dialog: MatDialog
  ) {
    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.nanoClient.id,
        this.path.join(""),
        folderName,
        this.createDriveDialogData.adminPw,
        (res) => { this.dialogService.showSnackbar(marker("Folder created successfully!")); this.listFolders(folderName) },
        (err) => { this.dialogService.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.nanoClient.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.nanoClient.id,
      this.path.join(""),
      "Folder",
      this.createDriveDialogData.adminPw,
      () => {}
    );
  }
}

interface CreateDriveDialogData {
  nanoClient: NanoClientRecord;
  adminPw: string;
}

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