import { autoinject, bindable, customElement, observable, TaskQueue } from 'aurelia-framework';
import { I18N } from 'aurelia-i18n';
import { ImportService, ImportSession, SheetDTO } from "../../service/import-service";

@autoinject
@customElement("import-step-2")
export class ImportStep2 {

  @bindable
  public session: ImportSession;

  @bindable
  public newStep;
  @bindable
  public cancelSession;

  private headerLineRowNumberError: string[] = [];
  private firstDataLineRowNumberError: string[] = [];

  public sheets: SheetDTO[];
  random: number;


  constructor(private importeService: ImportService, private taskQueue: TaskQueue, private i18n: I18N) {
  }

  async bind() {
    this.random = Date.now();
    this.sheets = await this.importeService.getSheetDtos(this.session.id);
    if (this.session.sheetsIndex == null) {
      this.session.sheetsIndex = [this.sheets[0].index];
    }
  }

  async confirm() {
    if (false == (this.session.headerLineRowNumber?.toString().length > 0)) {
      if (this.headerLineRowNumberError.length == 0)
        this.headerLineRowNumberError.push(this.i18n.tr("importdata.headerLineRowNumberError", { ns: "common" }));
    }
    else {
      this.headerLineRowNumberError.splice(0);
    }
    if (false == (this.session.firstDataLineRowNumber?.toString().length > 0)) {
      if (this.firstDataLineRowNumberError.length == 0)
        this.firstDataLineRowNumberError.push(this.i18n.tr("importdata.firstDataLineRowNumberError", { ns: "common" }));
    }
    else {
      this.firstDataLineRowNumberError.splice(0);
    }
    if (this.firstDataLineRowNumberError.length > 0 || this.headerLineRowNumberError.length > 0) {
      return;
    }
    await this.importeService.saveSession(this.session);
    this.newStep();
  }
}
