/*
 * The MIT License
 *
 * Copyright 2019 Intuit Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.intuit.karate.debug;

import com.intuit.karate.FileUtils;
import com.intuit.karate.JsonUtils;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.util.ByteProcessor;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author pthomas3
 */
public class DapDecoder extends ByteToMessageDecoder {

    private static final Logger logger = LoggerFactory.getLogger(DapDecoder.class);
    
    private int remaining;

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
        if (remaining > 0 && buffer.readableBytes() >= remaining) {
            CharSequence msg = buffer.readCharSequence(remaining, FileUtils.UTF8);
            out.add(encode(msg));
            remaining = 0;
        }
        int pos;
        while ((pos = findCrLfCrLf(buffer)) != -1) {
            int delimiterPos = pos;
            while (buffer.getByte(--pos) != ':') {
                // skip backwards
            }
            buffer.readerIndex(++pos);
            CharSequence lengthString = buffer.readCharSequence(delimiterPos - pos, FileUtils.UTF8);
            int length = Integer.valueOf(lengthString.toString().trim());
            buffer.readerIndex(delimiterPos + 4);
            if (buffer.readableBytes() >= length) {
                CharSequence msg = buffer.readCharSequence(length, FileUtils.UTF8);
                out.add(encode(msg));
                remaining = 0;
            } else {
                remaining = length;
            }
        }
    }
    
    private static int findCrLfCrLf(ByteBuf buffer) {
        int totalLength = buffer.readableBytes();
        int readerIndex = buffer.readerIndex();
        int i = buffer.forEachByte(readerIndex, totalLength, ByteProcessor.FIND_LF);
        if (i > 0 && buffer.getByte(i - 1) == '\r') {
            int more = readerIndex + totalLength - i;
            if (more > 1 && buffer.getByte(i + 1) == '\r' && buffer.getByte(i + 2) == '\n') {
                return i - 1;
            }
        }
        return -1;
    }    

    private static DapMessage encode(CharSequence raw) {
        if (logger.isTraceEnabled()) {
            logger.trace(">> {}", raw);
        }
        Map<String, Object> map = JsonUtils.toJsonDoc(raw.toString()).read("$");
        return new DapMessage(map);
    }

}

@@ -1782,22 +1782,18 @@
 ByteBuf 
-buffer
+in
 , List%3CO
@@ -1852,22 +1852,18 @@
  %3E 0 && 
-buffer
+in
 .readabl
@@ -1903,108 +1903,35 @@
     
-CharSequence msg = buffer.readCharSequence(remaining, FileUtils.UTF8);%0A            out.add(encode(ms
+out.add(encode(in, remainin
 g));
@@ -2020,22 +2020,18 @@
 rLfCrLf(
-buffer
+in
 )) != -1
@@ -2089,22 +2089,18 @@
  while (
-buffer
+in
 .getByte
@@ -2169,38 +2169,34 @@
   %7D%0A            
-buffer
+in
 .readerIndex(++p
@@ -2236,30 +2236,26 @@
 gthString = 
-buffer
+in
 .readCharSeq
@@ -2383,22 +2383,18 @@
         
-buffer
+in
 .readerI
@@ -2433,22 +2433,18 @@
     if (
-buffer
+in
 .readabl
@@ -2469,92 +2469,8 @@
 ) %7B%0A
-                CharSequence msg = buffer.readCharSequence(length, FileUtils.UTF8);%0A
     
@@ -2500,11 +2500,18 @@
 ode(
-msg
+in, length
 ));%0A
@@ -3197,27 +3197,110 @@
 ode(
-CharSequence raw) %7B
+ByteBuf in, int length) %7B%0A        String msg = in.readCharSequence(length, FileUtils.UTF8).toString();
 %0A   
@@ -3369,19 +3369,19 @@
 %3E%3E %7B%7D%22, 
-raw
+msg
 );%0A     
@@ -3443,22 +3443,11 @@
 Doc(
-raw.toString()
+msg
 ).re

improve prev commit even more