package jetbrains.mps.lang.plugin.behavior;

/*Generated by MPS */

import jetbrains.mps.core.aspects.behaviour.BaseBHDescriptor;
import org.jetbrains.mps.openapi.language.SAbstractConcept;
import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory;
import jetbrains.mps.core.aspects.behaviour.api.SMethod;
import jetbrains.mps.core.aspects.behaviour.SMethodBuilder;
import jetbrains.mps.core.aspects.behaviour.SJavaCompoundTypeImpl;
import jetbrains.mps.core.aspects.behaviour.AccessPrivileges;
import org.jetbrains.mps.openapi.model.SNode;
import org.jetbrains.mps.openapi.model.SModel;
import org.jetbrains.mps.openapi.language.SEnumerationLiteral;
import jetbrains.mps.openapi.editor.EditorContext;
import java.util.List;
import java.util.Arrays;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.baseLanguage.closures.runtime.Wrappers;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.lang.plugin.enumMigration.Keymap_MigrationUtils;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SModelOperations;
import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations;
import jetbrains.mps.util.NameUtil;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.openapi.editor.EditorPanelManager;
import jetbrains.mps.core.aspects.behaviour.api.SConstructor;
import org.jetbrains.annotations.Nullable;
import jetbrains.mps.core.aspects.behaviour.api.BHMethodNotFoundException;
import org.jetbrains.mps.openapi.language.SConcept;
import org.jetbrains.mps.openapi.language.SProperty;
import org.jetbrains.mps.openapi.language.SContainmentLink;
import org.jetbrains.mps.openapi.language.SReferenceLink;

public final class SimpleShortcutChange__BehaviorDescriptor extends BaseBHDescriptor {
  private static final SAbstractConcept CONCEPT = MetaAdapterFactory.getConcept(0x28f9e4973b424291L, 0xaeba0a1039153ab1L, 0x15afe07f2a9bb076L, "jetbrains.mps.lang.plugin.structure.SimpleShortcutChange");

  public static final SMethod<Void> addToKeymapChanges_id1adAGAvXyr3 = new SMethodBuilder<Void>(new SJavaCompoundTypeImpl(Void.class)).name("addToKeymapChanges").modifiers(1, AccessPrivileges.PUBLIC).concept(CONCEPT).baseMethodId(1336894855161718467L).languageId(0xaeba0a1039153ab1L, 0x28f9e4973b424291L).build2(SMethodBuilder.createJavaParameter((Class<SNode>) ((Class) Object.class), ""), SMethodBuilder.createJavaParameter((Class<SModel>) ((Class) Object.class), ""), SMethodBuilder.createJavaParameter((Class<Iterable<SEnumerationLiteral>>) ((Class) Object.class), ""), SMethodBuilder.createJavaParameter(EditorContext.class, ""));

  private static final List<SMethod<?>> BH_METHODS = Arrays.<SMethod<?>>asList(addToKeymapChanges_id1adAGAvXyr3);

  private static void ___init___(@NotNull SNode __thisNode__) {
  }

  /*package*/ static void addToKeymapChanges_id1adAGAvXyr3(@NotNull SAbstractConcept __thisConcept__, SNode shortcutChange, final SModel model, Iterable<SEnumerationLiteral> keymaps, final EditorContext editorContext) {
    final Wrappers._T<SNode> _shortcutChange = new Wrappers._T<SNode>(shortcutChange);
    Sequence.fromIterable(keymaps).visitAll((it) -> {
      {
        final String currentEnum = Keymap_MigrationUtils.value(it);
        final Wrappers._T<SNode> keyMap = new Wrappers._T<SNode>(ListSequence.fromList(SModelOperations.roots(model, CONCEPTS.KeymapChangesDeclaration$QD)).findFirst(new _FunctionTypes._return_P1_E0<Boolean, SNode>() {
          public Boolean invoke(SNode it) {
            return Keymap_MigrationUtils.value(SPropertyOperations.getEnum(it, PROPS.keymap$Oqju)).equals(currentEnum);
          }
        }));
        if ((keyMap.value == null)) {
          keyMap.value = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0x28f9e4973b424291L, 0xaeba0a1039153ab1L, 0x15afe07f2a9bb075L, "jetbrains.mps.lang.plugin.structure.KeymapChangesDeclaration"));
          // Enum value has not got any constraint, so it needs to be converted to valid identifier
          SPropertyOperations.set(keyMap.value, PROPS.name$MnvL, NameUtil.toValidCamelIdentifier(Keymap_MigrationUtils.value(it)));
          SPropertyOperations.setEnum(keyMap.value, PROPS.keymap$Oqju, it);
          // If IdeaConfigurationXml exists, take it in account
          SPropertyOperations.set(keyMap.value, PROPS.isPluginXmlKeymap$OQ3Q, ListSequence.fromList(SModelOperations.roots(model, CONCEPTS.IdeaConfigurationXml$o_)).isNotEmpty());
          if (ListSequence.fromList(SModelOperations.roots(model, CONCEPTS.IdeaConfigurationXml$o_)).isNotEmpty()) {
            ListSequence.fromList(SModelOperations.roots(model, CONCEPTS.IdeaConfigurationXml$o_)).where(new _FunctionTypes._return_P1_E0<Boolean, SNode>() {
              public Boolean invoke(SNode it) {
                return (SLinkOperations.getTarget(it, LINKS.actions$aiX4) != null);
              }
            }).visitAll(new _FunctionTypes._void_P1_E0<SNode>() {
              public void invoke(SNode it) {
                {
                  SNode keymapRef = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0x28f9e4973b424291L, 0xaeba0a1039153ab1L, 0x498be7ea73fa87eL, "jetbrains.mps.lang.plugin.structure.KeymapRef"));
                  SLinkOperations.setTarget(keymapRef, LINKS.keymap$F_03, keyMap.value);
                  ListSequence.fromList(SLinkOperations.getChildren(SLinkOperations.getTarget(it, LINKS.actions$aiX4), LINKS.keymapsRef$CPvp)).addElement(keymapRef);
                }
              }
            });

          }

          SModelOperations.addRootNode(model, keyMap.value);
        }

