package com.capgemini.reports.formatting.statsgen;

import com.capgemini.reports.ReportEnum;
import com.capgemini.reports.database.stats.GenJspStats;
import com.capgemini.reports.database.stats.UserStatBean;
import org.hibernate.Session;

import javax.servlet.annotation.WebServlet;
import java.text.ParseException;
import java.util.List;

/**
 * This class generates a CSV file whose contents are statistics on the number of generations performed by each user, on each report.
 */
@WebServlet("/downloadStats1")
public class ExecutionCountTableGenerator extends AbstractTableGenerator {

  private static final long serialVersionUID = 3930084692591665232L;

  /**
   * Table implementation for report count per code & user login.
   **/
  @Override
  public String generateTableAsCSV(final String aStartPeriod, final String aEndPeriod) throws ParseException {
    StringBuilder csvContent = new StringBuilder("Utilisateur");
    List<String> allReportCode = ReportEnum.getListReportCode();
    for (String reportCode : allReportCode) {
      csvContent.append(',').append(reportCode);
    }
    csvContent.append(",Global\n");

    // UserStatBean [name=Found Not, properties={R01=22, R03=8, R05=, R06=, R07=48, R10=7, R11=3, R13=87, R15=20, R16=12, R17=15, R19=2, R20=19, R21=7, R22=10, month=11, total=260, year=2015}]]
    for (UserStatBean user : GenJspStats.generateLaunchCountTableForCsv(aStartPeriod, aEndPeriod)) {
      csvContent.append(user.getName()).append(',');
      for (String reportCode : allReportCode) {
        csvContent.append(user.getDatas().get(reportCode)).append(',');
      }
      csvContent.append(user.getDatas().get("total"));
      csvContent.append('\n');
    }

    return csvContent.toString();
  }

  @Override
  protected String getFilename() {
    return "StatsNombreExecutions";
  }

}
