package com.zipcoder.server;

import java.io.IOException;
import java.io.OutputStream;

public class ResponseHello {


    private final OutputStream outputStream;

    public ResponseHello(OutputStream outputStream){
        this.outputStream = outputStream;
    }

    public void writeResponse() throws IOException {
        String responseMessage = "Hello, world!";
        StringBuilder httpResponse = new StringBuilder();

        buildHeader(httpResponse, responseMessage);
        httpResponse.append("\r\n");
        httpResponse.append(responseMessage);

        this.outputStream.write(httpResponse.toString().getBytes());
    }


    private void buildHeader(StringBuilder httpResponse, String responseMessage) {
        httpResponse.append("HTTP/1.1 200 OK\n");
        httpResponse.append("text/html\n");
        httpResponse.append("Content-Length: " + responseMessage.length() + "\n");
    }
}
