package jetbrains.mps.ide.actions;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.workbench.action.BaseAction;
import javax.swing.Icon;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.actionSystem.AnActionEvent;
import java.util.Map;
import java.util.List;
import org.jetbrains.mps.openapi.model.SModel;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import jetbrains.mps.ide.IdeBundle;
import jetbrains.mps.project.MPSProject;
import org.jetbrains.mps.openapi.module.SModule;
import jetbrains.mps.baseLanguage.closures.runtime.Wrappers;
import com.intellij.openapi.ui.Messages;
import com.intellij.util.ui.UIUtil;
import javax.swing.JCheckBox;
import org.jetbrains.mps.openapi.module.SRepository;
import jetbrains.mps.ide.save.SaveRepositoryCommand;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.smodel.SModelStereotype;
import jetbrains.mps.workbench.actions.model.DeleteModelHelper;

@GeneratedClass(node = "r:00000000-0000-4000-0000-011c895904a4(jetbrains.mps.ide.actions)/3575273646046443826", model = "r:00000000-0000-4000-0000-011c895904a4(jetbrains.mps.ide.actions)")
public class DeleteModels_Action extends BaseAction {
  private static final Icon ICON = null;

  private boolean forceSafe;
  public DeleteModels_Action(boolean forceSafe_par) {
    super("Delete Models", "", ICON);
    this.forceSafe = forceSafe_par;
    this.setIsAlwaysVisible(false);
    this.setExecuteOutsideCommand(true);
    updateInBackground(true);
  }
  @Override
  public boolean isDumbAware() {
    return true;
  }
  @Override
  public void doUpdate(@NotNull AnActionEvent event, final Map<String, Object> _params) {
    if (DeleteModels_Action.this.forceSafe) {
      event.getPresentation().setText((((List<SModel>) MapSequence.fromMap(_params).get("models")).size() == 1 ? IdeBundle.message("actions.model.delete.title.safe") : IdeBundle.message("actions.model.delete.title.safe.many")));
    } else {
      event.getPresentation().setText((((List<SModel>) MapSequence.fromMap(_params).get("models")).size() == 1 ? IdeBundle.message("actions.model.delete.title") : IdeBundle.message("actions.model.delete.title.many")));
    }
    setEnabledState(event.getPresentation(), DeleteModels_Action.this.isApplicable(_params));
  }
  @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);
      MapSequence.fromMap(_params).put("project", p);
      if (p == null) {
        return false;
      }
    }
    {
      SModule p = event.getData(MPSCommonDataKeys.CONTEXT_MODULE);
      MapSequence.fromMap(_params).put("contextModule", p);
      if (p == null) {
        return false;
      }
    }
    {
      List<SModel> p = event.getData(MPSCommonDataKeys.MODELS);
      MapSequence.fromMap(_params).put("models", p);
      if (p == null) {
        return false;
      }
      if (p.isEmpty()) {
        return false;
      }
    }
    return true;
  }
  @Override
  public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
    final Wrappers._boolean safeDelete = new Wrappers._boolean();
    if (DeleteModels_Action.this.forceSafe) {
      safeDelete.value = true;
    } else {
      int result = Messages.showCheckboxMessageDialog(IdeBundle.message("actions.model.delete.message"), IdeBundle.message("actions.model.delete.title.many"), new String[]{IdeBundle.message("actions.module.delete.ok.button.text"), Messages.CANCEL_BUTTON}, UIUtil.replaceMnemonicAmpersand(IdeBundle.message("actions.model.delete.option.safe")), true, 0, 0, Messages.getQuestionIcon(), (Integer exitCode, JCheckBox checkBox) -> (exitCode == -1 || exitCode == 1 ? Messages.CANCEL : Boolean.compare(true, checkBox.isSelected())));

      if (result == Messages.CANCEL) {
        return;
      }

      safeDelete.value = result == Messages.YES;
    }

    final SRepository repository = ((MPSProject) MapSequence.fromMap(_params).get("project")).getRepository();
    repository.getModelAccess().executeCommandInEDT(() -> {
      // see MPS-18743
      new SaveRepositoryCommand(repository).execute();
      for (SModel model : ListSequence.fromList(((List<SModel>) MapSequence.fromMap(_params).get("models")))) {
        if (SModelStereotype.isStubModel(model) || SModelStereotype.isDescriptorModel(model)) {
          continue;
        }
        DeleteModelHelper.deleteModel(((MPSProject) MapSequence.fromMap(_params).get("project")), ((SModule) MapSequence.fromMap(_params).get("contextModule")), model, safeDelete.value, true);
      }
    });
  }
  @NotNull
  public String getActionId() {
    StringBuilder res = new StringBuilder();
    res.append(super.getActionId());
    res.append("#");
    res.append(((Object) this.forceSafe).toString());
    res.append("!");
    return res.toString();
  }
  public boolean isApplicable(final Map<String, Object> _params) {
    for (SModel m : ListSequence.fromList(((List<SModel>) MapSequence.fromMap(_params).get("models")))) {
      if (!(SModelStereotype.isStubModel(m)) && !(SModelStereotype.isDescriptorModel(m))) {
        return true;
      }
    }
    return false;
  }
}
