package com.jscape.inet.http.protocol;

import com.jscape.inet.http.protocol.messages.Headers;
import com.jscape.inet.http.protocol.messages.StatusCode;

import java.io.InputStream;

import static com.jscape.util.Parameters.assertNotNull;

/**
 * @author Alex Usun
 */
public class Response {

    public final String protocolVersion;
    public final int statusCode;
    public final String reasonPhrase;
    public final Headers headers;
    public final InputStream body;
    public final Headers trailerHeaders;

    public Response(String protocolVersion, int statusCode, String reasonPhrase, Headers headers, InputStream body, Headers trailerHeaders) {
        assertNotNull(protocolVersion);
        this.protocolVersion = protocolVersion;

        this.statusCode = statusCode;

        assertNotNull(reasonPhrase);
        this.reasonPhrase = reasonPhrase;

        assertNotNull(headers);
        this.headers = headers;

        assertNotNull(body);
        this.body = body;
        
        assertNotNull(trailerHeaders);
        this.trailerHeaders = trailerHeaders;
    }
    
    public Response(String protocolVersion, int statusCode, String reasonPhrase, Headers headers, InputStream body) {
        this(protocolVersion, statusCode, reasonPhrase, headers, body, new Headers());
    }
    
    public boolean ok() {
        return StatusCode.OK == this.statusCode;
    }

    public boolean intermediate() {
        return StatusCode.intermediate(this.statusCode);
    }

    public boolean error() {
        return StatusCode.error(this.statusCode);
    }
    
    @Override
    public String toString() {
        return "Response {"
               + "protocolVersion='" + this.protocolVersion + '\''
               + ", statusCode=" + this.statusCode
               + ", reasonPhrase='" + this.reasonPhrase + '\''
               + ", headers=" + this.headers
               + ", body=" + this.body
               + ", trailerHeaders=" + this.trailerHeaders
               + '}';
    }
}
