/*
 * Copyright 2013-2018 Real Logic Ltd.
 *
 * 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 uk.co.real_logic.sbe.json;

import org.agrona.concurrent.UnsafeBuffer;
import uk.co.real_logic.sbe.ir.Ir;
import uk.co.real_logic.sbe.ir.Token;
import uk.co.real_logic.sbe.otf.OtfHeaderDecoder;
import uk.co.real_logic.sbe.otf.OtfMessageDecoder;

import java.nio.ByteBuffer;
import java.util.List;

/**
 * Pretty Print Json based upon the given Ir.
 */
public class JsonPrinter
{
    private final OtfHeaderDecoder headerDecoder;
    private final Ir ir;

    public JsonPrinter(final Ir ir)
    {
        this.ir = ir;
        headerDecoder = new OtfHeaderDecoder(ir.headerStructure());
    }

    public void print(final ByteBuffer encodedMessage, final StringBuilder output)
    {
        final UnsafeBuffer buffer = new UnsafeBuffer(encodedMessage);
        print(output, buffer, 0);
    }

    public void print(final StringBuilder output, final UnsafeBuffer buffer, final int bufferOffset)
    {
        final int templateId = headerDecoder.getTemplateId(buffer, bufferOffset);
        final int schemaId = headerDecoder.getSchemaId(buffer, bufferOffset);
        final int actingVersion = headerDecoder.getSchemaVersion(buffer, bufferOffset);
        final int blockLength = headerDecoder.getBlockLength(buffer, bufferOffset);

        validateId(schemaId);
        validateVersion(schemaId, actingVersion);

        final int messageOffset = bufferOffset + headerDecoder.encodedLength();

        final List<Token> msgTokens = ir.getMessage(templateId);

        OtfMessageDecoder.decode(
            buffer,
            messageOffset,
            actingVersion,
            blockLength,
            msgTokens,
            new JsonTokenListener(output));
    }

    private void validateId(final int schemaId)
    {
        if (schemaId != ir.id())
        {
            throw new IllegalArgumentException(
                String.format("Required schema id %d but was actually %d", ir.id(), schemaId));
        }
    }

    private void validateVersion(final int schemaId, final int actingVersion)
    {
        if (actingVersion > ir.version())
        {
            throw new IllegalArgumentException(
                String.format("Required schema id %d but was actually %d", ir.id(), schemaId));
        }
    }

    public String print(final ByteBuffer encodedMessage)
    {
        final StringBuilder sb = new StringBuilder();
        print(encodedMessage, sb);

        return sb.toString();
    }
}

@@ -1996,17 +1996,16 @@
 ngth();%0A
-%0A
         
@@ -2417,92 +2417,55 @@
 ion(
-%0A                String.format(%22Required schema id %25d but was actually %25d%22, ir.id(),
+%22Required schema id %22 + ir.id() + %22 but was %22 +
  sch
@@ -2462,33 +2462,32 @@
 as %22 + schemaId)
-)
 ;%0A        %7D%0A    
@@ -2693,22 +2693,8 @@
     
-String.format(
 %22Req
@@ -2710,44 +2710,84 @@
 ema 
-id %25d but was actually %25d%22, ir.id(),
+version %22 + actingVersion + %22 but was %22 + ir.version() + %22 for schema id %22 +
  sch
@@ -2792,17 +2792,16 @@
 chemaId)
-)
 ;%0A      

Fix JsonPrinting version validation.