/*
 * Copyright (c) 2012-2014 Spotify AB
 *
 * 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 com.spotify.netty4.handler.codec.zmtp;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

public class ZMTPWriter {

  public interface Output {

    ByteBuf buffer(int size);
  }

  public static class ByteBufAllocatorOutput implements Output {

    private final ByteBufAllocator allocator;

    public ByteBufAllocatorOutput(final ByteBufAllocator allocator) {
      this.allocator = allocator;
    }

    @Override
    public ByteBuf buffer(final int size) {
      return allocator.buffer(size);
    }
  }

  private final int version;

  private ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;

  private int expectedFrames;
  private int expectedSize;
  private ByteBuf buf;

  private int frame;

  public ZMTPWriter(final int version) {
    this.version = version;
  }

  public void alloc(final ByteBufAllocator alloc) {
    this.alloc = alloc;
  }

  public void reset() {
    expectedFrames = 0;
    frame = 0;
    buf = null;
    expectedSize = 0;
  }

  public void expectFrame(final int size) {
    expectedFrames++;
    expectedSize += ZMTPUtils.frameSize(size, version);
  }

  public void expectFrames(final int frames) {
    this.expectedFrames = frames;
  }

  public void expectSize(final int size) {
    this.expectedSize = size;
  }

  public void begin() {
    buf = alloc.buffer(expectedSize);
  }

  public ByteBuf frame(final int size) {
    if (frame >= expectedFrames) {
      throw new IllegalStateException("content");
    }
    final boolean more = (frame + 1 < expectedFrames);
    return frame(size, more);
  }

  private ByteBuf frame(final int size, final boolean more) {
    ZMTPUtils.writeFrameHeader(buf, size, more, version);
    frame++;
    return buf;
  }

  public void end() {
  }

  public ByteBuf finish() {
    return buf;
  }
}

@@ -746,402 +746,8 @@
  %7B%0A%0A
-  public interface Output %7B%0A%0A    ByteBuf buffer(int size);%0A  %7D%0A%0A  public static class ByteBufAllocatorOutput implements Output %7B%0A%0A    private final ByteBufAllocator allocator;%0A%0A    public ByteBufAllocatorOutput(final ByteBufAllocator allocator) %7B%0A      this.allocator = allocator;%0A    %7D%0A%0A    @Override%0A    public ByteBuf buffer(final int size) %7B%0A      return allocator.buffer(size);%0A    %7D%0A  %7D%0A%0A
   pr
@@ -1145,20 +1145,27 @@
  0;%0A    
-fram
+expectedSiz
 e = 0;%0A 
@@ -1179,35 +1179,28 @@
 = null;%0A    
-expectedSiz
+fram
 e = 0;%0A  %7D%0A%0A
@@ -1984,24 +1984,74 @@
  finish() %7B%0A
+    final ByteBuf result = this.buf;%0A    reset();%0A
     return b
@@ -2045,23 +2045,26 @@
 %0A    return 
-buf
+result
 ;%0A  %7D%0A%7D%0A

clean up + discard state on finish()