package jetbrains.mps.ide.devkit.actions;

/*Generated by MPS */

import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.project.Project;
import org.jetbrains.mps.openapi.model.SModel;
import jetbrains.mps.smodel.ModelAccessHelper;
import jetbrains.mps.util.Computable;
import javax.swing.JOptionPane;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SModelOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.smodel.action.SNodeFactoryOperations;
import org.jetbrains.mps.openapi.language.SConcept;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import org.jetbrains.mps.openapi.language.SReferenceLink;

public class GoToEditorDeclarationHelper {
  public GoToEditorDeclarationHelper() {
  }
  public static SNode getOrCreateEditorForConcept(Project project, final SModel languageEditor, final SNode concept, final SNode node) {
    ModelAccessHelper mah = new ModelAccessHelper(project.getModelAccess());
    SNode editorDeclaration = mah.runReadAction(new Computable<SNode>() {
      @Override
      public SNode compute() {
        return GoToEditorDeclarationHelper.findEditorDeclaration(languageEditor, concept);
      }
    });
    if (editorDeclaration != null) {
      return editorDeclaration;
    }
    String message = mah.runReadAction(new Computable<String>() {
      @Override
      public String compute() {
        return "Concept \"" + node.getConcept().getQualifiedName() + "\" has no editor.\n" + "Create new editor?";
      }
    });
    int option = JOptionPane.showConfirmDialog(null, message, "Editor not found", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
    if (option != JOptionPane.YES_OPTION) {
      return null;
    }
    return mah.executeCommand(new Computable<SNode>() {
      @Override
      public SNode compute() {
        return GoToEditorDeclarationHelper.createEditorDeclaration(concept, languageEditor);
      }
    });
  }
  public static SNode findEditorDeclaration(SModel editorModel, final SNode conceptDeclaration) {
    return ListSequence.fromList(SModelOperations.roots(editorModel, CONCEPTS.ConceptEditorDeclaration$BH)).findFirst((it) -> SLinkOperations.getTarget(it, LINKS.conceptDeclaration$HJmJ) == conceptDeclaration);
  }
  public static SNode createEditorDeclaration(SNode conceptDeclaration, SModel editorModelDescriptor) {
    SModel editorModel = editorModelDescriptor;
    SNode result = SNodeFactoryOperations.createNewRootNode(editorModel, CONCEPTS.ConceptEditorDeclaration$BH, null);
    SLinkOperations.setTarget(result, LINKS.conceptDeclaration$HJmJ, conceptDeclaration);
    return result;
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept ConceptEditorDeclaration$BH = MetaAdapterFactory.getConcept(0x18bc659203a64e29L, 0xa83a7ff23bde13baL, 0xf9845363abL, "jetbrains.mps.lang.editor.structure.ConceptEditorDeclaration");
  }

  private static final class LINKS {
    /*package*/ static final SReferenceLink conceptDeclaration$HJmJ = MetaAdapterFactory.getReferenceLink(0x18bc659203a64e29L, 0xa83a7ff23bde13baL, 0x10f7df344a9L, 0x10f7df451aeL, "conceptDeclaration");
  }
}
