/*
 * This program was produced for the U.S. Agency for International Development. It was prepared by the USAID | DELIVER PROJECT, Task Order 4. It is part of a project which utilizes code originally licensed under the terms of the Mozilla Public License (MPL) v2 and therefore is licensed under MPL v2 or later.
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the Mozilla Public License as published by the Mozilla Foundation, either version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Mozilla Public License for more details.
 *
 * You should have received a copy of the Mozilla Public License along with this program. If not, see http://www.mozilla.org/MPL/
 */

package org.openlmis.report.service;

import lombok.NoArgsConstructor;
import org.apache.ibatis.session.RowBounds;
import org.openlmis.core.service.ConfigurationSettingService;
import org.openlmis.report.mapper.OrderSummaryReportMapper;
import org.openlmis.report.model.ReportData;
import org.openlmis.report.model.filter.OrderReportFilter;
import org.openlmis.report.util.Constants;
import org.openlmis.report.util.InteractiveReportPeriodFilterParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

@Component
@NoArgsConstructor
public class OrderSummaryReportDataProvider extends ReportDataProvider {

  @Autowired
  private OrderSummaryReportMapper reportMapper;

  @Autowired
  private ConfigurationSettingService configurationService;

  private OrderReportFilter orderReportFilter;

  @Override
  protected List<? extends ReportData> getBeanCollectionReportData(Map<String, String[]> filterCriteria) {

      return getReportDataByFilterCriteriaAndPagingAndSorting(filterCriteria,filterCriteria,RowBounds.NO_ROW_OFFSET, RowBounds.NO_ROW_LIMIT);
     // return reportMapper.getReportData(filterCriteria);
  }

  @Override
  protected List<? extends ReportData> getResultSetReportData(Map<String, String[]> filterCriteria) {
      return getReportDataByFilterCriteriaAndPagingAndSorting(filterCriteria,filterCriteria,RowBounds.NO_ROW_OFFSET, RowBounds.NO_ROW_LIMIT);
  }

  @Override
  public List<? extends ReportData> getReportDataByFilterCriteriaAndPagingAndSorting(Map<String, String[]> filterCriteria, Map<String, String[]> SortCriteria, int page, int pageSize) {
      RowBounds rowBounds = new RowBounds((page-1)*pageSize,pageSize);
      return reportMapper.getFilteredSortedPagedOrderSummaryReport(getReportFilterData(filterCriteria),SortCriteria, rowBounds);
  }

  public OrderReportFilter getReportFilterData(Map<String, String[]> filterCriteria) {

    if(filterCriteria != null ){
        orderReportFilter = new OrderReportFilter();

        orderReportFilter.setFacilityTypeId(filterCriteria.get("facilityTypeId") == null ? 0 : Integer.parseInt(filterCriteria.get("facilityTypeId")[0])); //defaults to 0
        orderReportFilter.setFacilityId(filterCriteria.get("facilityId") == null ? 0 : Integer.parseInt(filterCriteria.get("facilityId")[0])); //defaults to 0
        orderReportFilter.setFacilityType( (filterCriteria.get("facilityType") == null || filterCriteria.get("facilityType")[0].equals("")) ? "ALL Facilities" : filterCriteria.get("facilityType")[0]);

        orderReportFilter.setScheduleId(filterCriteria.get("scheduleId") == null ? 0 : Integer.parseInt(filterCriteria.get("scheduleId")[0])); //defaults to 0
        orderReportFilter.setSchedule(filterCriteria.get("schedule")[0]);

        orderReportFilter.setProductId(filterCriteria.get("productId") == null ? 0 : Integer.parseInt(filterCriteria.get("productId")[0])); //defaults to 0
        if(orderReportFilter.getProductId() == 0){
            orderReportFilter.setProduct("All Products");
        }else if(orderReportFilter.getProductId() == -1){
            orderReportFilter.setProduct(configurationService.getConfigurationStringValue(Constants.CONF_INDICATOR_PRODUCTS).isEmpty() ? "Indicator Products" : configurationService.getConfigurationStringValue(Constants.CONF_INDICATOR_PRODUCTS));
        }
        orderReportFilter.setOrderType(filterCriteria.get("orderType") == null ? "" : filterCriteria.get("orderType")[0]);
        orderReportFilter.setPeriodId(filterCriteria.get("periodId") == null ? 0 : Integer.parseInt(filterCriteria.get("periodId")[0])); //defaults to 0
        orderReportFilter.setPeriod(filterCriteria.get("period")[0]);
        orderReportFilter.setProgramId(filterCriteria.get("programId") == null ? 0 : Integer.parseInt(filterCriteria.get("programId")[0])); //defaults to 0
        orderReportFilter.setProgram(filterCriteria.get("program")[0]);
      }
      return orderReportFilter;
  }

  @Override
  public String getFilterSummary(Map<String, String[]> params){
    return getReportFilterData(params).toString();
  }

}

@@ -1303,77 +1303,8 @@
 ts;%0A
-import org.openlmis.report.util.InteractiveReportPeriodFilterParser;%0A
 impo
@@ -1433,16 +1433,15 @@
 til.
-Calendar
+HashMap
 ;%0Aim
@@ -5033,10 +5033,285 @@
 );%0A  %7D%0A%0A
+  @Override%0A  public HashMap%3CString, String%3E getAdditionalReportData(Map params)%7B%0A    HashMap%3CString, String%3E result = new HashMap%3CString, String%3E() ;%0A    result.put(%22ADDRESS%22, configurationService.getConfigurationStringValue(%22ORDER_REPORT_ADDRESS%22));%0A    return result;%0A  %7D%0A
 %7D%0A

complete externalizing the address field on order summary report.