package org.apache.pinot.plugin.inputformat.protobuf.decoder;
import org.apache.pinot.spi.data.readers.GenericRow;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProtobufRecorderMessageExtractor {
  public static GenericRow execute(byte[] from, GenericRow to) throws Exception {
    Map<String, Object> msgMap = decodeorg_apache_pinot_plugin_inputformat_protobuf_ComplexTypes_TestMessageMessage(org.apache.pinot.plugin.inputformat.protobuf.ComplexTypes.TestMessage.parseFrom(from));
    to.putValue("string_field", msgMap.getOrDefault("string_field", null));
    to.putValue("int_field", msgMap.getOrDefault("int_field", null));
    to.putValue("long_field", msgMap.getOrDefault("long_field", null));
    to.putValue("double_field", msgMap.getOrDefault("double_field", null));
    to.putValue("float_field", msgMap.getOrDefault("float_field", null));
    to.putValue("bool_field", msgMap.getOrDefault("bool_field", null));
    to.putValue("bytes_field", msgMap.getOrDefault("bytes_field", null));
    to.putValue("nested_message", msgMap.getOrDefault("nested_message", null));
    to.putValue("repeated_nested_messages", msgMap.getOrDefault("repeated_nested_messages", null));
    to.putValue("complex_map", msgMap.getOrDefault("complex_map", null));
    to.putValue("simple_map", msgMap.getOrDefault("simple_map", null));
    to.putValue("enum_field", msgMap.getOrDefault("enum_field", null));
    to.putValue("nullable_string_field", msgMap.getOrDefault("nullable_string_field", null));
    to.putValue("nullable_int_field", msgMap.getOrDefault("nullable_int_field", null));
    to.putValue("nullable_long_field", msgMap.getOrDefault("nullable_long_field", null));
    to.putValue("nullable_double_field", msgMap.getOrDefault("nullable_double_field", null));
    to.putValue("nullable_float_field", msgMap.getOrDefault("nullable_float_field", null));
    to.putValue("nullable_bool_field", msgMap.getOrDefault("nullable_bool_field", null));
    to.putValue("nullable_bytes_field", msgMap.getOrDefault("nullable_bytes_field", null));
    to.putValue("repeated_strings", msgMap.getOrDefault("repeated_strings", null));
    to.putValue("repeated_ints", msgMap.getOrDefault("repeated_ints", null));
    to.putValue("repeated_longs", msgMap.getOrDefault("repeated_longs", null));
    to.putValue("repeated_doubles", msgMap.getOrDefault("repeated_doubles", null));
    to.putValue("repeated_floats", msgMap.getOrDefault("repeated_floats", null));
    to.putValue("repeated_bools", msgMap.getOrDefault("repeated_bools", null));
    to.putValue("repeated_bytes", msgMap.getOrDefault("repeated_bytes", null));
    to.putValue("repeated_enums", msgMap.getOrDefault("repeated_enums", null));
    return to;
  }

  public static Map<String, Object> decodeorg_apache_pinot_plugin_inputformat_protobuf_ComplexTypes_TestMessageMessage(org.apache.pinot.plugin.inputformat.protobuf.ComplexTypes.TestMessage msg) {
    Map<String, Object> msgMap = new HashMap<>();
    msgMap.put("string_field", msg.getStringField());
    msgMap.put("int_field", msg.getIntField());
    msgMap.put("long_field", msg.getLongField());
    msgMap.put("double_field", msg.getDoubleField());
    msgMap.put("float_field", msg.getFloatField());
    msgMap.put("bool_field", String.valueOf(msg.getBoolField()));
    msgMap.put("bytes_field", msg.getBytesField().toByteArray());
    if (msg.hasNestedMessage()) {
      msgMap.put("nested_message", decodeorg_apache_pinot_plugin_inputformat_protobuf_ComplexTypes_TestMessage_NestedMessageMessage(msg.getNestedMessage()));
    }
    List<Object> list6 = new ArrayList<>();
    for (org.apache.pinot.plugin.inputformat.protobuf.ComplexTypes.TestMessage.NestedMessage row: msg.getRepeatedNestedMessagesList()) {
      list6.add(decodeorg_apache_pinot_plugin_inputformat_protobuf_ComplexTypes_TestMessage_NestedMessageMessage(row));
    }
    if (!list6.isEmpty()) {
      msgMap.put("repeated_nested_messages", list6.toArray());
    }
    Map<Object, Map<String, Object>> map6 = new HashMap<>();
    for (Map.Entry<String, Map<String,org.apache.pinot.plugin.inputformat.protobuf.ComplexTypes.TestMessage.NestedMessage>> entry: msg.getComplexMapMap().entrySet()) {
      map6.put(entry.getKey(), decodeorg_apache_pinot_plugin_inputformat_protobuf_ComplexTypes_TestMessage_NestedMessageMessage( (org.apache.pinot.plugin.inputformat.protobuf.ComplexTypes.TestMessage.NestedMessage) entry.getValue()));
    }
    msgMap.put("complex_map", map6);
    msgMap.put("simple_map", msg.getSimpleMapMap());
    msgMap.put("enum_field", msg.getEnumField().name());
    if (msg.hasNullableStringField()) {
      msgMap.put("nullable_string_field", msg.getNullableStringField());
    }
    if (msg.hasNullableIntField()) {
      msgMap.put("nullable_int_field", msg.getNullableIntField());
    }
    if (msg.hasNullableLongField()) {
      msgMap.put("nullable_long_field", msg.getNullableLongField());
    }
    if (msg.hasNullableDoubleField()) {
      msgMap.put("nullable_double_field", msg.getNullableDoubleField());
    }
    if (msg.hasNullableFloatField()) {
      msgMap.put("nullable_float_field", msg.getNullableFloatField());
    }
    if (msg.hasNullableBoolField()) {
      msgMap.put("nullable_bool_field", String.valueOf(msg.getNullableBoolField()));
    }
    if (msg.hasNullableBytesField()) {
      msgMap.put("nullable_bytes_field", msg.getNullableBytesField().toByteArray());
    }
    if (msg.getRepeatedStringsCount() > 0) {
      msgMap.put("repeated_strings", msg.getRepeatedStringsList().toArray());
    }
    if (msg.getRepeatedIntsCount() > 0) {
      msgMap.put("repeated_ints", msg.getRepeatedIntsList().toArray());
    }
    if (msg.getRepeatedLongsCount() > 0) {
      msgMap.put("repeated_longs", msg.getRepeatedLongsList().toArray());
    }
    if (msg.getRepeatedDoublesCount() > 0) {
      msgMap.put("repeated_doubles", msg.getRepeatedDoublesList().toArray());
    }
    if (msg.getRepeatedFloatsCount() > 0) {
      msgMap.put("repeated_floats", msg.getRepeatedFloatsList().toArray());
    }
    List<Object> list10 = new ArrayList<>();
    for (String row: msg.getRepeatedBoolsList()) {
      list10.add(String.valueOf(row));
    }
    if (!list10.isEmpty()) {
      msgMap.put("repeated_bools", list10.toArray());
    }
    List<Object> list11 = new ArrayList<>();
    for (com.google.protobuf.ByteString row: msg.getRepeatedBytesList()) {
      list11.add(row.toByteArray());
    }
    if (!list11.isEmpty()) {
      msgMap.put("repeated_bytes", list11.toArray());
    }
    List<Object> list12 = new ArrayList<>();
    for (org.apache.pinot.plugin.inputformat.protobuf.ComplexTypes.TestMessage.TestEnum row: msg.getRepeatedEnumsList()) {
      list12.add(row.name());
    }
    if (!list12.isEmpty()) {
      msgMap.put("repeated_enums", list12.toArray());
    }
    return msgMap;
  }

  public static Map<String, Object> decodeorg_apache_pinot_plugin_inputformat_protobuf_ComplexTypes_TestMessage_NestedMessageMessage(org.apache.pinot.plugin.inputformat.protobuf.ComplexTypes.TestMessage.NestedMessage msg) {
    Map<String, Object> msgMap = new HashMap<>();
    msgMap.put("nested_string_field", msg.getNestedStringField());
    msgMap.put("nested_int_field", msg.getNestedIntField());
    return msgMap;
  }
}
