package org.exoplatform.cs.job;

import org.exoplatform.commons.Environment;
import org.exoplatform.commons.api.notification.NotificationContext;
import org.exoplatform.commons.api.notification.model.PluginKey;
import org.exoplatform.commons.notification.impl.NotificationContextImpl;
import org.exoplatform.container.ExoContainer;
import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.cs.dto.EnvironmentDTO;
import org.exoplatform.cs.dto.SpaceDTO;
import org.exoplatform.cs.dto.TicketDTO;
import org.exoplatform.cs.entity.StatLogEntity;
import org.exoplatform.cs.entity.TopicEntity;
import org.exoplatform.cs.integration.notification.CSReminderPlugin;
import org.exoplatform.cs.integration.notification.CSSubscriptionExpiredPlugin;
import org.exoplatform.cs.integration.notification.CSSubscriptionReminderPlugin;
import org.exoplatform.cs.service.CSSpaceService;
import org.exoplatform.cs.service.EnvironmentService;
import org.exoplatform.cs.service.StatsService;
import org.exoplatform.cs.service.TicketService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.List;

/**
 * Created by eXo Platform SAS.
 *
 * @author MedAmine
 */

public class ReminderNotificationJob implements Job {
  private static final Logger LOG = LoggerFactory.getLogger(ReminderNotificationJob.class);



  @Override
  public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    ExoContainer containerContext = ExoContainerContext.getCurrentContainer();
    TicketService ticketService = containerContext.getComponentInstanceOfType(TicketService.class);
    Calendar c = Calendar.getInstance();
    c.add(Calendar.DATE, -2);
    List<TopicEntity>  tickets = ticketService.findTicketsForReminder(c);
    for (TopicEntity ticket : tickets){
      String period= "";
      Calendar c1 = Calendar.getInstance();
      c1.add(Calendar.DATE, -7);
      Calendar c2 = Calendar.getInstance();
      c2.add(Calendar.DATE, -8);
      if(((Calendar)ticket.getUpdateDate()).before(c1)&& ((Calendar)ticket.getUpdateDate()).after(c2)){
        period = "7";
        NotificationContext ctx = NotificationContextImpl.cloneInstance().append(CSReminderPlugin.TICKET, ticket).append(CSReminderPlugin.PERIOD, period);
        ctx.getNotificationExecutor().with(ctx.makeCommand(PluginKey.key(CSReminderPlugin.ID))).execute(ctx);
      }

      c1 = Calendar.getInstance();
      c1.add(Calendar.DATE, -4);
      c2 = Calendar.getInstance();
      c2.add(Calendar.DATE, -5);
      if(((Calendar)ticket.getUpdateDate()).before(c1)&& ((Calendar)ticket.getUpdateDate()).after(c2)){
        period = "4";
        NotificationContext ctx = NotificationContextImpl.cloneInstance().append(CSReminderPlugin.TICKET, ticket).append(CSReminderPlugin.PERIOD, period);
        ctx.getNotificationExecutor().with(ctx.makeCommand(PluginKey.key(CSReminderPlugin.ID))).execute(ctx);
      }

      c1 = Calendar.getInstance();
      c1.add(Calendar.DATE, -2);
      c2 = Calendar.getInstance();
      c2.add(Calendar.DATE, -3);
      if(((Calendar)ticket.getUpdateDate()).before(c1)&& ((Calendar)ticket.getUpdateDate()).after(c2)){
        period = "2";
        NotificationContext ctx = NotificationContextImpl.cloneInstance().append(CSReminderPlugin.TICKET, ticket).append(CSReminderPlugin.PERIOD, period);
        ctx.getNotificationExecutor().with(ctx.makeCommand(PluginKey.key(CSReminderPlugin.ID))).execute(ctx);
      }

    }

    /*Automatically suspend spaces*/
    Calendar from = Calendar.getInstance();
    from.set(Calendar.YEAR, 2010);
    from.set(Calendar.MONTH, 0);
    from.set(Calendar.DAY_OF_MONTH, 0);
    Calendar to = Calendar.getInstance();
    to.add(Calendar.DATE, -1);
    CSSpaceService csSpaceService = containerContext.getComponentInstanceOfType(CSSpaceService.class);
    List<SpaceDTO>  spaces = csSpaceService.getActiveByEndSubscriptionDate(from, to);
    for(SpaceDTO space: spaces){

      try {
        space.setSubscriptionActive(false);
        csSpaceService.updateSpace(space);
        LOG.info("Space "+space.getGroupId()+" Suspended");
        NotificationContext ctx = NotificationContextImpl.cloneInstance().append(CSSubscriptionExpiredPlugin.SPACE, space);
        ctx.getNotificationExecutor().with(ctx.makeCommand(PluginKey.key(CSSubscriptionExpiredPlugin.ID))).execute(ctx);

      } catch (Exception e) {
        LOG.error("Can't suspend space "+space.getGroupId());
      }
    }

    /*Send Notifs for spaces to be suspended*/

    from = Calendar.getInstance();
    from.add(Calendar.DATE, 7);

    to = Calendar.getInstance();
    to.add(Calendar.DATE, 8);

    spaces = csSpaceService.getActiveByEndSubscriptionDate(from, to);
    for(SpaceDTO space: spaces){


        LOG.info("Space "+space.getGroupId()+" to be Suspended in 7 days");
      NotificationContext ctx = NotificationContextImpl.cloneInstance().append(CSSubscriptionReminderPlugin.SPACE, space);
      ctx.getNotificationExecutor().with(ctx.makeCommand(PluginKey.key(CSSubscriptionReminderPlugin.ID))).execute(ctx);

    }

    LOG.info("Reminder Notification Job finished");
  }

}
