package com.capgemini.reports.emailing.templates;

import com.capgemini.framework.common.TeamcenterDateFormatter;
import com.capgemini.framework.common.activedirectory.ActiveDirectory;
import com.capgemini.reports.database.javabean.DbUser;
import com.capgemini.reports.execution.AbstractExecutionReport;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * This class will allow to send different types of mail to the user.
 */
@Slf4j
public final class EmailReportHelper {

    private EmailReportHelper() {
        // Cannot be called
    }

  /**
   * Sends a mail to warn the user that his report has successfully completed and may be retrieved.
   *
   * @param downloadLink             the Location where the file has been stored
   * @param aAbstractExecutionReport will provide the report concerned
   * @param users                    the {@link List} of {@link DbUser} that have subscribed to this report
   */
  public static void sendEndEmail(final String downloadLink, final AbstractExecutionReport<?, ?> aAbstractExecutionReport,
                                  final List<DbUser> users) {
    // Register current time as the posting time
    aAbstractExecutionReport.setEndTime(TeamcenterDateFormatter.getCurrentTime());
    String completeReportName = aAbstractExecutionReport.getReportIdentifier().buildCompleteReportName().toString();
    new SuccessfulEmailTemplate(completeReportName, aAbstractExecutionReport.getPostingTime(), aAbstractExecutionReport.getStartTime(),
        aAbstractExecutionReport.getEndTime(), downloadLink).send(users);
  }

  /**
   * Sends a mail to warn the user that his SCHEDULED report has successfully completed and may be retrieved.
   *
   * @param downloadLink             the Location where the file has been stored
   * @param aAbstractExecutionReport will provide the report concerned
   * @param users                    the {@link List} of {@link DbUser} that have subscribed to this report
   * @param schedulingName           String - the scheduling name of the SCHEDULED report
   */
  public static void sendScheduledEndEmail(final String downloadLink, final AbstractExecutionReport<?, ?> aAbstractExecutionReport,
                                           final List<DbUser> users, final String schedulingName) {
    // Register current time as the posting time
    aAbstractExecutionReport.setEndTime(TeamcenterDateFormatter.getCurrentTime());
    String completeReportName = aAbstractExecutionReport.getReportIdentifier().buildCompleteReportName().toString();
    new SuccessfulEmailTemplate(completeReportName, aAbstractExecutionReport.getPostingTime(), aAbstractExecutionReport.getStartTime(),
        aAbstractExecutionReport.getEndTime(), downloadLink, schedulingName).send(users);
  }

    /**
     * Sends a mail to warn the user that his report has failed to complete.
     *
     * @param errorMessage             the error message to include for the user
     * @param aAbstractExecutionReport will provide the report concerned
     * @param users                    the {@link List} of {@link DbUser} that have subscribed to this report
     */
    public static void sendErrorEmail(final String errorMessage, final AbstractExecutionReport<?, ?> aAbstractExecutionReport,
                                      final List<DbUser> users) {
        // Register current time as the error time
        aAbstractExecutionReport.setFailureTime(TeamcenterDateFormatter.getCurrentTime());
        String completeReportName = aAbstractExecutionReport.getReportIdentifier().buildCompleteReportName().toString();
        new FailedEmailTemplate(completeReportName, aAbstractExecutionReport.getPostingTime(), aAbstractExecutionReport.getStartTime(),
                aAbstractExecutionReport.getFailureTime(), errorMessage).send(users);
    }

    /**
     * Sends a mail to warn to the report admins that his report has failed to complete.
     *
     * @param errorMessage             the error message to include for the admin report
     * @param aAbstractExecutionReport will provide the report concerned
     */
    public static void sendErrorEmailToAdminsReport(final String errorMessage, final AbstractExecutionReport<?, ?> aAbstractExecutionReport) {
        // Register current time as the error time
        aAbstractExecutionReport.setFailureTime(TeamcenterDateFormatter.getCurrentTime());
        String completeReportName = aAbstractExecutionReport.getReportIdentifier().buildCompleteReportName().toString();
        List<DbUser> adminsReport = ActiveDirectory.getAdminsReportUpdateDatabase();
        if (adminsReport.isEmpty()) {
            log.error("The request for recovery of the admins report from active directory of SNECMA has returned nothing, on fail {} ",
                    aAbstractExecutionReport.getReportIdentifier());
        } else {
            new FailedEmailTemplateAdmin(completeReportName, aAbstractExecutionReport.getPostingTime(), aAbstractExecutionReport.getStartTime(),
                    aAbstractExecutionReport.getFailureTime(), errorMessage).send(adminsReport);
        }
    }

