package net.zephyrizing.http_server;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.SequenceInputStream;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import net.zephyrizing.http_server.HttpRequest;
import net.zephyrizing.http_server.HttpRequest.Method;
import net.zephyrizing.http_server.HttpResponse;

import static net.zephyrizing.http_server.HttpRequest.Method.*;

public class HttpProtocol {
    public static HttpRequest requestFromLines(Stream<String> lines) {
        List<String> linesList = lines.filter(Predicate.isEqual("").negate())
            .limit(1).collect(Collectors.toList());

        if (linesList.isEmpty()) {
            return null;
        }

        String requestLine = linesList.get(0);
        RequestBuilder b = new RequestBuilder();

        try {
            parseRequestLine(b, requestLine);
        } catch (IllegalArgumentException e) {
            return null;
        }

        return b.build();
    }

    public static HttpRequest requestFromInputStream(InputStream stream) {
        BufferedReader r = new BufferedReader(new InputStreamReader(stream));
        RequestBuilder b = new RequestBuilder();

        try {
            String s = r.readLine();
            while ("".equals(s)) {
                s = r.readLine();
            }
            if (s == null) {
                return null;
            }
            parseRequestLine(b, s);
        } catch (IOException e) {
            return null;
        } catch (IllegalArgumentException e) {
            return null;
        }

        // List<String> headerLines = new ArrayList<String>();
        // String s = r.readLine();
        // while (!"".equals(s)) {
        //     headerLines.put(s);
        //     s = r.readLine();
        // }

        return b.build();
    }

    public static InputStream responseStream(HttpResponse response) {
        String headStr = Stream.concat(response.getStatusLineStream(),
                                       response.getHeaderStream())
            .collect(Collectors.joining("\r\n", "", "\r\n\r\n"));

        InputStream head = new ByteArrayInputStream(headStr.getBytes());
        InputStream body = response.getData();

        return new SequenceInputStream(head, body);
    }

    private static void parseRequestLine(RequestBuilder b, String requestLine) {
        String[] methodPathProto = requestLine.split(" ");

        if (methodPathProto.length != 3) {
            throw new IllegalArgumentException();
        }

        b.method(Method.valueOf(methodPathProto[0]))
            .path(methodPathProto[1])
            .protocolVersion(methodPathProto[2].replace("HTTP/", ""));
    }
}

@@ -603,553 +603,8 @@
 ol %7B
-%0A    public static HttpRequest requestFromLines(Stream%3CString%3E lines) %7B%0A        List%3CString%3E linesList = lines.filter(Predicate.isEqual(%22%22).negate())%0A            .limit(1).collect(Collectors.toList());%0A%0A        if (linesList.isEmpty()) %7B%0A            return null;%0A        %7D%0A%0A        String requestLine = linesList.get(0);%0A        RequestBuilder b = new RequestBuilder();%0A%0A        try %7B%0A            parseRequestLine(b, requestLine);%0A        %7D catch (IllegalArgumentException e) %7B%0A            return null;%0A        %7D%0A%0A        return b.build();%0A    %7D
 %0A%0A  

Delete no longer used method