/*
 * Copyright (c) 2000-2004 by JetBrains s.r.o. All Rights Reserved.
 * Use is subject to license terms.
 */
package com.intellij.util.ui.update;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

public class MergingUpdateQueue implements ActionListener, Disposable {

  private boolean myActive;

  private final Set<Update> mySheduledUpdates = new TreeSet<Update>();

  private Timer myWaiterForMerge;

  private boolean myFlushing;

  private String myName;
  private final JComponent myComponent;

  public MergingUpdateQueue(String name, int mergingTimeSpan, boolean isActive, JComponent component) {
    myComponent = component;
    myWaiterForMerge = new Timer(mergingTimeSpan, this);
    myName = name;


    if (isActive) {
      showNotify();
    }
  }

  public void setMergingTimeSpan(int timeSpan) {
    myWaiterForMerge.setDelay(timeSpan);
  }
  
  public void cancelAllUpdates() {
    synchronized(mySheduledUpdates) {
      mySheduledUpdates.clear();
    }
  }
  
  
  public void hideNotify() {
    if (!myActive) {
      return;
    }

    myActive = false;
    myWaiterForMerge.stop();
  }

  public void showNotify() {
    if (myActive) {
      return;
    }

    myWaiterForMerge.start();
    myActive = true;
    flush();
  }


  public void actionPerformed(ActionEvent e) {
    flush();
  }

  public void flush() {
    if (mySheduledUpdates.size() > 0) {
      flush(true);
    }
  }

  public boolean isFlushing() {
    return myFlushing;
  }

  public void flush(boolean invokeLaterIfNotDispatch) {
    if (myFlushing) return;
    if (!isModalityStateCorrect()) return;

    myFlushing = true;
    final Runnable toRun = new Runnable() {
      public void run() {
        try {
          List<Update> toUpdate;
          final Update[] all;
          
          synchronized(mySheduledUpdates) {
            toUpdate = new ArrayList<Update>(mySheduledUpdates.size());
            all = mySheduledUpdates.toArray(new Update[mySheduledUpdates.size()]);
            mySheduledUpdates.clear();
          }

          for (int i = 0; i < all.length; i++) {
            Update each = all[i];
            if (!isExpired(each)) {
              toUpdate.add(each);
            }
            each.setProcessed();
          }
          
          execute(toUpdate.toArray(new Update[toUpdate.size()]));
        } 
        finally {
          myFlushing = false;
        }
      }
    };

    if (invokeLaterIfNotDispatch && !ApplicationManager.getApplication().isDispatchThread()) {
      ApplicationManager.getApplication().invokeLater(toRun, ModalityState.NON_MMODAL);
    }
    else {
      toRun.run();
    }
  }

  private boolean isModalityStateCorrect() {
    ModalityState current = ApplicationManager.getApplication().getCurrentModalityState();
    final ModalityState modalityState = getModalityState();
    return !current.dominates(modalityState);
  }

  private boolean isExpired(Update each) {
    return each.isDisposed() || each.isExpired();
  }

  protected void execute(final Update[] update) {
    for (final Update each : update) {
      if (each.executeInWriteAction()) {
        ApplicationManager.getApplication().runWriteAction(new Runnable() {
          public void run() {
            each.run();
          }
        });
      }
      else {
        each.run();
      }
    }
  }

  public final void queue(final Update update) {
    final Application app = ApplicationManager.getApplication();
    if (app.isUnitTestMode()) {
      app.invokeLater(update);
      return;
    }
    
    synchronized(mySheduledUpdates) {
      boolean updateWasEatenByQueue = eatThisOrOthers(update);
      if (updateWasEatenByQueue) return;

      if (myActive) {
        if (mySheduledUpdates.isEmpty()) {
          myWaiterForMerge.restart();
        }
        put(update);
      } 
      else {
        put(update);
      }
    }
  }

  private boolean eatThisOrOthers(Update update) {
    if (mySheduledUpdates.contains(update)) {
      return false;
    }

    final Update[] updates = mySheduledUpdates.toArray(new Update[mySheduledUpdates.size()]);
    for (Update eachInQueue : updates) {
      if (eachInQueue.canEat(update)) {
        return true;
      }
      if (update.canEat(eachInQueue)) {
        mySheduledUpdates.remove(eachInQueue);
      }
    }
    return false;
  }

  public final void run(Update update) {
    execute(new Update[] {update});
  }

  private void put(Update update) {
    mySheduledUpdates.remove(update);
    mySheduledUpdates.add(update);
  }

  protected boolean passThroughForUnitTesting() {
    return true;
  }

  public boolean isActive() {
    return myActive;
  }

  public void dispose() {
    myWaiterForMerge.stop();
  }

  public String toString() {
    return "Merger: " + myName + " active=" + myActive + " sheduled=" + mySheduledUpdates;
  }

  public boolean containsUpdateOf(int priority) {
    Update[] update = mySheduledUpdates.toArray(new Update[mySheduledUpdates.size()]);
    for (Update each : update) {
      if (each.getPriority() == priority) {
        return true;
      }
    }
    return false;
  }

  public ModalityState getModalityState() {
    if (myComponent == null) return ModalityState.NON_MMODAL;
    return ModalityState.stateForComponent(myComponent);
  }
}

@@ -3677,30 +3677,24 @@
 %7D%0A%0A  public 
-final 
 void queue(f

Fabrique request