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

import java.util.Arrays;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

/**
 *  on 28.07.2015.
 */
public class PraesentationPathInfo {

  public static final String WAHL_PARAMETER = "wahl";

  public static final String ERGEBNISTYP_PARAMETER = "typ";

  private final String wahlId;

  private final String ergebnisTyp;

  private final String resource;

  public PraesentationPathInfo(String pathInfo) {
    String[] pathSplitted = pathInfo.split("/");
    String wahlId = null;
    String ergebnistyp = null;
    int firstResourcePart = 0;
    for (int i = 0; i < pathSplitted.length; i++) {
      if (WAHL_PARAMETER.equalsIgnoreCase(pathSplitted[i]) && pathSplitted.length > i+1) {
        wahlId = pathSplitted[i + 1];
        if (firstResourcePart < i + 2) {
          firstResourcePart = i + 2;
        }
      }
      if (ERGEBNISTYP_PARAMETER.equalsIgnoreCase(pathSplitted[i]) && pathSplitted.length > i+1) {
        ergebnistyp = pathSplitted[i + 1];
        if (firstResourcePart < i + 2) {
          firstResourcePart = i + 2;
        }
      }
    }

    String pathInfoResource = Arrays.stream(pathSplitted, firstResourcePart, pathSplitted.length)
      .filter(StringUtils::isNotBlank).map(subPath -> "/" + subPath)
      .collect(Collectors.joining());

    this.wahlId = wahlId;
    this.ergebnisTyp = ergebnistyp;
    if (StringUtils.isBlank(pathInfoResource) || "/".equals(pathInfoResource)){
      this.resource="";
    } else {
      this.resource = pathInfoResource;
    }
  }

  public String getErgebnisTyp() {
    return ergebnisTyp;
  }

  public String getResource() {
    return resource;
  }

  public String getWahlId() {
    return wahlId;
  }
}
