package de.ivu.elect.presentation;

import java.io.Serializable;
import java.util.Optional;

import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.nl.NlElectTreeProperty;
import de.ivu.elect.nl.NlElectionCategoryType;

public class NlWasSystemProperties implements Serializable {

  private static final long serialVersionUID = 1L;

  public Optional<NlElectionCategoryType> getDefaultWahlart(Mandant mandant) {
    var defaultElectionCategoryType = NlElectTreeProperty.getDefaultElectionCategoryType();
    if (defaultElectionCategoryType.isPresent()) {
      return defaultElectionCategoryType;
    }

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

    return Optional.empty();
  }

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

}
