package org.openlmis.report.service;


import lombok.NoArgsConstructor;
import org.apache.ibatis.session.RowBounds;
import org.openlmis.core.domain.Facility;
import org.openlmis.core.service.FacilityService;
import org.openlmis.report.mapper.VaccineStockStatusMapper;
import org.openlmis.report.model.ResultRow;
import org.openlmis.report.model.params.VaccineStockStatusParam;
import org.openlmis.report.model.report.VaccineStockStatusReport;
import org.openlmis.report.util.SelectedFilterHelper;
import org.openlmis.report.util.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.openlmis.core.domain.RightName.MANAGE_EQUIPMENT_INVENTORY;

@Component
@NoArgsConstructor
public class VaccineStockStatusReportDataProvider extends ReportDataProvider {

    @Autowired
    private VaccineStockStatusMapper reportMapper;

    @Autowired
    private SelectedFilterHelper filterHelper;
    @Autowired
    private FacilityService facilityService;

    @Override
    public List<? extends ResultRow> getReportBody(Map<String, String[]> filterCriteria, Map<String, String[]> sortCriteria, int page, int pageSize) {
        RowBounds rowBounds = new RowBounds((page - 1) * pageSize, pageSize);
/*
        List<VaccineStockStatusReport>statusReports = reportMapper.getReport(getReportFilterData(filterCriteria), rowBounds);

        List<VaccineStockStatusReport> stockStatusReportList = new ArrayList<>();
        VaccineStockStatusReport vaccineStockStatusReport = new VaccineStockStatusReport();
        vaccineStockStatusReport.setAdequacy(getTotal(statusReports));
        stockStatusReportList.add(vaccineStockStatusReport);*/

        List<VaccineStockStatusReport> stockStatusReports = reportMapper.getReport(getReportFilterData(filterCriteria),rowBounds);
        System.out.println("cahahhahahahahhaha");
        System.out.println(stockStatusReports
        );
        List<VaccineStockStatusReport> stockStatusReportList = new ArrayList<>();

        VaccineStockStatusReport vaccineStockStatusReport = new VaccineStockStatusReport();
        vaccineStockStatusReport.setAdequacy(getTotal(stockStatusReports));
        stockStatusReportList.add(vaccineStockStatusReport);
        System.out.println("----------------------");
        System.out.println(vaccineStockStatusReport.getAdequacy());
       return stockStatusReports;

    }

    public int getTotal(List<VaccineStockStatusReport> stockStatusReport){
        int total = 0;
        for (VaccineStockStatusReport aStockStatusReport : stockStatusReport) {

            total += aStockStatusReport.getAdequacy();
        }
        return (total / stockStatusReport.size()) * 100;
    }
    public VaccineStockStatusParam getReportFilterData(Map<String, String[]> filterCriteria) {

        VaccineStockStatusParam param = new VaccineStockStatusParam();

        Long programId = StringHelper.isBlank(filterCriteria, "program") ? 0L : Long.parseLong(filterCriteria.get("program")[0]);
        param.setProgram(programId);
        param.setFacilityLevel(filterCriteria.get("facilityLevel")[0]);
        String productCategory = StringHelper.isBlank(filterCriteria, "productCategory") ? null : ((String[]) filterCriteria.get("productCategory"))[0];
        param.setProductCategory(productCategory);

        String statusDate = StringHelper.isBlank(filterCriteria, "statusDate") ? null : ((String[]) filterCriteria.get("statusDate"))[0];
        param.setStatusDate(statusDate);

        String products = StringHelper.isBlank(filterCriteria, "products") ? null : ((String[]) filterCriteria.get("products"))[0];
        param.setProducts(products);


        List<Facility> facilities = facilityService.getUserSupervisedFacilities(this.getUserId(), programId, MANAGE_EQUIPMENT_INVENTORY);
        facilities.add(facilityService.getHomeFacility(this.getUserId()));

        StringBuilder str = new StringBuilder();
        str.append("{");
        for (Facility f : facilities) {
            str.append(f.getId());
            str.append(",");
        }
        if (str.length() > 1) {
            str.deleteCharAt(str.length() - 1);
        }
        str.append("}");
        param.setFacilityIds(str.toString());

        return param;
    }

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




@@ -653,36 +653,8 @@
 t;%0A%0A
-import java.util.ArrayList;%0A
 impo
@@ -672,16 +672,16 @@
 l.List;%0A
+
 import j
@@ -1320,1151 +1320,92 @@
 e);%0A
-/*%0A
        
- List%3CVaccineStockStatusReport%3EstatusReports = reportMapper.getReport(getReportFilterData(filterCriteria), rowBounds);%0A%0A        List%3CVaccineStockStatusReport%3E stockStatusReportList = new ArrayList%3C%3E();%0A        VaccineStockStatusReport vaccineStockStatusReport = new VaccineStockStatusReport();%0A        vaccineStockStatusReport.setAdequacy(getTotal(statusReports));%0A        stockStatusReportList.add(vaccineStockStatusReport);*/%0A%0A        List%3CVaccineStockStatusReport%3E stockStatusReports = reportMapper.getReport(getReportFilterData(filterCriteria),rowBounds);%0A        System.out.println(%22cahahhahahahahhaha%22);%0A        System.out.println(stockStatusReports%0A        );%0A        List%3CVaccineStockStatusReport%3E stockStatusReportList = new ArrayList%3C%3E();%0A%0A        VaccineStockStatusReport vaccineStockStatusReport = new VaccineStockStatusReport();%0A        vaccineStockStatusReport.setAdequacy(getTotal(stockStatusReports));%0A        stockStatusReportList.add(vaccineStockStatusReport);%0A        System.out.println(%22----------------------%22);%0A        System.out.println(vaccineStockStatusReport.getAdequacy());%0A       return stockStatusReports
+return  reportMapper.getReport(getReportFilterData(filterCriteria),rowBounds)
 ;%0A%0A 

Remove unnecessary code in vaccine stock status report