package nl.utwente.viskell.ui;

import java.util.ArrayList;

import javafx.scene.control.MenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.util.Duration;
import jfxtras.scene.layout.CircularPane;
import jfxtras.scene.menu.CirclePopupMenu;
import nl.utwente.viskell.ui.components.Block;

/**
 * Circle menu is a context based menu implementation for {@link Block} classes.
 * Preferably each block class has it's own instance of circle menu. When a
 * block based class has significant differences to other block classes that
 * result in different context based actions it should use a specialized
 * extension of circle menu instead of this one.
 * <p>
 * Current context based features include delete. Copy, Paste and Save
 * functionality is under development.
 * </p>
 */
public class CircleMenu extends CirclePopupMenu {

    /** List of all block on which a CircleMenu is active. */
    private static ArrayList<Block> activeBlocks = new ArrayList<>();
    
    /** The context of the menu. */
    private Block block;

    /** Show the Circle menu for a block if not already active. */
    public static void showFor(Block block, MouseEvent t) {
        if (! activeBlocks.contains(block)) {
            activeBlocks.add(block);
            CircleMenu menu = new CircleMenu(block);
            menu.show(t);
        }
    }
    
    public CircleMenu(Block block) {
        super(block, null);
        this.block = block;

        // Define menu items
        
        // Cut Option
        ImageView image = makeImageView("/ui/cut32.png");
        MenuItem delete = new MenuItem("cut", image);
        delete.setOnAction(t -> delete());

        // Copy Option
        image = makeImageView("/ui/copy32.png");
        MenuItem copy = new MenuItem("copy", image);
        copy.setOnAction(t -> copy());

        // Paste Option
        image = makeImageView("/ui/paste32.png");
        MenuItem paste = new MenuItem("paste", image);
        paste.setOnAction(t -> paste());

        // Save Option
        image = makeImageView("/ui/save32.png");
        MenuItem save = new MenuItem("save", image);
        save.setOnAction(t -> saveBlock());

        // Registration
        this.getItems().addAll(copy, paste, delete, save);

        // Animation
        this.setAnimationInterpolation(CircularPane::animateOverTheArcWithFade);
        this.setAnimationDuration(Duration.ONE);
    }
    
    private ImageView makeImageView(String path) {
        ImageView image = new ImageView(new Image(this.getClass().getResourceAsStream(path)));
        image.getStyleClass().add("hoverMenu");
        return image;
    }

    @Override
    public void hide() {
        CircleMenu.activeBlocks.remove(this.block);
        super.hide();
    }
    
    /** Copy the {@link Block} in this context. */
    public void copy() {
        // TODO implement clipBoard in main app.
    }

    /** Paste {@link Block} from memory. */
    public void paste() {
    }

    /** Delete the {@link Block} in this context. */
    public void delete() {
        block.getPane().removeBlock(block);
    }

    /** Saves the {@link Block} in this context. */
    public void saveBlock() {
        // TODO store block in custom catalog?
    }
}

@@ -29,37 +29,8 @@
 i;%0A%0A
-import java.util.ArrayList;%0A%0A
 impo
@@ -63,16 +63,16 @@
 nuItem;%0A
+
 import j
@@ -877,145 +877,8 @@
  %7B%0A%0A
-    /** List of all block on which a CircleMenu is active. */%0A    private static ArrayList%3CBlock%3E activeBlocks = new ArrayList%3C%3E();%0A    %0A
     
@@ -1066,95 +1066,8 @@
 ) %7B%0A
-        if (! activeBlocks.contains(block)) %7B%0A            activeBlocks.add(block);%0A    
     
@@ -1119,20 +1119,16 @@
         
-    
 menu.sho
@@ -1137,18 +1137,8 @@
 t);%0A
-        %7D%0A
     
@@ -2433,132 +2433,8 @@
  %7D%0A%0A
-    @Override%0A    public void hide() %7B%0A        CircleMenu.activeBlocks.remove(this.block);%0A        super.hide();%0A    %7D%0A    %0A
     

Drop the active block list