/*
 * Autopsy Forensic Browser
 *
 * Copyright 2019 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sleuthkit.autopsy.report.infrastructure;

import org.openide.util.NbBundle;
import org.sleuthkit.autopsy.progress.ProgressIndicator;
import org.sleuthkit.autopsy.report.ReportProgressPanel;

/**
 * An adapter that adapts the ReportProgressPanel interface to the
 * ProgressIndicator interface so that a given progress indicator can be used
 * where a ReportProgressPanel is expected.
 */
public class ReportProgressIndicator extends ReportProgressPanel {

    private static final long serialVersionUID = 1L;
    private final ProgressIndicator progressIndicator;

    /**
     * Constructs an adapter that adapts the ReportProgressPanel interface to
     * the ProgressIndicator interface so that a given progress indicator can be
     * used where a ReportProgressPanel is expected.
     *
     * @param progressIndicator The progress indicator tobe adapted.
     */
    public ReportProgressIndicator(ProgressIndicator progressIndicator) {
        this.progressIndicator = progressIndicator;
    }

    @Override
    public void setLabels(String reportName, String reportPath) {
        /*
         * Not expected to be called, method exists for ReportGenerationPanel,
         * which is not expected to be instantiated when this class is used.
         */
    }

    @Override
    @NbBundle.Messages({
        "ReportProgressIndicator.startMessage=Report generation started"
    })
    public void start() {
        setStatus(ReportStatus.RUNNING);
        progressIndicator.start(Bundle.ReportProgressIndicator_startMessage());
    }

    @Override
    @NbBundle.Messages({
        "ReportProgressIndicator.switchToDeterminateMessage=Report generation progress switched to determinate"
    })
    public void setMaximumProgress(int max) {
        progressIndicator.switchToDeterminate(Bundle.ReportProgressIndicator_switchToDeterminateMessage(), 0, max);
    }

    @Override
    public void setProgress(int value) {
        progressIndicator.progress(value);
    }

    @Override
    @NbBundle.Messages({
        "ReportProgressIndicator.switchToIndeterminateMessage=Report generation progress switched to indeterminate"
    })
    public void setIndeterminate(boolean indeterminate) {
        progressIndicator.switchToIndeterminate(Bundle.ReportProgressIndicator_switchToIndeterminateMessage());
    }

    @Override
    public void updateStatusLabel(String statusMessage) {
        if (getStatus() != ReportStatus.CANCELED) {
            progressIndicator.progress(statusMessage);
        }
    }

    @Override
    @NbBundle.Messages({
        "ReportProgressIndicator.completedMessage=Report generation completed",
        "ReportProgressIndicator.completedWithErrorsMessage=Report generation completed with errors",})
    public void complete(ReportStatus reportStatus) {
        if (getStatus() != ReportStatus.CANCELED) {
            switch (reportStatus) {
                case COMPLETE: {
                    progressIndicator.progress(Bundle.ReportProgressIndicator_completedMessage());
                    break;
                }
                case ERROR: {
                    progressIndicator.progress(Bundle.ReportProgressIndicator_completedWithErrorsMessage());
                    break;
                }
                default: {
                    break;
                }
            }
        }
        progressIndicator.finish();
    }

    @Override
    public void complete(ReportStatus reportStatus, String statusMessage) {
        if (getStatus() != ReportStatus.CANCELED) {
            progressIndicator.progress(statusMessage);
        }
        complete(reportStatus);
    }

    @Override
    @NbBundle.Messages({
        "ReportProgressIndicator.cancelledMessage=Report generation cancelled",})
    public void cancel() {
        switch (getStatus()) {
            case COMPLETE:
                break;
            case CANCELED:
                break;
            case ERROR:
                break;
            default:
                setStatus(ReportStatus.CANCELED);
                progressIndicator.progress(Bundle.ReportProgressIndicator_cancelledMessage());
                break;
        }
    }

}

@@ -1250,16 +1250,52 @@
 dicator;
+%0A    private int workUnitsCompleted;
 %0A%0A    /*
@@ -1572,16 +1572,17 @@
 cator to
+ 
 be adapt
@@ -1828,17 +1828,32 @@
       * 
-N
+This method is n
 ot expec
@@ -1872,16 +1872,12 @@
 lled
-, method
+. It
  exi
@@ -1883,16 +1883,38 @@
 ists for
+ use by the%0A         *
  ReportG
@@ -1928,27 +1928,16 @@
 onPanel,
-%0A         *
  which i
@@ -1974,16 +1974,27 @@
 ted when
+%0A         *
  this cl
@@ -2014,24 +2014,73 @@
          */%0A
+        super.setLabels(reportName, reportPath);%0A
     %7D%0A%0A    @
@@ -2215,24 +2215,56 @@
 d start() %7B%0A
+        workUnitsCompleted = 0;%0A
         setS
@@ -2740,32 +2740,205 @@
 oid 
-setProgress(int value) %7B
+increment() %7B%0A        ++workUnitsCompleted;%0A        progressIndicator.progress(workUnitsCompleted);%0A    %7D%0A%0A    @Override%0A    public void setProgress(int value) %7B%0A        workUnitsCompleted = value;
 %0A   
@@ -2969,21 +2969,34 @@
 rogress(
-value
+workUnitsCompleted
 );%0A    %7D

Fix backwards compatibility issue for reports