package jetbrains.mps.project.persistence;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.util.MacroHelper;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.project.structure.modules.SolutionDescriptor;
import org.jdom.Element;
import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes;
import jetbrains.mps.project.ModuleId;
import jetbrains.mps.project.structure.modules.SolutionKind;
import jetbrains.mps.util.xml.XmlUtil;
import java.util.List;
import jetbrains.mps.internal.collections.runtime.Sequence;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.Collection;

/**
 * XML/DOM persistence for a descriptor of Solution module
 */
@GeneratedClass(node = "r:a42e26eb-bbea-4e8d-a549-0d224ab71e57(jetbrains.mps.project.persistence)/842994667883032842", model = "r:a42e26eb-bbea-4e8d-a549-0d224ab71e57(jetbrains.mps.project.persistence)")
public class SolutionDescriptorPersistence {
  private final String SOURCE_GEN_DEFAULT = "${module}/source_gen";

  public static final String SOURCE_PATH = "sourcePath";
  public static final String SOURCE_PATH_SOURCE = "source";
  public static final String COMPILE_IN_MPS = "compileInMPS";
  /*package*/ static final String PLUGIN_KIND = "pluginKind";
  private final MacroHelper myMacroHelper;

  public SolutionDescriptorPersistence(@NotNull MacroHelper macroHelper) {
    myMacroHelper = macroHelper;
  }

  @NotNull
  public SolutionDescriptor load(@NotNull final Element rootElement) throws ModuleReadException {
    SolutionDescriptor descriptor;
    try {
      descriptor = ((_FunctionTypes._return_P0_E0<SolutionDescriptor>) () -> {
        final SolutionDescriptor result_8ckma3_a0a0a0b0k = new SolutionDescriptor();
        final String result_8ckma3_a0a0a0a0b0k = rootElement.getAttributeValue("name");
        result_8ckma3_a0a0a0b0k.setNamespace(result_8ckma3_a0a0a0a0b0k);

        if (rootElement.getAttributeValue("uuid") != null) {
          final ModuleId result_8ckma3_a0a2a0a0a0b0k = ModuleId.fromString(rootElement.getAttributeValue("uuid"));
          result_8ckma3_a0a0a0b0k.setId(result_8ckma3_a0a2a0a0a0b0k);
        }

        String pluginKind = rootElement.getAttributeValue(PLUGIN_KIND);
        if (pluginKind != null && pluginKind.length() > 0) {
          final SolutionKind result_8ckma3_a0a5a0a0a0b0k = SolutionKind.valueOf(pluginKind);
          result_8ckma3_a0a0a0b0k.setKind(result_8ckma3_a0a5a0a0a0b0k);
        }

        final boolean result_8ckma3_a7a0a0a0b0k = XmlUtil.booleanWithDefault(rootElement, COMPILE_IN_MPS, false);
        result_8ckma3_a0a0a0b0k.setCompileInMPS(result_8ckma3_a7a0a0a0b0k);

        String moduleVersion = rootElement.getAttributeValue("moduleVersion");
        if (moduleVersion != null) {
          try {
            result_8ckma3_a0a0a0b0k.setModuleVersion(Integer.parseInt(moduleVersion));
          } catch (NumberFormatException ignored) {
          }
        }

        final String result_8ckma3_a21a0a0a0b0k = myMacroHelper.expandPath(XmlUtil.stringWithDefault(rootElement, "generatorOutputPath", SOURCE_GEN_DEFAULT));
        result_8ckma3_a0a0a0b0k.setOutputPath(result_8ckma3_a21a0a0a0b0k);

        result_8ckma3_a0a0a0b0k.getModelRootDescriptors().addAll(ModuleDescriptorPersistence.loadModelRoots(XmlUtil.children(XmlUtil.first(rootElement, "models"), "modelRoot"), myMacroHelper));

        result_8ckma3_a0a0a0b0k.setNeedsExternalIdeaCompile(XmlUtil.first(rootElement, "compileInIDEA") != null);

        result_8ckma3_a0a0a0b0k.readOnlyStubModule(XmlUtil.first(rootElement, "readOnlyStubs") != null);

        Element facets = XmlUtil.first(rootElement, "facets");
        if (facets != null) {
          result_8ckma3_a0a0a0b0k.getModuleFacetDescriptors().addAll(ModuleDescriptorPersistence.loadFacets(XmlUtil.children(facets, "facet"), myMacroHelper));
        }


        List<String> javaLibs = Sequence.fromIterable(XmlUtil.children(XmlUtil.first(rootElement, "stubModelEntries"), "stubModelEntry")).select((it) -> it.getAttributeValue("path")).toList();
        result_8ckma3_a0a0a0b0k.getJavaLibOriginalValues().addAll(javaLibs);
        result_8ckma3_a0a0a0b0k.getJavaLibPersistedValues().addAll(ListSequence.fromList(javaLibs).select((it) -> myMacroHelper.expandPath(it)).toList());

        ModuleDescriptorPersistence.loadDependencies(result_8ckma3_a0a0a0b0k, rootElement);

        List<String> sources = Sequence.fromIterable(XmlUtil.children(XmlUtil.first(rootElement, SOURCE_PATH), SOURCE_PATH_SOURCE)).select((it) -> it.getAttributeValue("path")).toList();
        result_8ckma3_a0a0a0b0k.getSourcePathOriginalValue().addAll(sources);
        result_8ckma3_a0a0a0b0k.getSourcePathPersistedValue().addAll(ListSequence.fromList(sources).select((it) -> myMacroHelper.expandPath(it)).toList());
        return result_8ckma3_a0a0a0b0k;
      }).invoke();
    } catch (Exception ex) {
      throw new ModuleReadException(ex);
    }
    return descriptor;
  }

