/*
 * Copyright 2016 The Chromium Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
package io.flutter.run.daemon;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

class ProgressHandler {
  final Project myProject;
  final List<String> myTasks = new ArrayList<>();

  Task.Backgroundable myTask;

  ProgressHandler(@NotNull Project project) {
    this.myProject = project;
  }

  /**
   * Start a progress task.
   *
   * @param log the title of the progress task
   */
  public void start(String log) {
    synchronized (myTasks) {
      myTasks.add(log);
      myTasks.notify();

      if (myTask == null) {
        myTask = new Task.Backgroundable(myProject, log, false) {
          @Override
          public void run(@NotNull ProgressIndicator indicator) {
            indicator.setText(log);

            synchronized (myTasks) {
              while (!myTasks.isEmpty()) {
                indicator.setText(myTasks.get(myTasks.size() - 1));

                try {
                  myTasks.wait();
                }
                catch (InterruptedException e) {
                  // ignore

                }
              }

              myTask = null;
            }
          }
        };

        // TODO(devoncarew): Debounce this.
        ApplicationManager.getApplication().invokeLater(() -> {
          ProgressManager.getInstance().run(myTask);
        });
      }
    }
  }

  /**
   * Notify that a progress task has finished.
   */
  public void done() {
    synchronized (myTasks) {
      myTasks.remove(myTasks.size() - 1);
      myTasks.notify();
    }
  }

  /**
   * Finish any outstanding progress tasks.
   */
  public void cancel() {
    synchronized (myTasks) {
      myTasks.clear();
      myTasks.notifyAll();
    }
  }
}

@@ -654,16 +654,24 @@
 %3E();%0A%0A  
+private 
 Task.Bac
@@ -967,24 +967,27 @@
 Tasks.notify
+All
 ();%0A%0A      i
@@ -1521,23 +1521,8 @@
 %7D%0A  
-            %7D%0A%0A
     
@@ -1546,16 +1546,33 @@
 = null;%0A
+              %7D%0A%0A
         
@@ -1709,16 +1709,52 @@
 () -%3E %7B%0A
+          synchronized(myTasks) %7B%0A  
         
@@ -1798,16 +1798,28 @@
 yTask);%0A
+          %7D%0A
         
@@ -2014,16 +2014,19 @@
 s.notify
+All
 ();%0A    

Fix inspections. First step toward #230 (#318)