package nl.utwente.group10.ui;

import java.io.IOException;

import javafx.scene.control.ContextMenu;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import nl.utwente.group10.haskell.catalog.Context;
import nl.utwente.group10.haskell.catalog.FunctionEntry;
import nl.utwente.group10.haskell.catalog.HaskellCatalog;
import nl.utwente.group10.ui.components.blocks.Block;
import nl.utwente.group10.ui.components.blocks.ValueBlock;
import nl.utwente.group10.ui.components.blocks.DisplayBlock;
import nl.utwente.group10.ui.components.blocks.FunctionBlock;

public class MainMenu extends ContextMenu {
    private CustomUIPane parent;

    public MainMenu(HaskellCatalog catalog, CustomUIPane tactilePane) {
        parent = tactilePane;

        for (String category : catalog.getCategories()) {
            Menu submenu = new Menu(category);

            for (FunctionEntry entry : catalog.getCategory(category)) {
                MenuItem item = new MenuItem(entry.getName());
                item.setOnAction(event -> addFunctionBlock(entry));
                submenu.getItems().add(item);
        }

            this.getItems().addAll(submenu);
        }

        MenuItem valueBlockItem = new MenuItem("Value Block");
        valueBlockItem.setOnAction(event -> addValueBlock());
        MenuItem displayBlockItem = new MenuItem("Display Block");
        displayBlockItem.setOnAction(event -> addDisplayBlock());
        
        //TODO remove this item when debugging of visualFeedback is done
        MenuItem errorItem = new MenuItem("Error all Blocks");
        errorItem.setOnAction(event -> parent.errorAll());

        MenuItem quitItem = new MenuItem("Quit");
        quitItem.setOnAction(event -> System.exit(0));

        SeparatorMenuItem sep = new SeparatorMenuItem();

        this.getItems().addAll(valueBlockItem, displayBlockItem, sep, errorItem, quitItem);
    }

    private void addFunctionBlock(FunctionEntry entry) {
        try {
            FunctionBlock fb = new FunctionBlock(entry.getName(), entry.asHaskellObject(new Context()), parent); // TODO: Once the Env is available, the type should be pulled from the Env here (don't just calculate it over and over). Or just pass the signature String.
            addBlock(fb);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addValueBlock() {
        try {
            addBlock(new ValueBlock(parent));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addDisplayBlock() {
        try {
            addBlock(new DisplayBlock(parent));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addBlock(Block block) {
        parent.getChildren().add(block);
    }

}

@@ -52,16 +52,103 @@
 ception;
+%0Aimport java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.Comparator;
 %0A%0Aimport
@@ -897,27 +897,33 @@
     
-for (
+ArrayList%3C
 String
+%3E
  categor
 y : 
@@ -918,20 +918,38 @@
  categor
-y : 
+ies = new ArrayList%3C%3E(
 catalog.
@@ -964,16 +964,99 @@
 ories())
+;%0A        Collections.sort(categories);%0A%0A        for (String category : categories)
  %7B%0A     
@@ -1110,21 +1110,26 @@
         
-for (
+ArrayList%3C
 Function
@@ -1137,17 +1137,36 @@
 ntry
+%3E
  entr
-y : 
+ies = new ArrayList%3C%3E(
 cata
@@ -1191,16 +1191,105 @@
 tegory))
+;%0A            Collections.sort(entries);%0A%0A            for (FunctionEntry entry : entries)
  %7B%0A     

Fix ordering of menu items