package jetbrains.mps.samples.ChemMastery.editor;

/*Generated by MPS */

import javax.swing.JComponent;
import jetbrains.mps.openapi.editor.EditorContext;
import org.jetbrains.mps.openapi.model.SNode;
import javax.swing.JButton;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.smodel.action.SNodeFactoryOperations;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.editor.runtime.selection.SelectionUtil;
import jetbrains.mps.openapi.editor.selection.SelectionManager;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import java.awt.Font;
import jetbrains.mps.nodeEditor.EditorSettings;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import org.jetbrains.mps.openapi.language.SConcept;
import org.jetbrains.mps.openapi.language.SContainmentLink;

public class ButtonFactory {
  public static JComponent createRemoveButton(final EditorContext editorContext, final SNode node) {
    JButton button = ButtonFactory.createButton(node, editorContext, "Remove", new Runnable() {
      @Override
      public void run() {
        SNodeOperations.deleteNode(node);
      }
    });
    return button;
  }
  public static JComponent createNextDocumentButton(final EditorContext editorContext, final SNode node) {
    JButton button = ButtonFactory.createButton(node, editorContext, "Text", new Runnable() {
      @Override
      public void run() {
        SNode doc = SNodeFactoryOperations.createNewNode(CONCEPTS.DocumentationEntry$cW, null);
        SNode l = ListSequence.fromList(SLinkOperations.getChildren(SLinkOperations.getTarget(doc, LINKS.text$LyCV), LINKS.lines$U$m7)).first();
        ListSequence.fromList(SLinkOperations.getChildren(l, LINKS.elements$_j45)).clear();
        SLinkOperations.addNewChild(l, LINKS.elements$_j45, CONCEPTS.Word$Dn);
        SNodeOperations.insertNextSiblingChild(node, doc);
        SelectionUtil.selectCell(editorContext, doc, SelectionManager.FIRST_EDITABLE_CELL);
      }
    });
    return button;
  }
  public static JComponent createNextEquationButton(final EditorContext editorContext, final SNode node) {
    JButton button = ButtonFactory.createButton(node, editorContext, "Equation", new Runnable() {
      @Override
      public void run() {
        SNode entry = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xa9a262e8f8054598L, 0x88c614f38937d309L, 0x6ef7184fab9f24e0L, "jetbrains.mps.samples.ChemMastery.structure.EquationEntry"));
        SNode eq = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0xa9a262e8f8054598L, 0x88c614f38937d309L, 0x6ef7184fab9f2133L, "jetbrains.mps.samples.ChemMastery.structure.ChemEquation"));
        SLinkOperations.setTarget(entry, LINKS.equation$J35l, eq);
        SNodeOperations.insertNextSiblingChild(node, entry);
        SelectionUtil.selectLabelCellAnSetCaret(editorContext, SLinkOperations.getTarget(entry, LINKS.equation$J35l), "LEFT_EQ", 0);
      }
    });
    return button;
  }
  public static JComponent createDocumentButton(final EditorContext editorContext, final SNode node) {
    JButton button = ButtonFactory.createButton(node, editorContext, "Describe", new Runnable() {
      @Override
      public void run() {
        SNode doc = SNodeFactoryOperations.createNewNode(CONCEPTS.DocumentationEntry$cW, null);
        SNode l = ListSequence.fromList(SLinkOperations.getChildren(SLinkOperations.getTarget(doc, LINKS.text$LyCV), LINKS.lines$U$m7)).first();
        ListSequence.fromList(SLinkOperations.getChildren(l, LINKS.elements$_j45)).clear();
        SLinkOperations.addNewChild(l, LINKS.elements$_j45, CONCEPTS.Word$Dn);
        SNodeOperations.insertPrevSiblingChild(node, doc);
        SelectionUtil.selectCell(editorContext, doc, SelectionManager.FIRST_EDITABLE_CELL);
      }
    });
    return button;
  }
  private static JButton createButton(final SNode node, final EditorContext editorContext, String title, final Runnable action) {
    JButton button = new JButton(title);
    button.setFont(new Font(EditorSettings.getInstance().getFontFamily(), Font.PLAIN, EditorSettings.getInstance().getFontSize() / 6 * 5));
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent p0) {
        editorContext.getRepository().getModelAccess().executeCommandInEDT(action);
      }
    });
    return button;
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept DocumentationEntry$cW = MetaAdapterFactory.getConcept(0xa9a262e8f8054598L, 0x88c614f38937d309L, 0x6ef7184fab9f24e2L, "jetbrains.mps.samples.ChemMastery.structure.DocumentationEntry");
    /*package*/ static final SConcept Word$Dn = MetaAdapterFactory.getConcept(0xc7fb639fbe784307L, 0x89b0b5959c3fa8c8L, 0x229012ddae35f04L, "jetbrains.mps.lang.text.structure.Word");
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink text$LyCV = MetaAdapterFactory.getContainmentLink(0xa9a262e8f8054598L, 0x88c614f38937d309L, 0x6ef7184fab9f24e2L, 0x6ef7184fab9f24e3L, "text");
    /*package*/ static final SContainmentLink lines$U$m7 = MetaAdapterFactory.getContainmentLink(0xc7fb639fbe784307L, 0x89b0b5959c3fa8c8L, 0x2331694e5619f411L, 0x2331694e561a03b8L, "lines");
    /*package*/ static final SContainmentLink elements$_j45 = MetaAdapterFactory.getContainmentLink(0xc7fb639fbe784307L, 0x89b0b5959c3fa8c8L, 0x2331694e561af166L, 0x2331694e561af167L, "elements");
    /*package*/ static final SContainmentLink equation$J35l = MetaAdapterFactory.getContainmentLink(0xa9a262e8f8054598L, 0x88c614f38937d309L, 0x6ef7184fab9f24e0L, 0x6ef7184fab9f24dbL, "equation");
  }
}
