package com.fwdekker.randomness;

import com.intellij.openapi.actionSystem.ActionGroup;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.event.InputEvent;
import java.util.ArrayList;
import java.util.List;


/**
 * A group of actions for a particular type of random data that can be generated.
 */
public abstract class DataGroupAction extends ActionGroup {
    private final DataInsertAction insertAction;
    private final DataArrayInsertAction insertArrayAction;
    private final SettingsAction settingsAction;


    /**
     * Constructs a new {@code DataGroupAction}.
     */
    public DataGroupAction() {
        insertAction = getInsertAction();
        insertArrayAction = getInsertArrayAction();
        settingsAction = getSettingsAction();
    }


    @NotNull
    @Override
    public final AnAction[] getChildren(final @Nullable AnActionEvent event) {
        final List<AnAction> children = new ArrayList<>();
        children.add(insertAction);
        children.add(insertArrayAction);
        if (settingsAction != null) {
            children.add(settingsAction);
        }

        return children.toArray(new AnAction[0]);
    }

    @Override
    public final boolean canBePerformed(final DataContext context) {
        return true;
    }

    @Override
    public final void actionPerformed(final AnActionEvent event) {
        super.actionPerformed(event);

        final boolean shiftPressed = (event.getModifiers() & (InputEvent.SHIFT_MASK | InputEvent.SHIFT_DOWN_MASK)) != 0;
        final boolean ctrlPressed = (event.getModifiers() & (InputEvent.CTRL_MASK | InputEvent.CTRL_DOWN_MASK)) != 0;

        if (shiftPressed) {
            insertArrayAction.actionPerformed(event);
        } else if (ctrlPressed && settingsAction != null) {
            settingsAction.actionPerformed(event);
        } else {
            insertAction.actionPerformed(event);
        }
    }

    @Override
    public final void update(final AnActionEvent event) {
        super.update(event);

        event.getPresentation().setText(insertAction.getName());
    }

    @Override
    public final boolean isPopup() {
        return true;
    }


    /**
     * Returns a new {@link DataInsertAction}.
     *
     * @return a new {@link DataInsertAction}
     */
    protected abstract DataInsertAction getInsertAction();

    /**
     * Returns a new {@link DataArrayInsertAction}.
     *
     * @return a new {@link DataArrayInsertAction}
     */
    protected abstract DataArrayInsertAction getInsertArrayAction();

    /**
     * Returns a new {@link SettingsAction}.
     *
     * @return a new {@link SettingsAction}
     */
    protected abstract SettingsAction getSettingsAction();
}

@@ -366,59 +366,8 @@
 nt;%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A
 %0A%0A/*
@@ -1035,80 +1035,47 @@
     
-final List%3CAnAction%3E children = new ArrayList%3C%3E();%0A        children.add(
+return new AnAction%5B%5D%7B%0A                
 inse
@@ -1078,26 +1078,25 @@
 insertAction
-);
+,
 %0A        chi
@@ -1092,29 +1092,24 @@
         
-children.add(
+        
 insertAr
@@ -1113,26 +1113,25 @@
 tArrayAction
-);
+,
 %0A        if 
@@ -1131,63 +1131,16 @@
     
-if (settingsAction != null) %7B%0A            children.add(
+        
 sett
@@ -1149,18 +1149,16 @@
 gsAction
-);
 %0A       
@@ -1163,58 +1163,8 @@
    %7D
-%0A%0A        return children.toArray(new AnAction%5B0%5D)
 ;%0A  
@@ -1755,34 +1755,8 @@
 ssed
- && settingsAction != null
 ) %7B%0A
@@ -2139,24 +2139,77 @@
 ue;%0A    %7D%0A%0A%0A
+    // TODO mark these methods as returning @NotNull%0A
     /**%0A    

Remove support for null settings actions