import {Component} from '@angular/core';
import {HwobsDir, HwobsFile, HwobsService} from "../hwobs.service";

@Component({
  selector: 'app-page-hwobs-manager',
  templateUrl: './page-hwobs-manager.component.html',
  styleUrls: ['./page-hwobs-manager.component.scss']
})
export class PageHwobsManagerComponent {
  dirs: HwobsDir[] = [];
  files: HwobsFile[] = [];
  prefix: string = "storage/"

  openFile(file: any) {
    console.log('Opening file:', file);
    // Add your logic to open the file details here
  }

  openDir(dir?: HwobsDir | null, type: string = "pre") {
    if (dir) {
      this.prefix = dir.Prefix
      this.listDir(dir.Prefix)
    } else {
      if (type == "pre") {
        let list = this.prefix.split("/")
        if (list.length > 1) {
          let prefix = list.splice(0, list.length - 1).join("/")
          console.log(prefix)
          this.prefix = prefix
          this.listDir(prefix)
        }
      }
    }
  }

  showName(name: string) {
    return name.replaceAll(this.prefix, "")
  }

  constructor(
    private hwobs: HwobsService
  ) {
    this.listDir(this.prefix)
  }

  async listDir(prefix: string) {
    let {dirs, files} = await this.hwobs.listDir(prefix)
    this.dirs = dirs
    this.files = files
  }

}
