/**
   * This method is going to return a Map with the End template records loaded from the external
   * BaseData xml file
   *
   * @param inputDto
   * @return
   */
  public static Map<String, NavigationPointerDto> loadEndTemplatePattern(
      LoadEndTemplatePatternInputDto inputDto) {
    Map<String, NavigationPointerDto> Navigation_BaseDataDto =
        new LinkedHashMap<String, NavigationPointerDto>();

    if (LOG.isDebugEnabled()) {
      LOG.debug(
          "UtilityNavigationPointerEnd loadEndTemplatePattern() inputDto.getMapOptionSetIdNumber() : "
              + inputDto.getMapOptionSetIdNumber());
    }

    UtilityNavigationPointerEnd.validateInputDto(inputDto);

    if (UtilityNavigationPointerEnd.validateInputDtoData(inputDto)) {
      try {
        Map<String, BaseDataDto> listBaseData =
            inputDto.getEndTemplateList().get(inputDto.getPrinterType());

        /**
         * We are going to group all the end BaseData Records on the map of navigateEndDataMap,
         * using the page xml node data as a key and as a value the list of baseData records
         */
        Map<String, Map<String, BaseDataDto>> navigateEndDataMap =
            UtilityNavigationPointerEnd.groupLastRecords(listBaseData);

        String endTemplateStartPage =
            UtilityNavigationPointerEnd.getFirstEndTemplatePageNumber(inputDto);
        int countInitOptionSetId =
            UtilityNavigationPointerEnd.getLastFirstOptionSetId(inputDto, endTemplateStartPage);
        int lastEndProdAccOptionSetId =
            inputDto.getMapOptionSetIdNumber().get(endTemplateStartPage).getOptionSetId();

        /*
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("UtilityNavigationPointerEnd loadEndTemplatePattern() endTemplateStartPage : " + endTemplateStartPage);
                            LOG.debug("UtilityNavigationPointerEnd loadEndTemplatePattern() countInitOptionSetId : " + countInitOptionSetId);
                            LOG.debug("UtilityNavigationPointerEnd loadEndTemplatePattern() navigateEndDataMap.size : " + navigateEndDataMap.size());
                            LOG.debug("UtilityNavigationPointerEnd loadEndTemplatePattern() lastEndProdAccOptionSetId : " + lastEndProdAccOptionSetId);
                        }
        */
        NavigableMap<String, OptionSetIdNumber> groupEndOptionSetNumbers =
            UtilityNavigationPointerEnd.getGroupEndOptionSetNumbers(inputDto, endTemplateStartPage);

        int procOptionSetIdProcInit =
            inputDto.getMapOptionSetIdNumber().get("proc").getOptionSetId();

        while (countInitOptionSetId <= lastEndProdAccOptionSetId) {

          for (Map.Entry<String, OptionSetIdNumber> entryOptionSetId :
              groupEndOptionSetNumbers.entrySet()) {

            Map<String, String> mapOptionSetIdToUpdate = new LinkedHashMap<String, String>();
            boolean panelCreated = false;

            if (LOG.isDebugEnabled()) {
              LOG.debug(
                  "UtilityNavigationPointerEnd loadEndTemplatePattern() entryOptionSetId.getKey() : "
                      + entryOptionSetId.getKey());
              LOG.debug(
                  "UtilityNavigationPointerEnd loadEndTemplatePattern() navigateEndDataMap : "
                      + navigateEndDataMap);
            }

            for (Map.Entry<String, BaseDataDto> baseDataDto :
                navigateEndDataMap.get(entryOptionSetId.getKey()).entrySet()) {
              String nextPageValue = "";

              NavigationPointerDto navigationPointerDto = new NavigationPointerDto();
              navigationPointerDto.setDatasource("" + Rp3Constants.CODES.END_TEMPLATE_DATA_SOURCE);
              navigationPointerDto.setDataSourceRefId(baseDataDto.getKey());
              navigationPointerDto.setPage(String.valueOf(entryOptionSetId.getKey()));
              navigationPointerDto.setStreamId(inputDto.getStreamId());

              if (!"proc".equalsIgnoreCase(entryOptionSetId.getKey())) {

                Map.Entry<String, OptionSetIdNumber> nextOptionSetIdPanelEntry =
                    groupEndOptionSetNumbers.higherEntry(entryOptionSetId.getKey());
                if (nextOptionSetIdPanelEntry != null
                    && !"proc".equalsIgnoreCase(nextOptionSetIdPanelEntry.getKey())) {
                  if (nextOptionSetIdPanelEntry != null && panelCreated == false) {
                    nextOptionSetIdPanelEntry.getValue().incrementOptionSetIdByOne();
                    nextPageValue =
                        nextOptionSetIdPanelEntry.getKey()
                            + "."
                            + nextOptionSetIdPanelEntry.getValue().getOptionSetId();
                    panelCreated = true;
                  } else if (nextOptionSetIdPanelEntry == null) {
                    nextPageValue = baseDataDto.getValue().getNextPanel();
                  } else if (panelCreated) {
                    nextPageValue =
                        nextOptionSetIdPanelEntry.getKey()
                            + "."
                            + nextOptionSetIdPanelEntry.getValue().getOptionSetId();
                  }
                } else if (nextOptionSetIdPanelEntry != null
                    && "proc".equalsIgnoreCase(nextOptionSetIdPanelEntry.getKey())) {
                  // We need to increment by one each proc number
                  nextOptionSetIdPanelEntry.getValue().incrementOptionSetIdByOne();
                  nextPageValue =
                      nextOptionSetIdPanelEntry.getKey()
                          + "."
                          + nextOptionSetIdPanelEntry.getValue().getOptionSetId();
                }

                navigationPointerDto.setPageOptionSetID(String.valueOf(countInitOptionSetId));
              } else {
                String pageToCheck =
                    baseDataDto.getValue().getPage()
                        + "."
                        + baseDataDto.getValue().getPageOptionSetID();
                if (!mapOptionSetIdToUpdate.containsKey(pageToCheck)) {
                  mapOptionSetIdToUpdate.put(
                      pageToCheck, String.valueOf(++procOptionSetIdProcInit));
                }

                navigationPointerDto.setPageOptionSetID(mapOptionSetIdToUpdate.get(pageToCheck));
                nextPageValue = baseDataDto.getValue().getNextPanel();
              }

              navigationPointerDto.setNextPage(nextPageValue);
              navigationPointerDto.setPrinterType(inputDto.getPrinterType());

              navigationPointerDto.inputType = baseDataDto.getValue().getHtmlInputType();
              navigationPointerDto.webFriendlyName = baseDataDto.getValue().getLabel();
              Navigation_BaseDataDto.put(UtilityRefIdGenerator.buildRefId(), navigationPointerDto);
            }
          }
          countInitOptionSetId++;
        }
      } catch (RuntimeException re) {
        LOG.error("Error UtilityTemplateEndPattern getEndTemplateMap() " + re.getMessage(), re);
        StatusMessage statusMessage =
            new StatusMessage(
                Rp3Constants.CODES_MSG.STATUS_MSG_LEVEL_METHOD.toString(),
                MsgConstants.UNPE.STATUS_MSG_ERROR.toString(),
                Arrays.asList(re.getMessage()),
                Integer.valueOf(Rp3Constants.CODES_MSG.STATUS_MSG_LEVEL_EXCEPTION.toString()));
        inputDto.getStatusMessagingList().add(statusMessage);
      }
    }

    return Navigation_BaseDataDto;
  }
  /**
   * This method process text file for the qualifying question layout format
   *
   * @param res
   * @param sc
   * @param contRow
   * @param listColumnsNames
   */
  private static void parseQqLayout(
      ParseOutputObject res, Scanner sc, Integer contRow, List<String> listColumnsNames) {
    Integer contCol;
    QualifyingQuestionParseObjectDto qualifyingQuestionParseObjectDto =
        new QualifyingQuestionParseObjectDto();
    List<QualifyingQuestionModelDto> qualifyingQuestionModelDtoList =
        new ArrayList<QualifyingQuestionModelDto>();
    List<QualifyingQuestionDto> qualifyingQuestionDtoList = new ArrayList<QualifyingQuestionDto>();
    Map<String, Integer> mapTabCoordinate = new LinkedHashMap<String, Integer>();
    Map<String, QualifyingQuestionDto> lstQqDto =
        new LinkedHashMap<String, QualifyingQuestionDto>();
    Map<String, QualifyingQuestionModelDto> lstQqModelDto =
        new LinkedHashMap<String, QualifyingQuestionModelDto>();

    while (sc.hasNextLine()) {
      String valueX = sc.nextLine();
      contCol = 0;
      QualifyingQuestionDto qualifyingQuestionDto = null;
      QualifyingQuestionModelDto qualifyingQuestionModelDto = null;

      if (valueX.isEmpty() == false) {
        if (contRow == 0) {
          Scanner sc1 = new Scanner(valueX);
          sc1.useDelimiter("\\t");
          while (sc1.hasNext()) {
            String valueY = sc1.next();
            if (valueY != null && valueY.isEmpty() == false) {
              listColumnsNames.add(valueY);
              if (LOG.isDebugEnabled()) {
                LOG.debug("Rp3UtilityParser parse listColumnsNames : " + listColumnsNames);
              }
              mapTabCoordinate.put(valueY, contCol);
            }
            contCol++;
          }
          if (LOG.isDebugEnabled()) {
            LOG.debug("Rp3UtilityParser parse mapTabCoordinate : " + mapTabCoordinate);
          }
        } else {
          String[] arrayColsData = valueX.split("\\t");
          if (arrayColsData.length > 0) {

            if ((mapTabCoordinate.get("Panel ID") != null
                    && !arrayColsData[mapTabCoordinate.get("Option Labels")].isEmpty())
                && (mapTabCoordinate.get("Link to Panel") != null
                    && !arrayColsData[mapTabCoordinate.get("Link to Panel")].isEmpty())) {
              qualifyingQuestionDto = new QualifyingQuestionDto();
              qualifyingQuestionDto.setPanelID(arrayColsData[mapTabCoordinate.get("Panel ID")]);
              qualifyingQuestionDto.setOptionLabels(
                  arrayColsData[mapTabCoordinate.get("Option Labels")]);
              qualifyingQuestionDto.setLinkToPanel(
                  arrayColsData[mapTabCoordinate.get("Link to Panel")]);
              qualifyingQuestionDto.setCategoryGroup(
                  arrayColsData[mapTabCoordinate.get("Category Group")]);
              qualifyingQuestionDto.setDescription(
                  arrayColsData[mapTabCoordinate.get("Description")]);
              qualifyingQuestionDto.setRefId(UtilityRefIdGenerator.buildRefId());
            }
            if (LOG.isDebugEnabled()) {
              LOG.debug(
                  "Rp3UtilityParser parse arrayColsData : "
                      + Arrays.toString(arrayColsData)
                      + " length : "
                      + arrayColsData.length);
              LOG.debug("Rp3UtilityParser parse mapTabCoordinate : " + mapTabCoordinate);
            }

            if (arrayColsData.length == 9
                && (mapTabCoordinate.get("Panel ID_2") != null
                    && !arrayColsData[mapTabCoordinate.get("Panel ID_2")].isEmpty())
                && !arrayColsData[mapTabCoordinate.get("Link to Model")].isEmpty()
                && !arrayColsData[mapTabCoordinate.get("Stream ID")].isEmpty()) {
              qualifyingQuestionModelDto = new QualifyingQuestionModelDto();
              qualifyingQuestionModelDto.setPanelID(
                  arrayColsData[mapTabCoordinate.get("Panel ID_2")]);
              qualifyingQuestionModelDto.setLinkToModel(
                  arrayColsData[mapTabCoordinate.get("Link to Model")]);
              qualifyingQuestionModelDto.setStreamID(
                  arrayColsData[mapTabCoordinate.get("Stream ID")]);
              qualifyingQuestionModelDto.setRefId(UtilityRefIdGenerator.buildRefId());
            }
          }
        }
        contRow++;
        if (qualifyingQuestionDto != null) {
          qualifyingQuestionDtoList.add(qualifyingQuestionDto);
          lstQqDto.put(qualifyingQuestionDto.getRefId(), qualifyingQuestionDto);
        }

        if (qualifyingQuestionModelDto != null) {
          qualifyingQuestionModelDtoList.add(qualifyingQuestionModelDto);
          lstQqModelDto.put(qualifyingQuestionModelDto.getRefId(), qualifyingQuestionModelDto);
        }
      }
    }

    qualifyingQuestionParseObjectDto.setLstQqDto(lstQqDto);
    qualifyingQuestionParseObjectDto.setLstQqModelDto(lstQqModelDto);

    res.setQualifyingQuestionParseObjectDto(qualifyingQuestionParseObjectDto);
  }