import { FileFlow } from './../../../resources/elements/flow-uploader';
import { autoinject, bindable, customElement, observable } from 'aurelia-framework';
import { IFile } from '../../../model/common-model';
import { ImportService, ImportSession } from '../../service/import-service';

@autoinject
@customElement("import-step-1")
export class ImportStep1 {

  @bindable
  public session: ImportSession;

  @bindable
  public newStep;

  @bindable
  public cancelSession;

  @bindable
  public context;


  private files: Array<IFile> = [];
  private fileUploaded: boolean = false;

  private columnsInfo: Array<{ columnName: string, columnVisibility: boolean }> = [
    { columnName: 'description', columnVisibility: false },
    { columnName: 'fileTypeId', columnVisibility: false },
    { columnName: 'iconFile', columnVisibility: false },
    { columnName: 'actionButton', columnVisibility: false },
    { columnName: 'size', columnVisibility: false },
  ];


  constructor(private importeService: ImportService) {
  }

  bind() {
    if (this.session.importFileSessions.fileUri != null) {
      this.setFile();
    } else {
      this.fileUploaded = false;
      this.files.splice(0);
    }
  }

  public async uploadFile(file: FileFlow) {
  }

  public async uploadedFile(fileUploaded: FileFlow) {
    this.session.importFileSessions.fileName = fileUploaded.name;
    this.session.importFileSessions.fileUri = fileUploaded.url;
    this.setFile();
  }
  private setFile() {
    let fileEntity = {
      name: this.session.importFileSessions.fileName,
      url: this.session.importFileSessions.fileUri
    }
    this.files.splice(0);
    this.files.push(fileEntity as IFile);
    this.fileUploaded = true;
  }

  public cancelFile() {
    this.files.splice(0);
    this.fileUploaded = false;
  }

  async confirm() {
    await this.importeService.saveFileSession(this.session.id, this.session.importFileSessions, this.context);
    this.newStep();
  }
}
