package com.capgemini.reports.common.scheduling.view;

import com.capgemini.reports.database.HibernateUtil;
import com.capgemini.reports.database.javabean.DbSchedulingCriteriaVal;
import com.capgemini.reports.database.javabean.DbSchedulingReport;
import com.capgemini.reports.database.manager.MngSchedulingCriteriaVal;
import com.capgemini.reports.database.manager.MngSchedulingReport;
import com.capgemini.reports.exceptions.InvalidInputException;
import com.capgemini.reports.periodicity.SchedulingPeriod;
import com.hp.gagawa.java.elements.Div;
import com.hp.gagawa.java.elements.Input;

import lombok.Getter;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * This formatter page can display the existing scheduling data of a SchedulingReport from the DB. It is used in the 'update scheduling' page.
 */
@Getter
public class UpdateSchedulingFormatter extends SchedulingFormatter {

  private final Long reportId;

  /**
   * Builds a formatter instance, that can display the 'update scheduling' page of the provided Scheduling Report ID.
   *
   * @param reportId the id of a {@link DbSchedulingReport} from the DB
   */
  public UpdateSchedulingFormatter(final Long reportId) {
    super();
    this.reportId = reportId;
  }

  /**
   * Builds the html content that will show the scheduling's parameters, in html.
   */
  public String showReportUpdatePage() throws InvalidInputException {
    SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    Session session = sessionFactory.openSession();
    try {
      final String uncheckedSchedulingName = new MngSchedulingReport().searchById(session, reportId).getSchedulingName();
      String theSchedulingName = uncheckedSchedulingName == null ? "" : uncheckedSchedulingName;

      SchedulingPeriod thePeriodicity = new MngSchedulingCriteriaVal().getPeriod(session, reportId);

      // hidden id of scheduling report
      Input hidden = new Input().setType("hidden").setName("id").setValue(String.valueOf(reportId));

      // scheduling name
      Div containerSchedulingName = getSchedulingName(theSchedulingName);

      // Period (daily / weekly / weekend / date)
      Div containerPeriod = getPeriod(thePeriodicity.getPeriodicity(), reportId).setId("div_container_period").setCSSClass(SUB_TITLE_PLANIFICATION);

      // Sub period (day in week / weekend num / day in month / date
      Div containerSubPeriod = new Div();
      containerSubPeriod.setId("div_container_subPeriod").setStyle(MARGIN_TOP_10PX).setCSSClass(SUB_TITLE_SUB_PERIOD);

      // Active subPeriod
      Div containerAllSubPeriods = thePeriodicity.accept(this);

      containerSubPeriod.appendChild(containerAllSubPeriods);

      // Parent
      Div parent = new Div();
      parent.setId("div_temporal");
      parent.appendChild(hidden);
      parent.appendChild(containerSchedulingName);
      parent.appendChild(containerPeriod);
      parent.appendChild(containerSubPeriod);

      return parent.write();
    }
    finally {
      session.close();
    }
  }
}
