package com.capgemini.reports.formatting;

import com.capgemini.reports.database.javabean.DbReport;
import com.capgemini.reports.database.javabean.enumeration.ReportStateEnum;
import com.capgemini.reports.database.manager.MngReport.RunningReportEntry;
import com.capgemini.reports.exceptions.InvalidInputException;
import com.capgemini.reports.exceptions.NothingToBeShownException;
import com.capgemini.reports.quartz.SchedulerServer.JobExecution;
import org.apache.commons.lang.StringEscapeUtils;
import org.hibernate.Session;

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

/**
 * This {@link AbstractTableFormatter} can generate an html page (using {@link AbstractTableFormatter#makeJsContent() makeJsContent} method).
 * <p>
 * It generates a table with five columns:
 * <ol>
 * <li>Every reports waiting to be executed in low priority queue</li>
 * <li>Every reports waiting to be executed in med priority queue</li>
 * <li>Every reports waiting to be executed in high priority queue</li>
 * <li>The report requests that have been delayed to tonight</li>
 * <li>The report schedulings whose execution will trigger tonight.</li>
 * </ol>
 */
public class QueueStatus extends AbstractTableFormatter {
    private final List<RunningReportEntry> theWaitingReports;
    private final List<JobExecution> tonightJobs;

    /**
     * Make an instance to create a html page contents. <br>
     * The contents are the reports in the queues, and the jobs that will trigger tonight.
     * 
     * @param theWaitingReports a {@link list} of {@link RunningReportEntry}, obtained from DB, reflecting HornetQ's status.
     * @param tonightJobs a {@link List} of {@link JobExecution} form Quartz.
     */
    public QueueStatus(final List<RunningReportEntry> theWaitingReports, final List<JobExecution> tonightJobs) {
        super();
        this.theWaitingReports = theWaitingReports;
        this.tonightJobs = tonightJobs;
    }

    @Override
    protected String makeTable(Session session) throws InvalidInputException {
        if (theWaitingReports.isEmpty() && tonightJobs.isEmpty()) {
            throw new NothingToBeShownException("No Quartz Jobs will execute today", "Aucun rapport en attente");
        }
        JsTableCreator reportArray = new JsTableCreator("reportArray");
        for (RunningReportEntry currentReport : theWaitingReports) {
            String safename = currentReport.getSafeName();
            DbReport dbReport = currentReport.getReport();
            Map<String, String> lineMap = new HashMap<>();
          lineMap.put("login", StringEscapeUtils.escapeJavaScript(safename));
            lineMap.put("priority",
                    ReportStateEnum.WAIT.toString().equals(dbReport.getState()) ? "0" : String.valueOf(currentReport.getReportPriority()));
            reportArray.addLineAndBean(lineMap, dbReport);
        }
        StringBuilder fullTable = new StringBuilder(reportArray.toString());

        // Add code schedulingReportArray
        JsTableCreator schedulingReportArray = new JsTableCreator("schedulingReportArray");
        for (JobExecution runningSchedulingReportEntry : tonightJobs) {
            Map<String, String> reportEntryAsMap = new HashMap<>();
            reportEntryAsMap.put("reportCode", runningSchedulingReportEntry.getReportCode().getReportCode());
            reportEntryAsMap.put("name", runningSchedulingReportEntry.getPlanificationName());
            schedulingReportArray.addLine(reportEntryAsMap);
        }
        fullTable.append(schedulingReportArray);

        return fullTable.toString();
    }

    @Override
    protected String successPageTemplateLocation() {
        return "/html/queueStatus/queue_status_success.html";
    }

    @Override
    protected String failedPageTemplateLocation() {
        return "/html/queueStatus/queue_status_failed.html";
    }

    @Override
    protected void addOptionalTags(Session session) {
        // Nothing to do for QueueStatus
    }

    @Override
    protected void addOptionalErrorTags() {
        // Nothing to do for QueueStatus
    }
}
