package com.wizzardo.servlet;

import com.wizzardo.http.response.Response;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Locale;

/**
 * @author: wizzardo
 * Date: 26.10.14
 */
public class HttpResponse extends Response implements HttpServletResponse {
    private ByteArrayOutputStream buffer;
    private PrintWriter writer;

    boolean hasWriter() {
        return writer != null;
    }

    byte[] getData() {
        if (writer == null)
            return null;

        writer.flush();
        return buffer.toByteArray();
    }

    @Override
    public void addCookie(Cookie cookie) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public String encodeURL(String url) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public String encodeRedirectURL(String url) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public String encodeUrl(String url) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public String encodeRedirectUrl(String url) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void sendError(int sc, String msg) throws IOException {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void sendError(int sc) throws IOException {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void sendRedirect(String location) throws IOException {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void setDateHeader(String name, long date) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void addDateHeader(String name, long date) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void setHeader(String name, String value) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void addHeader(String name, String value) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void setIntHeader(String name, int value) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void addIntHeader(String name, int value) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void setStatus(int sc) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void setStatus(int sc, String sm) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public int getStatus() {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public String getHeader(String name) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public Collection<String> getHeaders(String name) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public Collection<String> getHeaderNames() {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public String getCharacterEncoding() {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public String getContentType() {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        if (writer == null)
            writer = new PrintWriter(buffer = new ByteArrayOutputStream());

        return writer;
    }

    @Override
    public void setCharacterEncoding(String charset) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void setContentLength(int len) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void setContentLengthLong(long len) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void setContentType(String type) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void setBufferSize(int size) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public int getBufferSize() {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void flushBuffer() throws IOException {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void resetBuffer() {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public boolean isCommitted() {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void reset() {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public void setLocale(Locale loc) {
        throw new UnsupportedOperationException("Not implemented yet.");
    }

    @Override
    public Locale getLocale() {
        throw new UnsupportedOperationException("Not implemented yet.");
    }
}

@@ -537,16 +537,80 @@
  writer;
+%0A    private int status = 200;%0A    private String statusMessage;
 %0A%0A    bo
@@ -2310,19 +2310,19 @@
 ic void 
-set
+add
 Header(S
@@ -2361,219 +2361,32 @@
     
-throw new UnsupportedOperationException(%22Not implemented yet.%22);%0A    %7D%0A%0A    @Override%0A    public void addHeader(String name, String value) %7B%0A        throw new UnsupportedOperationException(%22Not implemented yet.%22
+appendHeader(name, value
 );%0A 
@@ -2472,70 +2472,42 @@
     
-throw new UnsupportedOperationException(%22Not implemented yet.%22
+header(name, String.valueOf(value)
 );%0A 
@@ -2593,70 +2593,48 @@
     
-throw new UnsupportedOperationException(%22Not implemented yet.%22
+appendHeader(name, String.valueOf(value)
 );%0A 
@@ -2701,71 +2701,19 @@
     
-throw new UnsupportedOperationException(%22Not implemented yet.%22)
+status = sc
 ;%0A  
@@ -2790,194 +2790,376 @@
     
-throw new UnsupportedOperationException(%22Not implemented yet.%22);%0A    %7D%0A%0A    @Override%0A    public int getStatus() %7B%0A        throw new UnsupportedOperationException(%22Not implemented yet.%22)
+status = sc;%0A        statusMessage = sm;%0A    %7D%0A%0A    @Override%0A    protected byte%5B%5D statusToBytes() %7B%0A        if (statusMessage == null)%0A            return (%22HTTP/1.1 %22 + status + %22%5Cr%5Cn%22).getBytes();%0A        else%0A            return (%22HTTP/1.1 %22 + status + %22 %22 + statusMessage + %22%5Cr%5Cn%22).getBytes();%0A    %7D%0A%0A    @Override%0A    public int getStatus() %7B%0A        return status
 ;%0A  
@@ -3232,70 +3232,26 @@
     
-throw new UnsupportedOperationException(%22Not implemented yet.%22
+return header(name
 );%0A 
@@ -3338,70 +3338,27 @@
     
-throw new UnsupportedOperationException(%22Not implemented yet.%22
+return headers(name
 );%0A 
@@ -3438,70 +3438,27 @@
     
-throw new UnsupportedOperationException(%22Not implemented yet.%22
+return headerNames(
 );%0A 

implement some methods