  @NotNull
  public Element save(SolutionDescriptor descriptor) {
    Element result = new Element("solution");
    if (descriptor.getNamespace() != null) {
      result.setAttribute("name", descriptor.getNamespace());
    }
    if (descriptor.getId() != null) {
      result.setAttribute("uuid", descriptor.getId().toString());
    }
    result.setAttribute("moduleVersion", Integer.toString(descriptor.getModuleVersion()));
    if (descriptor.getOutputPath() != null) {
      String p = myMacroHelper.shrinkPath(descriptor.getOutputPath());
      if (!(SOURCE_GEN_DEFAULT.equals(p))) {
        result.setAttribute("generatorOutputPath", p);
      }
    }
    if (descriptor.needsExternalIdeaCompile()) {
      result.addContent(new Element("compileInIDEA"));
    }
    if (descriptor.isReadOnlyStubModule()) {
      result.addContent(new Element("readOnlyStubs"));
    }

    Element models = new Element("models");
    ModuleDescriptorPersistence.saveModelRoots(models, descriptor.getModelRootDescriptors(), myMacroHelper);
    result.addContent(models);

    if (!(descriptor.getModuleFacetDescriptors().isEmpty())) {
      Element facets = new Element("facets");
      ModuleDescriptorPersistence.saveFacets(facets, descriptor.getModuleFacetDescriptors(), myMacroHelper);
      result.addContent(facets);
    }


    Collection<String> additionalJavaStubPaths = descriptor.getJavaLibPersistedValues();
    if (!(additionalJavaStubPaths.isEmpty())) {
      Element stubModelEntries = new Element("stubModelEntries");
      ModuleDescriptorPersistence.saveStubModelEntries(stubModelEntries, additionalJavaStubPaths, myMacroHelper);
      result.addContent(stubModelEntries);
    }

    Collection<String> sources = descriptor.getSourcePathPersistedValue();
    if (!(sources.isEmpty())) {
      Element sourcePath = new Element(SOURCE_PATH);
      for (String p : sources) {
        XmlUtil.tagWithAttribute(sourcePath, SOURCE_PATH_SOURCE, "path", myMacroHelper.shrinkPath(p));
      }
      result.addContent(sourcePath);
    }

    ModuleDescriptorPersistence.saveDependencies(result, descriptor);
    return result;
  }
}
