package de.ivu.elect.presentation.security;

import java.util.HashMap;
import java.util.Map;

import javax.enterprise.event.Observes;
import javax.enterprise.event.Reception;
import javax.enterprise.inject.Specializes;
import javax.inject.Inject;

import de.ivu.elect.business.gebietsbaum.boundary.GebietSelectChangeEvent;
import de.ivu.elect.business.gebietsbaum.boundary.GebietSessionSelect;
import de.ivu.elect.business.gebietsbaum.entity.Gebiet;
import de.ivu.elect.business.mandant.boundary.MandantSelectChangeEvent;
import de.ivu.elect.business.mandant.control.MandantSupport;
import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.business.security.control.ElectCommonPrincipal;
import de.ivu.elect.business.wahl.boundary.WahlSelectChangeEvent;
import de.ivu.elect.business.wahl.boundary.WahlSessionSelector;
import de.ivu.elect.business.wahl.boundary.WahlterminSelectChangeEvent;
import de.ivu.elect.business.wahl.boundary.WahlterminSessionSelect;
import de.ivu.elect.business.wahl.entity.Wahl;
import de.ivu.elect.business.wahl.entity.Wahltermin;
import de.ivu.elect.jee.presentation.redirect.RedirectParameterAttachement;

/**
 *  on 03.07.2015.
 */
@Specializes
public class ElectRedirectParameterAttachement extends RedirectParameterAttachement {

  public static final String ELECT_MANDANT_NUMMER = MandantenNummerUrlParameter.MANDANT_PARAMETER;

  public static final String ELECT_WAHLTERMIN_ID = "elect_wtid";

  public static final String ELECT_WAHL_ID = "elect_wid";

  public static final String ELECT_GEBIET_ID = "elect_gid";

  private static final long serialVersionUID = 1L;

  @Inject
  private transient ElectCommonPrincipal electCommonPrincipal;

  @Inject
  private transient MandantenNummerUrlParameter mandantenNummerUrlParameter;

  @Inject
  private transient WahlSessionSelector wahlSessionSelect;

  @Inject
  private transient WahlterminSessionSelect wahlterminSessionSelect;

  @Inject
  private transient GebietSessionSelect gebietSessionSelect;

  @Inject
  private transient MandantSupport mandantSupport;

  @Override
  protected Map<String, String> buildMap() {
    Map<String, String> result = new HashMap<>();
    addMandantWahlterminGebietParameter(result);
    return result;

  }

  protected void addMandantWahlterminGebietParameter(Map<String, String> result) {
    // ELECTKOM-146
    if (mandantSupport.isMandantenUnterstuetzt()) {
      Mandant mandant = electCommonPrincipal.getMandant();
      Long mandantenNummer = null;
      if (mandant != null && mandant.getNummer() != null) {
        mandantenNummer = mandant.getNummer();
        mandantenNummerUrlParameter.setMandantenNummer(mandant.getNummer());
      }
      if (mandantenNummer == null) {
        mandantenNummer = mandantenNummerUrlParameter.getMandantenNummer();
      }

      if (mandantenNummer != null) {
        result.put(ELECT_MANDANT_NUMMER, mandantenNummer.toString());
      }
    }

    Wahltermin wahltermin = wahlterminSessionSelect.getEntity();
    if (wahltermin != null) {
      result.put(ELECT_WAHLTERMIN_ID, wahltermin.getId().toString());
    }
    Wahl wahl = wahlSessionSelect.getEntity();
    if (wahl != null) {
      result.put(ELECT_WAHL_ID, wahl.getId().toString());
    }
    Gebiet gebiet = gebietSessionSelect.getEntity();
    if (gebiet != null) {
      result.put(ELECT_GEBIET_ID, gebiet.getId().toString());
    }
  }

  public void onGebietSelectChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) GebietSelectChangeEvent event) {
    reset();
  }

  public void onWahlSelectChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) WahlSelectChangeEvent event) {
    reset();
  }

  public void onWahlterminSelectChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) WahlterminSelectChangeEvent event) {
    reset();
  }

  public void onMandantSelectChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) MandantSelectChangeEvent event) {
    reset();
  }
}
