package com.capgemini.reports.definition.r27report;

import com.capgemini.framework.teamcenter.authent.TcSession;
import com.capgemini.reports.definition.r27report.ExecutionR27.ReportDataR27;
import com.capgemini.reports.execution.mapconstructor.AbstractStaticTemplateMapConstructor;
import com.capgemini.reports.execution.mapconstructor.IMapConstructor;
import com.capgemini.reports.printing.PrintParameters;
import com.teamcenter.soa.client.model.strong.Item;
import lombok.extern.slf4j.Slf4j;

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

/**
 * Extracts the relevant data obtained from TC into the expected List of Maps for Jasper.
 */
@Slf4j
public class R27Printer extends AbstractStaticTemplateMapConstructor<ExecutionR27, ExecutionR27.ReportDataR27> implements IMapConstructor<ExecutionR27, ExecutionR27.ReportDataR27> {

  private static final long serialVersionUID = 4281716591716247163L;

  /**
   * Puts the data gathered by an {@link ExecutionR27} into some {@link PrintParameters} for printing.
   *
   * @param reportData the data-bearing {@link ReportDataR27}
   * @param mainReport the {@link PrintParameters}
   */
  private void print(final ReportDataR27 reportData, final PrintParameters mainReport) {
    List<Item> parts = reportData.getParts();
    if (parts.isEmpty()) {
      mainReport.addReportData(setupEmptyData());
    }
    else {
      List<Map<R27Keys, ?>> reportDataMap = new ArrayList<>();
      for (Item part : parts) {
        Map<R27Keys, String> map = new EnumMap<>(R27Keys.class);
        map.put(R27Keys.ITEM_ID, part.get_item_id());
        reportDataMap.add(map);
      }
      mainReport.addReportDataFromEnum(reportDataMap);
    }
  }

  private static List<Map<String, ?>> setupEmptyData() {
    List<Map<String, ?>> list = new ArrayList<>();
    list.add(new HashMap<String, Object>());
    return list;
  }

  @Override
  public void constructDataMaps(TcSession session, PrintParameters printParameters, ReportDataR27 reportData) {
    print(reportData, printParameters);
  }
}
