/*
   * (non-Javadoc)
   *
   * @see atg.commerce.search.refinement.CommerceFacetSearchTools#isEligibleFacetValue(atg.repository.search.refinement.FacetTrail,
   *      atg.repository.search.refinement.FacetValue)
   */
  @Override
  protected boolean isEligibleFacetValue(FacetTrail pFacetTrail, FacetValue pFacetValue) {
    boolean result = true;

    if (PerformanceMonitor.isEnabled()) {
      PerformanceMonitor.startOperation("search", "TEST:CastFacetSearchTools.isEligibleFacetValue");
    }
    if (isSkipValuesAlreadyInTrail()) {
      FacetValue[] trailValues = pFacetTrail.getFacetValues();
      for (int i = 0; i < trailValues.length; i++) {
        if (trailValues[i] instanceof DisjunctionMultiValue) {
          for (Object o : ((DisjunctionMultiValue) trailValues[i]).getValues()) {
            Object value =
                pFacetValue instanceof FacetDisjunctionMultiValue
                    ? ((FacetDisjunctionMultiValue) pFacetValue).getSingleValue()
                    : pFacetValue instanceof RangeFacetDisjunctionMultiValue
                        ? ((RangeFacetDisjunctionMultiValue) pFacetValue).getSingleValue()
                        : pFacetValue.getValue();
            if (value.equals(o)) {
              if (PerformanceMonitor.isEnabled()) {
                PerformanceMonitor.endOperation(
                    "search", "TEST:CastFacetSearchTools.isEligibleFacetValue");
              }
              return false;
            }
          }

        } else {
          result = super.isEligibleFacetValue(pFacetTrail, pFacetValue);
          break;
        }
      }
    } // end if
    if (PerformanceMonitor.isEnabled()) {
      PerformanceMonitor.endOperation("search", "TEST:CastFacetSearchTools.isEligibleFacetValue");
    }
    return result;
  }
  /**
   * Forms correct trails for ajouters(pop-ups). Save ajouter's trail as map :ajouter id to trail.
   * If trail is empty or contains only one value(case for product listing (we don't show pivot
   * category facet at the UI)) so no one facet value was selected, so re-running search for pop-up
   * isn't necessary. the same situation for question parameter(search re-run isn't necessary).
   *
   * @param pRequest dynamo http request
   * @param pResponse dynamo http response
   * @throws ServletException
   * @throws IOException
   */
  public void service(DynamoHttpServletRequest pRequest, DynamoHttpServletResponse pResponse)
      throws ServletException, IOException {
    try {
      if (PerformanceMonitor.isEnabled()) {
        PerformanceMonitor.startOperation("search", "TEST:FacetedSearchPopUpFinder.service");
      }
      String initialTrail = pRequest.getParameter(TRAIL);

      if ((getCommerceFacetTrailTools() != null)
          && !StringUtils.isBlank(initialTrail)
          && (initialTrail.indexOf(":") != initialTrail.lastIndexOf(":"))) {
        List<FacetValue> trails =
            getCommerceFacetTrailTools().parseFacetValueString(initialTrail, null);

        Map<String, ArrayList<String>> facetIdToFacetValues =
            new HashMap<String, ArrayList<String>>();

        String firstSafeFacet;

        /*
         * pivot category || SRCH facet || root catgory facet - should stay the same; if this facet will be multiple
         * (facetId:facetValue1|facetValue2|...) So do check, the reason of this is .getTrailString() method returns
         * not correct value
         */
        if (trails.get(0) instanceof DisjunctionMultiValue) {
          firstSafeFacet = trails.get(0).toString();
          trails.remove(0);
        } else {
          firstSafeFacet = trails.get(0).getTrailString();
          trails.remove(0);
        }

        if (trails != null) {
          for (FacetValue fv : trails) {
            facetIdToFacetValues.put(fv.getFacet().getId(), new ArrayList());
          }

          for (FacetValue fv : trails) {
            if (facetIdToFacetValues.get(fv.getFacet().getId()) != null) {
              if (fv instanceof DisjunctionMultiValue) {
                ((ArrayList) facetIdToFacetValues.get(fv.getFacet().getId())).add(fv.toString());
              } else {
                ((ArrayList) facetIdToFacetValues.get(fv.getFacet().getId()))
                    .add(fv.getTrailString());
              }
            }
          }
          Map<String, String> resultedMapFacetIdToTrail = new HashMap<String, String>();

          // trails for queries
          for (Map.Entry<String, ArrayList<String>> excludedFacet :
              facetIdToFacetValues.entrySet()) {
            StringBuffer inludedFacetsSB = new StringBuffer();

            for (Map.Entry<String, ArrayList<String>> inludedFacets :
                facetIdToFacetValues.entrySet()) {
              if (!excludedFacet.getKey().equalsIgnoreCase(inludedFacets.getKey())) {
                for (String str : inludedFacets.getValue()) {
                  inludedFacetsSB.append(str).append(":");
                }
              }
            }

            // prepend first facet value (srch || pivot || root category)
            inludedFacetsSB.insert(0, ":").insert(0, firstSafeFacet);

            // delete extra last ":"
            if (inludedFacetsSB.lastIndexOf(":") == (inludedFacetsSB.length() - 1)) {
              inludedFacetsSB.replace(
                  inludedFacetsSB.lastIndexOf(":"), inludedFacetsSB.length(), "");
            }

            resultedMapFacetIdToTrail.put(excludedFacet.getKey(), inludedFacetsSB.toString());
          } // end for

          pRequest.setParameter(RESULTED_MAP_FACET_ID_TO_TRAIL, resultedMapFacetIdToTrail);
        } // end if
      } // end if
    } finally {
      if (PerformanceMonitor.isEnabled()) {
        PerformanceMonitor.endOperation("search", "TEST:FacetedSearchPopUpFinder.service");
      }
    } // end try-finally
    pRequest.serviceLocalParameter(OUTPUT, pRequest, pResponse);
  }