/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.apache.qpid.proton4j.codec.util;

import org.apache.qpid.proton4j.amqp.Symbol;
import org.apache.qpid.proton4j.amqp.UnsignedLong;
import org.apache.qpid.proton4j.buffer.ProtonBuffer;
import org.apache.qpid.proton4j.codec.EncoderState;
import org.apache.qpid.proton4j.codec.EncodingCodes;
import org.apache.qpid.proton4j.codec.encoders.AbstractDescribedTypeEncoder;

public class NoLocalTypeEncoder extends AbstractDescribedTypeEncoder<NoLocalType> {

    @Override
    public Class<NoLocalType> getTypeClass() {
        return NoLocalType.class;
    }

    @Override
    public UnsignedLong getDescriptorCode() {
        return NoLocalType.DESCRIPTOR_CODE;
    }

    @Override
    public Symbol getDescriptorSymbol() {
        return NoLocalType.DESCRIPTOR_SYMBOL;
    }

    @Override
    public void writeType(ProtonBuffer buffer, EncoderState state, NoLocalType value) {
        buffer.writeByte(EncodingCodes.DESCRIBED_TYPE_INDICATOR);
        state.getEncoder().writeUnsignedLong(buffer, state, getDescriptorCode());
        state.getEncoder().writeString(buffer, state, value.getDescribed());
    }

    @Override
    public void writeArray(ProtonBuffer buffer, EncoderState state, Object[] values) {
        // Write the Array Type encoding code, we don't optimize here.
        buffer.writeByte(EncodingCodes.ARRAY32);

        int startIndex = buffer.getWriteIndex();

        // Reserve space for the size and write the count of list elements.
        buffer.writeInt(0);
        buffer.writeInt(values.length);

        writeRawArray(buffer, state, values);

        // Move back and write the size
        int endIndex = buffer.getWriteIndex();
        long writeSize = endIndex - startIndex - Integer.BYTES;

        if (writeSize > Integer.MAX_VALUE) {
            throw new IllegalArgumentException("Cannot encode given array, encoded size to large: " + writeSize);
        }

        buffer.setInt(startIndex, (int) writeSize);
    }

    @Override
    public void writeRawArray(ProtonBuffer buffer, EncoderState state, Object[] values) {
        buffer.writeByte(EncodingCodes.DESCRIBED_TYPE_INDICATOR);
        state.getEncoder().writeUnsignedLong(buffer, state, getDescriptorCode());

        // TODO - This encodes incorrectly, should only be String type followed by the
        //        encoded values

        for (Object value : values) {
            NoLocalType noLocal = ((NoLocalType) value);
            state.getEncoder().writeString(buffer, state, noLocal.getDescribed());
        }
    }
}

@@ -1096,16 +1096,67 @@
 gCodes;%0A
+import org.apache.qpid.proton4j.codec.TypeEncoder;%0A
 import o
@@ -3076,155 +3076,102 @@
     
-// TODO - This encodes incorrectly, should only be String type followed by the%0A        //        encoded values%0A%0A        for (Object value : values
+Object%5B%5D elements = new Object%5Bvalues.length%5D;%0A%0A        for (int i = 0; i %3C values.length; ++i
 ) %7B%0A
@@ -3198,19 +3198,16 @@
 ype 
-noLocal
+value
  = 
-(
 (NoL
@@ -3221,16 +3221,67 @@
 e) value
+s%5Bi%5D;%0A            elements%5Bi%5D = value.getDescribed(
 );%0A     
@@ -3287,92 +3287,155 @@
     
+%7D%0A%0A
     
-state.getEncoder().writeString(buffer, state, noLocal.getDescribed());%0A        %7D
+    TypeEncoder%3C?%3E entryEncoder = state.getEncoder().getTypeEncoder(String.class);%0A        entryEncoder.writeArray(buffer, state, elements);
 %0A   

Fix test class to do proper array encoding.