/*! ******************************************************************************
 *
 * Pentaho Data Integration
 *
 * Copyright (C) 2018 by Hitachi Vantara : http://www.pentaho.com
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.pentaho.hadoop.shim.common.format.avro;

import org.pentaho.di.core.row.ValueMeta;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.hadoop.shim.api.format.AvroSpec;
import org.pentaho.hadoop.shim.api.format.IAvroInputField;
import org.pentaho.hadoop.shim.common.format.BaseFormatInputField;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AvroInputField extends BaseFormatInputField implements IAvroInputField {

  ///////// Below added methods/variables to this object - Did this second /////////////
  private List<String> pathParts;
  private List<String> indexedVals = new ArrayList<>();

  private boolean m_isValid;
  protected String m_cleansedVariableName;
  protected String m_resolvedFieldName;

  /**
   * Index of this field in the incoming row stream
   */
  //private int m_inputIndex = -1;

  protected ValueMetaInterface m_fieldVM;
  /**
   * The name of the variable to hold this field's values
   */
  public String m_variableName = "";

  private ValueMeta tempValueMeta;
  private List<String> tempParts;

  //protected static Class<?> PKG = AvroInputMetaBase.class;
  protected static Class<?> PKG = AvroNestedReader.class;

  public int getOutputIndex() {
    return outputIndex;
  }

  public void setOutputIndex( int outputIndex ) {
    this.outputIndex = outputIndex;
  }

  private int outputIndex; // the index that this field is in the output
  // row structure

  public void setPathParts( List<String> pathParts ) {
    this.pathParts = pathParts;
  }

  public List<String> getPathParts() {

    return pathParts;
  }

  public ValueMeta getTempValueMeta() {
    return tempValueMeta;
  }

  public void setTempValueMeta( ValueMeta tempValueMeta ) {
    this.tempValueMeta = tempValueMeta;
  }

  public List<String> getTempParts() {
    return tempParts;
  }

  public void setTempParts( List<String> tempParts ) {
    this.tempParts = tempParts;
  }

  public void setIndexedVals( List<String> mindexedVals ) {
    this.indexedVals = mindexedVals;
  }

  public List<String> getIndexedVals() {
    int bracketPos = formatFieldName.indexOf( '[' );
    if ( indexedVals.isEmpty() && bracketPos > 0 ) {
      String values = formatFieldName.substring( bracketPos + 1, formatFieldName.length() - 1 );
      indexedVals = Arrays.asList( values.split( "\\s*,\\s*" ) );
    }
    return indexedVals;
  }

  public String getIndexedValues() {
    return String.join( " , ", getIndexedVals() );
  }

  public void setIndexedValues( String indexedValues ) {
    setIndexedVals( Arrays.asList( indexedValues.split( "\\s*,\\s*" ) ) );
  }

  @Override
  public String getAvroFieldName() {
    return formatFieldName;
  }

  @Override public void setAvroFieldName( String avroFieldName ) {
    this.formatFieldName = avroFieldName;
  }

  @Override
  public AvroSpec.DataType getAvroType() {
    return AvroSpec.DataType.getDataType( getFormatType() );
  }

  @Override
  public void setAvroType( AvroSpec.DataType avroType ) {
    setFormatType( avroType.getId() );
  }

  @Override
  public void setAvroType( String avroType ) {
    for ( AvroSpec.DataType tmpType : AvroSpec.DataType.values() ) {
      if ( tmpType.getName().equalsIgnoreCase( avroType ) ) {
        setFormatType( tmpType.getId() );
        break;
      }
    }
  }

  @Override
  public String getDisplayableAvroFieldName() {

    return formatFieldName;
  }

}

@@ -2967,37 +2967,31 @@
   %7D%0A%0A  p
-ublic List%3CString%3E ge
+rivate void ini
 tIndexed
@@ -3103,14 +3103,75 @@
 s %3E 
-0
+-1
  ) %7B%0A
+      int closeBracketPos = formatFieldName.indexOf( '%5D' );%0A
     
@@ -3235,36 +3235,23 @@
  1, 
-formatFieldName.length() - 1
+closeBracketPos
  );%0A
@@ -3318,24 +3318,93 @@
  ) );%0A    %7D%0A
+  %7D%0A%0A  public List%3CString%3E getIndexedVals() %7B%0A    initIndexedVals();%0A
     return i
@@ -3724,32 +3724,151 @@
 FieldName;%0A  %7D%0A%0A
+  @Override%0A  public void setFormatFieldName( String formatFieldName ) %7B%0A    setAvroFieldName( formatFieldName );%0A  %7D%0A%0A
   @Override publ
@@ -3914,24 +3914,24 @@
 eldName ) %7B%0A
-
     this.for
@@ -3952,32 +3952,55 @@
  avroFieldName;%0A
+    initIndexedVals();%0A
   %7D%0A%0A  @Override

Fix for top level collection index