package com.github.zhongl.stompize.demo;

import com.github.zhongl.stompize.Content;
import com.github.zhongl.stompize.Optional;
import com.github.zhongl.stompize.Required;
import com.github.zhongl.stompize.Stompize;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.DefaultCompositeByteBuf;
import io.netty.buffer.UnpooledByteBufAllocator;
import io.netty.channel.Channel;
import java.util.ArrayList;
import java.util.List;

import static com.github.zhongl.stompize.Bytes.buf;

/** @author <a href="mailto:zhong.lunfu@gmail.com">zhongl<a> */
public class DemoClientImpl extends DemoClient {

    private static final ByteBuf SEND        = buf("SEND");
    private static final ByteBuf DESTINATION = buf("\ndestination:");
    private static final ByteBuf TRANSACTION = buf("\ntransaction:");
    private static final ByteBuf RECEIPT     = buf("\nreceipt:");
    private static final ByteBuf ACK         = buf("ACK");
    private static final ByteBuf ID          = buf("\nid:");

    private final Channel channel;

    public DemoClientImpl(Channel channel, String s, boolean b, double d, float f, long l) {
        super(s, b, d, f, l);
        this.channel = channel;
    }

    @Override
    public void send(
            @Required("destination") String destination,
            @Optional("transaction") String transaction,
            @Optional("receipt") String receipt,
            Content content
    ) {
        List<ByteBuf> components = new ArrayList<ByteBuf>();
        components.add(SEND);

        Stompize.addRequiredHeaderTo(components, DESTINATION, destination);
        Stompize.addOptionalHeaderTo(components, TRANSACTION, transaction);
        Stompize.addOptionalHeaderTo(components, RECEIPT, receipt);
        Stompize.addContentTo(components, content);

        channel.write(new DefaultCompositeByteBuf(UnpooledByteBufAllocator.HEAP_BY_DEFAULT, false, 32, components));
        channel.flush();
    }

    @Override
    public void ack(@Required("id") String id, @Optional("receipt") String receipt, @Optional("transaction") String transaction) {
        List<ByteBuf> components = new ArrayList<ByteBuf>();
        components.add(ACK);

        Stompize.addRequiredHeaderTo(components, ID, id);
        Stompize.addOptionalHeaderTo(components, RECEIPT, receipt);
        Stompize.addOptionalHeaderTo(components, TRANSACTION, transaction);
        Stompize.addContentTo(components, null);

        channel.write(new DefaultCompositeByteBuf(UnpooledByteBufAllocator.HEAP_BY_DEFAULT, false, 32, components));
        channel.flush();
    }


}

@@ -688,41 +688,30 @@
 Buf 
-DESTINATION = buf(%22%5Cndestination:
+ACK         = buf(%22ACK
 %22);%0A
@@ -743,27 +743,27 @@
 ByteBuf 
-TRANSACTION
+destination
  = buf(%22
@@ -764,23 +764,23 @@
  buf(%22%5Cn
-transac
+destina
 tion:%22);
@@ -817,36 +817,40 @@
 Buf 
-RECEIPT     = buf(%22%5Cnreceipt
+transaction = buf(%22%5Cntransaction
 :%22);
@@ -875,39 +875,39 @@
 c final ByteBuf 
-ACK    
+receipt
      = buf(%22ACK%22
@@ -894,35 +894,42 @@
 eipt     = buf(%22
-ACK
+%5Cnreceipt:
 %22);%0A    private 
@@ -949,18 +949,18 @@
 ByteBuf 
-ID
+id
         
@@ -1557,18 +1557,33 @@
 s, D
-ESTINATION
+emoClientImpl.destination
 , de
@@ -1639,35 +1639,50 @@
 components, 
-TRANSACTION
+DemoClientImpl.transaction
 , transactio
@@ -1726,39 +1726,54 @@
 rTo(components, 
-RECEIPT
+DemoClientImpl.receipt
 , receipt);%0A    
@@ -2259,10 +2259,25 @@
 ts, 
-I
 D
+emoClientImpl.id
 , id
@@ -2332,15 +2332,30 @@
 ts, 
-RECEIPT
+DemoClientImpl.receipt
 , re
@@ -2415,19 +2415,34 @@
 ts, 
-TRANSACTION
+DemoClientImpl.transaction
 , tr

Clean code.