    /**
     * Sends a mail to warn the user that his report, launched by a workflow, has failed to complete.
     *
     * @param errorMessage             the error message to include for the user
     * @param aAbstractExecutionReport will provide the report concerned
     * @param users                    the {@link List} of {@link DbUser} that have subscribed to this report
     * @param relaunchURL              the URL to launch this report again
     */
    public static void sendWorkflowErrorEmail(final String errorMessage, final AbstractExecutionReport<?, ?> aAbstractExecutionReport,
                                              final List<DbUser> users, final String relaunchURL) {
        String reportName = aAbstractExecutionReport.getReportIdentifier().buildCompleteReportName().toString();
        String postingTime = aAbstractExecutionReport.getPostingTime();
        String startTime = aAbstractExecutionReport.getStartTime();
        String failureTime = aAbstractExecutionReport.getFailureTime();
      new FailedWorkflowEmailTemplate(reportName, postingTime, startTime, failureTime, errorMessage, relaunchURL).send(users);
    }

    /**
     * Sends a mail to warn to the report admin that his report (launched by workflow) has failed to complete.
     *
     * @param errorMessage             the error message to include for the admin report
     * @param aAbstractExecutionReport will provide the report concerned
     * @param adminsReport             the {@link List} of {@link DbUser} that have subscribed to this report
     * @param relaunchURL              the URL to use to re-launch the report
     */
    public static void sendWorkflowErrorEmailToAdminsReport(final String errorMessage, final AbstractExecutionReport<?, ?> aAbstractExecutionReport,
                                                            final List<DbUser> adminsReport, final String relaunchURL) {
        String reportName = aAbstractExecutionReport.getReportIdentifier().buildCompleteReportName().toString();
        String postingTime = aAbstractExecutionReport.getPostingTime();
        String startTime = aAbstractExecutionReport.getStartTime();
        String failureTime = aAbstractExecutionReport.getFailureTime();
        FailedWorkflowEmailTemplateAdmin emailTemplate = new FailedWorkflowEmailTemplateAdmin(reportName, postingTime, startTime, failureTime, errorMessage, relaunchURL);
        emailTemplate.send(adminsReport);
    }

    public static void sendScheduledErrorEmailToAdminsReport(String errorMessage, AbstractExecutionReport<?, ?> aAbstractExecutionReport) {
        // Register current time as the error time
        aAbstractExecutionReport.setFailureTime(TeamcenterDateFormatter.getCurrentTime());
        String completeReportName = aAbstractExecutionReport.getReportIdentifier().buildCompleteReportName().toString();
        List<DbUser> adminsReport = ActiveDirectory.getAdminsReportUpdateDatabase();
        if (adminsReport.isEmpty()) {
            log.error("The request for recovery of the admins report from active directory of SNECMA has returned nothing, on fail {} ",
                    aAbstractExecutionReport.getReportIdentifier());
        } else {
            new FailedEmailTemplateAdmin(completeReportName, aAbstractExecutionReport.getPostingTime(), aAbstractExecutionReport.getStartTime(),
                    aAbstractExecutionReport.getFailureTime(), errorMessage, aAbstractExecutionReport.getSchedulingName()).send(adminsReport);
        }
    }

    public static void sendScheduledErrorEmail(String errorMessage, AbstractExecutionReport<?, ?> aAbstractExecutionReport, List<DbUser> users) {
        // Register current time as the error time
        aAbstractExecutionReport.setFailureTime(TeamcenterDateFormatter.getCurrentTime());
        String completeReportName = aAbstractExecutionReport.getReportIdentifier().buildCompleteReportName().toString();
        new FailedEmailTemplate(completeReportName, aAbstractExecutionReport.getPostingTime(), aAbstractExecutionReport.getStartTime(),
                aAbstractExecutionReport.getFailureTime(), errorMessage, aAbstractExecutionReport.getSchedulingName()).send(users);
    }
}
