package de.ivu.elect.presentation;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import javax.annotation.Nullable;

import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.nl.met.ElectionCategoryType;
import de.ivu.elect.nl.met.MasterElectTreeProperty;

public class NlSystemProperties implements Serializable {

  private static final long serialVersionUID = 1L;

  public Optional<Date> getDefaultWahldatum(Mandant mandant) {
    Optional<Date> defaultElectionDate = MasterElectTreeProperty.getDefaultElectionDate();
    if (defaultElectionDate.isPresent()) {
      return defaultElectionDate;
    }

    // Fallback für Demo-System
    if (mandant != null && !mandant.getArt().isOhneMandant()) {
      return extrahiereDatumAusMandantName(mandant.getName());
    }

    return Optional.empty();
  }

  public Optional<ElectionCategoryType> getDefaultWahlart(Mandant mandant) {
    Optional<ElectionCategoryType> defaultElectionCategoryType = MasterElectTreeProperty.getDefaultElectionCategoryType();
    if (defaultElectionCategoryType.isPresent()) {
      return defaultElectionCategoryType;
    }

    // Fallback für Demo-System
    if (mandant != null && !mandant.getArt().isOhneMandant()) {
      return extrahiereWahlartAusMandantName(mandant.getName());
    }

    return Optional.empty();
  }

  public NlElectionLevel getDefaultElectionLevel(Mandant mandant) {
    NlElectionLevel nlElectionLevel = NlElectionLevel.UNBEKANNT;
    try {
      // ELECT-9903
      nlElectionLevel = NlElectionLevel.valueOf(System.getProperty("nl.election.level"));

    } catch (Exception e) {
      // ignorieren
    }

    if (nlElectionLevel.isLevelGesetzt()) {
      return nlElectionLevel;
    }

    // Fallback für Demo-System
    if (mandant != null && !mandant.getArt().isOhneMandant()) {
      return extrahiereElectionLevelAusMandantName(mandant.getName());
    }

    return nlElectionLevel;
  }

  private NlElectionLevel extrahiereElectionLevelAusMandantName(String name) {

    Set<NlElectionLevel>  result = new HashSet<>();

    try {
      for (String s : name.split("_")) {
        try {
          result.add(NlElectionLevel.valueOf(s));
        } catch (Exception e) {
          // ignore
        }
      }
    } catch (Exception e) {
      // ignore
    }

    if (result.isEmpty()) {
      return NlElectionLevel.UNBEKANNT;
    }

    return NlElectionLevel.kombiniere(result);
  }

  private Optional<ElectionCategoryType> extrahiereWahlartAusMandantName(String name) {
    try {
      for (String s : name.split("_")) {
        try {
          return Optional.of(ElectionCategoryType.valueOf(s));
        } catch (Exception e) {
          // ignore
        }
      }
    } catch (Exception e) {
      // ignore
    }
    return Optional.empty();
  }

  private Optional<Date> extrahiereDatumAusMandantName(String name) {
    try {
      for (String s : name.split("_")) {
        try {
          return Optional.of(new SimpleDateFormat("yyyyMMdd").parse(s));
        } catch (Exception e) {
          // ignore
        }
      }
    } catch (Exception e) {
      // ignore
    }
    return Optional.empty();
  }

  public boolean isPreventShutdown(@Nullable Mandant mandant) {
    if (Boolean.TRUE.toString().equalsIgnoreCase(System.getProperty("nl.prevent.shutdown"))) {
      return true;
    }

    if (mandant != null && !mandant.getArt().isOhneMandant()) {
      return true;
    }

    return false;
  }

}
