
package de.ivu.elect.business.datei.entity;

import de.ivu.elect.business.exchange.entity.ImportStatus;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

public enum FileType {
  XML("application/xml", "xml", true),
  CSV("text/csv", "csv", true),
  ZIP("application/zip", "zip", true),
  GZIP("application/gzip", "gz", false),
  PDF("application/pdf", "pdf", false),
  ODT("application/vnd.oasis.opendocument.text", "odt", false),
  OTT("application/vnd.oasis.opendocument.text-template", "ott", false),
  ODS("application/vnd.oasis.opendocument.spreadsheet", "ods", false),
  DVZ(ZIP.getDefaultMimeType(), "dvz", ZIP.isVorschauMoeglich()), // ELECT-5759
  DOCX("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx", false),
  DOC("application/msword", "doc", false),
  XLS("application/vnd.ms-excel", "xls", false),
  XLSX("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx", false),
  SQL("text/x-sql", "sql", false),
  RTF("text/rtf", "rtf", true),
  HTML("text/html", "html", true),
  TXT("text/plain", "txt", true),
  LAFST("text/plain", "lafst", true),
  JPEG("image/jpeg", "jpg", false),
  PNG("image/png", "png", false),
  SVG("image/svg+xml", "svg", false),
  EXE("application/vnd.microsoft.portable-executable", "exe", false),
  JSON("application/json", "json", false),
  ALL("", "*", false);

  private final String defaultMimeType;

  private final String fileExtension;

  private final boolean vorschauMoeglich;

  private FileType(String mimeType, String fileExtension, boolean vorschauMoeglich) {
    this.defaultMimeType = mimeType;
    this.fileExtension = fileExtension;
    this.vorschauMoeglich = vorschauMoeglich;
  }

  public boolean isArchive() {
    return isZipFile() || this.equals(GZIP) || this.equals(DVZ);
  }

  public boolean isDokumentVorlageArchive() {
    return isZipFile() || this.equals(DVZ);
  }

  public boolean isPdfFile() {
    return this.equals(PDF);
  }

  public boolean isCsvFile() {
    return this.equals(CSV);
  }

  public boolean isXmlFile() {
    return this.equals(XML);
  }

  public boolean isZipFile() {
    return this.equals(ZIP);
  }

  public boolean isExeFile() {
    return this.equals(EXE);
  }

  public boolean isOpenDocumentFile() {
    return this.equals(ODT) || this.equals(ODS);
  }

  public String getDefaultMimeType() {
    return defaultMimeType;
  }

  public String getFileExtension() {
    return fileExtension;
  }

  /**
   * FIXME: Please refactor! (I'm with stupid...)
   *
   * Es wird immer eine CsvDatei-Entität erstellt, da das Format später noch festgelegt werden kann.
   */
  public Datei createDatei() {
    return createCsvDatei();
  }

  public CsvDatei createCsvDatei() {
    return new CsvDatei();
  }

  public ImportStatus getDefaultUploadStatus() {
    return ImportStatus.HOCHGELADEN;
  }

  // ELECTMUC-290
  public boolean isVorschauMoeglich() {
    return vorschauMoeglich;
  }

  public static FileType fromFilename(String filename) {
    if (StringUtils.isBlank(filename)) {
      return ALL;
    }

    String lower = filename.trim().toLowerCase();
    return Arrays.stream(values()).filter(value -> lower.endsWith(value.fileExtension)).findFirst().orElse(ALL);
  }
}