        // Have to copy node, because can't add same node to several concepts as child
        _shortcutChange.value = SNodeOperations.copyNode(_shortcutChange.value);
        ListSequence.fromList(SLinkOperations.getChildren(keyMap.value, LINKS.shortcutChange$L1Z4)).addElement(_shortcutChange.value);

        EditorPanelManager editorPanelManager = editorContext.getEditorPanelManager();
        if (editorPanelManager != null) {
          // Focus on last keystroke as newly added will be last
          editorPanelManager.openEditor(ListSequence.fromList(SLinkOperations.getChildren(_shortcutChange.value, LINKS.keystroke$Nxja)).last());
        }
      }
    });
  }

  /*package*/ SimpleShortcutChange__BehaviorDescriptor() {
  }

  @Override
  protected void initNode(@NotNull SNode node, @NotNull SConstructor constructor, @Nullable Object[] parameters) {
    ___init___(node);
  }

  @Override
  protected <T> T invokeSpecial0(@NotNull SNode node, @NotNull SMethod<T> method, @Nullable Object[] parameters) {
    int methodIndex = BH_METHODS.indexOf(method);
    if (methodIndex < 0) {
      throw new BHMethodNotFoundException(this, method);
    }
    switch (methodIndex) {
      default:
        throw new BHMethodNotFoundException(this, method);
    }
  }

  @Override
  protected <T> T invokeSpecial0(@NotNull SAbstractConcept concept, @NotNull SMethod<T> method, @Nullable Object[] parameters) {
    int methodIndex = BH_METHODS.indexOf(method);
    if (methodIndex < 0) {
      throw new BHMethodNotFoundException(this, method);
    }
    switch (methodIndex) {
      case 0:
        addToKeymapChanges_id1adAGAvXyr3(concept, (SNode) parameters[0], (SModel) parameters[1], (Iterable<SEnumerationLiteral>) parameters[2], (EditorContext) parameters[3]);
        return null;
      default:
        throw new BHMethodNotFoundException(this, method);
    }
  }

  @NotNull
  @Override
  public List<SMethod<?>> getDeclaredMethods() {
    return BH_METHODS;
  }

  @NotNull
  @Override
  public SAbstractConcept getConcept() {
    return CONCEPT;
  }

  private static final class CONCEPTS {
    /*package*/ static final SConcept KeymapChangesDeclaration$QD = MetaAdapterFactory.getConcept(0x28f9e4973b424291L, 0xaeba0a1039153ab1L, 0x15afe07f2a9bb075L, "jetbrains.mps.lang.plugin.structure.KeymapChangesDeclaration");
    /*package*/ static final SConcept IdeaConfigurationXml$o_ = MetaAdapterFactory.getConcept(0x28f9e4973b424291L, 0xaeba0a1039153ab1L, 0x2c7d36ab0e3b095aL, "jetbrains.mps.lang.plugin.structure.IdeaConfigurationXml");
  }

  private static final class PROPS {
    /*package*/ static final SProperty keymap$Oqju = MetaAdapterFactory.getProperty(0x28f9e4973b424291L, 0xaeba0a1039153ab1L, 0x15afe07f2a9bb075L, 0x15afe07f2a9bb0c9L, "keymap");
    /*package*/ static final SProperty name$MnvL = MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name");
    /*package*/ static final SProperty isPluginXmlKeymap$OQ3Q = MetaAdapterFactory.getProperty(0x28f9e4973b424291L, 0xaeba0a1039153ab1L, 0x15afe07f2a9bb075L, 0x77ff578948f6b772L, "isPluginXmlKeymap");
  }

  private static final class LINKS {
    /*package*/ static final SContainmentLink actions$aiX4 = MetaAdapterFactory.getContainmentLink(0x28f9e4973b424291L, 0xaeba0a1039153ab1L, 0x2c7d36ab0e3b095aL, 0x2c7d36ab0e3b095cL, "actions");
    /*package*/ static final SReferenceLink keymap$F_03 = MetaAdapterFactory.getReferenceLink(0x28f9e4973b424291L, 0xaeba0a1039153ab1L, 0x498be7ea73fa87eL, 0x498be7ea73fa87fL, "keymap");
    /*package*/ static final SContainmentLink keymapsRef$CPvp = MetaAdapterFactory.getContainmentLink(0x28f9e4973b424291L, 0xaeba0a1039153ab1L, 0x498be7ea73fa71eL, 0x498be7ea73fa880L, "keymapsRef");
    /*package*/ static final SContainmentLink shortcutChange$L1Z4 = MetaAdapterFactory.getContainmentLink(0x28f9e4973b424291L, 0xaeba0a1039153ab1L, 0x15afe07f2a9bb075L, 0x15afe07f2a9bb077L, "shortcutChange");
    /*package*/ static final SContainmentLink keystroke$Nxja = MetaAdapterFactory.getContainmentLink(0x28f9e4973b424291L, 0xaeba0a1039153ab1L, 0x15afe07f2a9bb076L, 0x15afe07f2a9bb07eL, "keystroke");
  }
}
