package bndtools.central;

import java.util.concurrent.atomic.AtomicReference;

import org.bndtools.api.ILogger;
import org.bndtools.api.Logger;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import aQute.bnd.service.progress.ProgressPlugin;
import bndtools.Plugin;

public class JobProgress implements ProgressPlugin {
    static final ILogger logger = Logger.getLogger(JobProgress.class);

    @Override
    public Task startTask(String name, int size) {
        TaskJob taskjob = new TaskJob(name, size);
        taskjob.schedule();
        return taskjob;
    }

    private static class TaskJob extends Job implements Task {
        private final String name;
        private final int size;
        private final AtomicReference<IStatus> status = new AtomicReference<>();
        private volatile IProgressMonitor monitor;

        TaskJob(String name, int size) {
            super(name);
            this.name = name;
            this.size = size;
            logger.logInfo(name, null);
        }

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            this.monitor = monitor;

            monitor.beginTask(name, size);
            while (status.get() == null) {
                if (!isCanceled(monitor)) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {}
                }
            }
            monitor.done();

            return status.get();
        }

        /**
         * Must ensure task is done if true is returned.
         */
        private boolean isCanceled(IProgressMonitor m) {
            boolean canceled = m.isCanceled();
            if (canceled) {
                status.compareAndSet(null, new Status(Status.CANCEL, Plugin.PLUGIN_ID, "Canceled"));
            }
            return canceled;
        }

        @Override
        public void worked(int units) {
            IProgressMonitor m = monitor;
            if (m == null || (status.get() != null)) {
                return;
            }
            m.worked(units);
        }

        @Override
        public void done(String message, Throwable error) {
            status.compareAndSet(null, new Status(error == null ? Status.OK : Status.ERROR, Plugin.PLUGIN_ID, message, error));
        }

        @Override
        public boolean isCanceled() {
            IProgressMonitor m = monitor;
            if (m == null) {
                return false;
            }
            return isCanceled(m);
        }
    }
}

@@ -1492,16 +1492,272 @@
 ion e) %7B
+%0A                        monitor.setCanceled(true);%0A                        status.compareAndSet(null, new Status(Status.CANCEL, Plugin.PLUGIN_ID, %22InterruptedException%22, e));%0A                        Thread.currentThread().interrupt();%0A                    
 %7D%0A      

Handle InterruptedException as a cancel