package org.easyarch.netpet.asynclient.http.response.impl;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import org.easyarch.netpet.asynclient.http.response.AsyncHttpResponse;
import org.easyarch.netpet.kits.ByteKits;
import org.easyarch.netpet.web.mvc.entity.Json;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xingtianyu on 17-4-4
 * 下午9:51
 * description:
 */

public class AsyncHttpResponseImpl implements AsyncHttpResponse {

    private FullHttpRequest request;
    private HttpHeaders headers;

    private ByteBuf buf;

    public AsyncHttpResponseImpl(FullHttpRequest request){
        this.request = request;
        this.headers = request.headers();
        this.buf = request.content().copy();
    }

    @Override
    public String getHeader(String name) {
        return this.headers.get(name);
    }

    @Override
    public Map<String, String> getAllHeaders() {
        Map<String,String> headers = new HashMap<>();
        for (String name:this.headers.names()){
            String value = this.headers.get(name);
            headers.put(name,value);
        }
        return headers;
    }

    @Override
    public HttpHeaders getHeaders() {
        return this.headers;
    }

    @Override
    public byte[] getBytes() {
        return ByteKits.toByteArray(this.buf);
    }

    @Override
    public ByteBuf getBuf() {
        return this.buf;
    }

    @Override
    public ByteArrayOutputStream getStream() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            baos.write(getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos;
    }

    @Override
    public String getString() {
        return ByteKits.toString(this.buf);
    }

    @Override
    public Json getJson() {
        String string = getString();
        return Json.parse(string);
    }

    @Override
    public int getStatusCode() {
        return 0;
    }

    @Override
    public int getContentLength() {
        return 0;
    }
}

@@ -130,21 +130,74 @@
 llHttpRe
-qu
+sponse;%0Aimport io.netty.handler.codec.http.HttpHeaderNam
 es
-t
 ;%0Aimport
@@ -351,16 +351,60 @@
 teKits;%0A
+import org.easyarch.netpet.kits.StringKits;%0A
 import o
@@ -718,29 +718,31 @@
 llHttpRe
-quest request
+sponse response
 ;%0A    pr
@@ -842,21 +842,23 @@
 tpRe
-quest request
+sponse response
 )%7B%0A 
@@ -875,23 +875,25 @@
 s.re
-quest = request
+sponse = response
 ;%0A  
@@ -915,21 +915,22 @@
 ers = re
-quest
+sponse
 .headers
@@ -958,13 +958,14 @@
 = re
-quest
+sponse
 .con
@@ -2200,33 +2200,56 @@
 %0A        return 
-0
+response.status().code()
 ;%0A    %7D%0A%0A    @Ov
@@ -2303,16 +2303,228 @@
     
-return 0
+String contentLength = getHeader(%0A                HttpHeaderNames.CONTENT_LENGTH.toString());%0A        if (StringKits.isEmpty(contentLength))%7B%0A            return 0;%0A        %7D%0A        return Integer.parseInt(contentLength)
 ;%0A  

refactor asynchttpclient