package com.airlock.iam.servicecontainer.app.infrastructure.webapp.presenter;

import com.airlock.iam.elog.api.LogTypes;
import com.airlock.iam.log.audit.AuditLogger;
import com.airlock.iam.plugin.framework.misc.ioc.PluginLoader;
import com.airlock.iam.servicecontainer.api.domain.model.task.Task;
import com.airlock.iam.servicecontainer.app.application.configuration.TaskSchedule;
import com.airlock.iam.servicecontainer.app.application.configuration.TaskSchedulerService;
import com.airlock.iam.servicecontainer.app.application.configuration.TimeUnit;
import com.airlock.iam.servicecontainer.app.infrastructure.webapp.util.ServiceContainerVaadinTranslator;
import com.airlock.iam.servicecontainer.app.infrastructure.webapp.view.TaskSchedulerView;
import com.vaadin.server.VaadinSession;
import java.util.List;

public class TaskSchedulerServicePresenter extends ServicePresenter<TaskSchedulerView, TaskSchedulerService> {
   @Override
   public Class<TaskSchedulerView> getViewClass() {
      return TaskSchedulerView.class;
   }

   public Long getCheckIntervalMillis() {
      return this.getService().getCheckIntervalMillis();
   }

   public List<TaskSchedule> getTaskSchedules() {
      return this.getService().getTasks();
   }

   public void startStopTask(TaskSchedule taskSchedule) {
      String username = (String)VaadinSession.getCurrent().getAttribute("username");
      if (taskSchedule.isRunning()) {
         taskSchedule.getTask().pleaseStop();
         AuditLogger.log(TaskSchedulerServicePresenter.class, LogTypes.SERVER_INFO_SYS, "User " + username + " stopped service " + taskSchedule.getTaskName());
      } else {
         this.getService().executeTask(taskSchedule);
         AuditLogger.log(TaskSchedulerServicePresenter.class, LogTypes.SERVER_INFO_SYS, "User " + username + " started service " + taskSchedule.getTaskName());
      }
   }

   public String getPluginDescription(TaskSchedule taskSchedule) {
      return PluginLoader.getDisplayName(taskSchedule.getTaskConfig());
   }

   public String getExecutionInterval(TaskSchedule taskSchedule) {
      TimeUnit timeUnit = taskSchedule.getInterval();
      return timeUnit.getTime() == -1L
         ? ServiceContainerVaadinTranslator.translate("taskschedulerservice.never")
         : timeUnit.getTime() + " " + ServiceContainerVaadinTranslator.translate("taskschedulerservice." + timeUnit.getUnit());
   }

   public String getFirstExecutionTime(TaskSchedule taskSchedule) {
      return taskSchedule.getFirstTimeString();
   }

   public String getNextExecutionTime(TaskSchedule taskSchedule) {
      long nextExecutionDate = taskSchedule.computeNextExecutionDate(System.currentTimeMillis());
      return nextExecutionDate == -1L
         ? ServiceContainerVaadinTranslator.translate("taskschedulerservice.not-scheduled")
         : TaskSchedulerService.formatDate(nextExecutionDate);
   }

   public String getLatestExecutionTime(TaskSchedule taskSchedule) {
      long latestExecutionDate = taskSchedule.getLatestExecutionStartDate();
      return latestExecutionDate == -1L ? "-" : TaskSchedulerService.formatDate(latestExecutionDate);
   }

   public boolean isTaskRunning(TaskSchedule taskSchedule) {
      return taskSchedule.isRunning();
   }

   public int getProgressPercentage(TaskSchedule taskSchedule) {
      return taskSchedule.getTask().getProgressPercentage();
   }

   public List<Task.ExecutionResult> getLastExecutions(TaskSchedule taskSchedule) {
      return taskSchedule.getTask().getLastExecutions();
   }
}
