/*
 * Copyright (c) 2021 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { Component, HostListener } from '@angular/core';
import { BciMultiFileSelectIntl, FileValidationError, FileValidationTypeError } from '@bci-web-core/core';
import { CustomBciMultiFileSelectIntl } from './multi-file-select-demo-intl';

@Component({
  selector: 'bci-multi-file-select-demo',
  templateUrl: './multi-file-select-demo.component.html',
  styleUrls: ['./multi-file-select-demo.component.scss'],
  providers: [{ provide: BciMultiFileSelectIntl, useClass: CustomBciMultiFileSelectIntl }],
})
export class MultiFileSelectDemoComponent {
  readonly SUPPORTED_FILE_TYPES = ['.xslx','.txt','.docx','.json','.csv'];
  readonly MAX_FILE_SIZE_IN_MB = 1;
  readonly MAX_FILES = 3;
  readonly MULTIPLE_FILE_SELECTION = true;
  readonly UNIQUE_FILE_NAMES = true;

  errorMessages: string[] = [];

  @HostListener('window:resize')
  onFilesChange() {
    this.clearErrorMessages();
  } 

  onFileValidationError($event: FileValidationError[]) {    
    this.clearErrorMessages();
    $event.forEach(event => {
      const fileName = $event.length > 1 ? event.name : 'The selected file';
      switch (event.validationError) {
        case FileValidationTypeError.DUPLICATED_FILE:
          this.errorMessages.push(`${fileName} already in the list.`);
          break;
        case FileValidationTypeError.EXCEEDED_FILE_NUMBER:
          this.errorMessages.push(`${fileName} exceeds the maximum number of files allowed.`);
          break;
        case FileValidationTypeError.MISSING_FILE_EXTENSION:
          this.errorMessages.push(`${fileName} has no extension.`);
          break;
        case FileValidationTypeError.INVALID_FILE_TYPE:
          this.errorMessages.push(`${fileName} is not of type ${this.SUPPORTED_FILE_TYPES.toString()}.`);
          break;
        case FileValidationTypeError.EXCEEDED_FILE_SIZE:
          this.errorMessages.push(`${fileName} exceeds the maximum size of ${this.MAX_FILE_SIZE_IN_MB} MB.`);
          break;
        case FileValidationTypeError.EXCEEDED_FILE_SIZE_AND_INVALID_FILE_TYPE:
          this.errorMessages.push(`${fileName} exceeds the maximum size of ${this.MAX_FILE_SIZE_IN_MB} MB and is not of type ${this.SUPPORTED_FILE_TYPES.toString()}.`);
          break;
        case FileValidationTypeError.EXCEEDED_FILE_SIZE_AND_MISSING_EXTENSION:
          this.errorMessages.push(`${fileName} has no extension and exceeds the maximum size of ${this.MAX_FILE_SIZE_IN_MB} MB.`);
          break;
      }
    });    
  } 

  private clearErrorMessages() {
    this.errorMessages = [];
  } 
}
