package org.grails.ignite;

import it.sauronsoftware.cron4j.Predictor;
import it.sauronsoftware.cron4j.Scheduler;
import it.sauronsoftware.cron4j.SchedulingPattern;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;

/**
 * Created by dstieglitz on 3/30/16.
 */
public class IgniteCronDistributedRunnable<V> extends IgniteDistributedRunnable implements RunnableScheduledFuture<V> {

    private String cronTaskId;
    private boolean cancelled;

    public IgniteCronDistributedRunnable(DistributedScheduledThreadPoolExecutor executor, Runnable runnable) {
        super(executor, runnable);
    }

    @Override
    public boolean isPeriodic() {
        throw new UnsupportedOperationException("Operation not supported (yet)");
    }

    @Override
    public long getDelay(TimeUnit unit) {
        throw new UnsupportedOperationException("Operation not supported (yet)");
    }

    @Override
    public int compareTo(Delayed o) {
        return 0;
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        if (cronTaskId == null) {
            throw new IllegalArgumentException("Can't cancel a task without a cron task ID");
        }
        executor.deschedule(cronTaskId);
        cancelled = true;
        return isCancelled();
    }

    @Override
    public boolean isCancelled() {
        return this.cancelled;
    }

    @Override
    public boolean isDone() {
        throw new UnsupportedOperationException("Operation not supported (yet)");
    }

    @Override
    public V get() throws InterruptedException, ExecutionException {
        throw new UnsupportedOperationException("Operation not supported (yet)");
    }

    @Override
    public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
        throw new UnsupportedOperationException("Operation not supported (yet)");
    }

    public String getCronTaskId() {
        return this.cronTaskId;
    }

    public void setCronTaskId(String cronTaskId) {
        this.cronTaskId = cronTaskId;
    }

    public Map toDataMap() {
        Map result = new HashMap();
        Scheduler cronScheduler = executor.getCronScheduler();
        if (cronScheduler == null) {
            result.put("cronExpression", "NULL SCHEDULER");
        } else {
            SchedulingPattern schedulingPattern = cronScheduler.getSchedulingPattern(cronTaskId);
            String cronExpression;
            Predictor p = null;

            if (schedulingPattern == null) {
                cronExpression = "NO PATTERN";
            } else {
                cronExpression = schedulingPattern.toString();
                p = new Predictor(cronExpression);
            }

            result.put("cronTaskId", cronTaskId);
            result.put("cancelled", cancelled);
            result.put("cronExpression", cronExpression);

            if (p != null) {
                result.put("nextRun", p.nextMatchingDate());
            }
        }

        return result;
    }
}


@@ -2136,24 +2136,160 @@
  HashMap();%0A
+%0A        if (executor == null) %7B // FIXME this occurs, but why?!%0A            result.put(%22error%22, %22NULL EXECUTOR%22);%0A        %7D else %7B%0A    
         Sche
@@ -2339,32 +2339,36 @@
 uler();%0A        
+   
+ 
 if (cronSchedule
@@ -2372,32 +2372,36 @@
 uler == null) %7B%0A
+    
             resu
@@ -2444,32 +2444,36 @@
 ULER%22);%0A        
+  
+  
 %7D else %7B%0A       
@@ -2457,32 +2457,36 @@
        %7D else %7B%0A
+    
             Sche
@@ -2579,16 +2579,20 @@
         
+ 
+   
 String c
@@ -2606,16 +2606,20 @@
 ession;%0A
+    
         
@@ -2651,24 +2651,28 @@
             
+    
 if (scheduli
@@ -2700,32 +2700,36 @@
                 
+   
+ 
 cronExpression =
@@ -2747,32 +2747,36 @@
 N%22;%0A            
+    
 %7D else %7B%0A       
@@ -2776,32 +2776,36 @@
                 
+  
+  
 cronExpression =
@@ -2851,16 +2851,20 @@
         
+    
 p = new 
@@ -2894,35 +2894,43 @@
 n);%0A            
+ 
-%7D%0A%0A
+   %7D%0A%0A    
             resu
@@ -2967,32 +2967,36 @@
 d);%0A            
+    
 result.put(%22canc
@@ -3019,32 +3019,36 @@
 d);%0A            
+   
+ 
 result.put(%22cron
@@ -3086,24 +3086,28 @@
             
+    
 if (p != nul
@@ -3119,32 +3119,36 @@
                 
+  
+  
 result.put(%22next
@@ -3176,16 +3176,34 @@
 ate());%0A
+                %7D%0A
         

check for null executor when providing status