package jetbrains.mps.ide.devkit.actions;

/*Generated by MPS */

import jetbrains.mps.workbench.action.BaseAction;
import javax.swing.Icon;
import jetbrains.mps.icons.MPSIcons;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.actionSystem.AnActionEvent;
import java.util.Map;
import com.intellij.openapi.actionSystem.Presentation;
import jetbrains.mps.ide.actions.MPSCommonDataKeys;
import jetbrains.mps.ide.ui.tree.module.ProjectModuleTreeNode;
import jetbrains.mps.project.MPSProject;
import org.jetbrains.mps.openapi.module.SModule;
import jetbrains.mps.smodel.Language;
import javax.swing.tree.TreeNode;
import jetbrains.mps.ide.actions.NewModelActionExecutor;
import org.jetbrains.mps.openapi.model.SModel;
import jetbrains.mps.project.structure.modules.LanguageDescriptor;

public class NewAccessoryModel_Action extends BaseAction {
  private static final Icon ICON = MPSIcons.Nodes.Models.AccessoryModel;

  public NewAccessoryModel_Action() {
    super("New Accessory Model", "", ICON);
    this.setIsAlwaysVisible(false);
    this.setExecuteOutsideCommand(true);
  }
  @Override
  public boolean isDumbAware() {
    return true;
  }
  @Override
  public void doUpdate(@NotNull AnActionEvent event, final Map<String, Object> _params) {
    Presentation presentation = event.getPresentation();
    presentation.setEnabledAndVisible(true);
    presentation.setText(((event.getData(MPSCommonDataKeys.TREE_NODE) instanceof ProjectModuleTreeNode ? "" : "New ")) + "Accessory Model");
  }
  @Override
  protected boolean collectActionData(AnActionEvent event, final Map<String, Object> _params) {
    if (!(super.collectActionData(event, _params))) {
      return false;
    }
    {
      MPSProject p = event.getData(MPSCommonDataKeys.MPS_PROJECT);
      if (p == null) {
        return false;
      }
    }
    {
      SModule p = event.getData(MPSCommonDataKeys.CONTEXT_MODULE);
      if (p == null) {
        return false;
      }
      if (p != null && !(p instanceof Language)) {
        return false;
      }
    }
    {
      TreeNode p = event.getData(MPSCommonDataKeys.TREE_NODE);
      if (p == null) {
        return false;
      }
    }
    return true;
  }
  @Override
  public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
    NewAccessoryModel_Action.this.getExecutor(event).execute();
  }
  protected NewModelActionExecutor getExecutor(final AnActionEvent event) {
    return new NewModelActionExecutor(event.getData(MPSCommonDataKeys.MPS_PROJECT), ((Language) event.getData(MPSCommonDataKeys.CONTEXT_MODULE)), event.getData(MPSCommonDataKeys.TREE_NODE)) {
      @Override
      protected void onModelCreated(final SModel model) {
        // FIXME bad design. onModelCreated runs command to create a model and attach it to a module,
        //  here separate command registers it with a language. What's the point to have these executors, what does
        //  we gain by re-using actions?
        myProject.getModelAccess().executeCommand(() -> {
          Language language = (Language) model.getModule();
          LanguageDescriptor descriptor = language.getModuleDescriptor();
          descriptor.getAccessoryModels().add(model.getReference());
          language.save();
        });
        super.onModelCreated(model);
      }
    };
  }
}
