package org.openlca.proto.output;

import java.time.Instant;
import java.util.Arrays;

import org.openlca.core.model.ProductSystem;
import org.openlca.core.model.Version;
import org.openlca.proto.generated.Proto;
import org.openlca.util.Strings;

public class ProductSystemWriter {

  private final WriterConfig config;

  public ProductSystemWriter(WriterConfig config) {
    this.config = config;
  }

  public Proto.ProductSystem write(ProductSystem system) {
    var proto = Proto.ProductSystem.newBuilder();
    if (system == null)
      return proto.build();

    // root entity fields
    proto.setType("ProductSystem");
    proto.setId(Strings.orEmpty(system.refId));
    proto.setName(Strings.orEmpty(system.name));
    proto.setDescription(Strings.orEmpty(system.description));
    proto.setVersion(Version.asString(system.version));
    if (system.lastChange != 0L) {
      var instant = Instant.ofEpochMilli(system.lastChange);
      proto.setLastChange(instant.toString());
    }

    // categorized entity fields
    if (Strings.notEmpty(system.tags)) {
      Arrays.stream(system.tags.split(","))
        .filter(Strings::notEmpty)
        .forEach(proto::addTags);
    }
    if (system.category != null) {
      proto.setCategory(Out.refOf(system.category, config));
    }

    // model specific fields
    // TODO

    return proto.build();
  }
}

@@ -80,16 +80,142 @@
 rrays;%0A%0A
+import org.openlca.core.model.ImpactCategory;%0Aimport org.openlca.core.model.ModelType;%0Aimport org.openlca.core.model.Process;%0A
 import o
@@ -1446,42 +1446,1270 @@
     
-// TODO%0A%0A    return proto.build();
+mapParameterSets(system, proto);%0A%0A    // TODO%0A%0A    return proto.build();%0A  %7D%0A%0A  private void mapParameterSets(ProductSystem system, Proto.ProductSystem.Builder proto) %7B%0A    for (var paramSet : system.parameterSets) %7B%0A      var protoSet = Proto.ParameterRedefSet.newBuilder();%0A      protoSet.setName(Strings.orEmpty(paramSet.name));%0A      protoSet.setDescription(Strings.orEmpty(paramSet.description));%0A      protoSet.setIsBaseline(paramSet.isBaseline);%0A      for (var redef : paramSet.parameters) %7B%0A        var protoRedef = Proto.ParameterRedef.newBuilder();%0A        protoRedef.setName(Strings.orEmpty(redef.name));%0A        protoRedef.setValue(redef.value);%0A        if (redef.uncertainty != null) %7B%0A          var u = Out.uncertaintyOf(redef.uncertainty);%0A          protoRedef.setUncertainty(u);%0A        %7D%0A        if (redef.contextId != null) %7B%0A          var context = redef.contextType == ModelType.PROCESS%0A            ? config.db.getDescriptor(Process.class, redef.contextId)%0A            : config.db.getDescriptor(ImpactCategory.class, redef.contextId);%0A          if (context != null) %7B%0A            protoRedef.setContext(Out.refOf(context));%0A          %7D%0A        %7D%0A        protoSet.addParameters(protoRedef);%0A      %7D%0A      proto.addParameterSets(protoSet);%0A    %7D
 %0A  %7D

write product system parameter sets