/*
 * Copyright (c) 2004-2017, by Alex Leigh.
 * All rights reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 */

package lc.mecha.http.server;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lc.mecha.http.client.HttpUtil;
import org.eclipse.jetty.server.Request;

import java.io.UnsupportedEncodingException;

/**
 * This class represents a request from a HTTP server. It contains the HTTP servlet request and response
 * objects and is useful for situations where only a single object can be passed through an API (such as is
 * the case with the message fabric API).
 * <p>
 * Although the http servlet & response are available in this class out of a prevailing sense of pragmatism over
 * correctness, their use should as a matter of course be avoided as a leaky abstraction. Instead, it is better
 * to implement the required functionality in this class in order to provide a neutral API.
 *
 * @author Alex Leigh
 */
public final class HttpTransaction {
    public final Request request;
    public final HttpServletRequest httpServletRequest;
    public final HttpServletResponse httpServletResponse;
    private String prefix;

    public HttpTransaction(final Request request, HttpServletRequest httpServletRequest,
                           final HttpServletResponse httpServletResponse) throws UnsupportedEncodingException {
        this.request = request;
        this.httpServletRequest = httpServletRequest;
        this.httpServletResponse = httpServletResponse;
    }

    @Override
    public String toString() {
        return "WebRequest{" +
                "request=" + request +
                ", httpServletRequest=" + httpServletRequest +
                ", httpServletResponse=" + httpServletResponse +
                "} " + super.toString();
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getPathInfo() {
        return httpServletRequest.getPathInfo().substring(prefix.length());
    }

    @Override
    public boolean equals(final Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        final HttpTransaction that = (HttpTransaction) o;

        if (httpServletRequest != null ? !httpServletRequest.equals(that.httpServletRequest) : that.httpServletRequest != null)
            return false;
        if (httpServletResponse != null ? !httpServletResponse.equals(that.httpServletResponse) : that.httpServletResponse != null)
            return false;
        return request != null ? request.equals(that.request) : that.request == null;

    }

    @Override
    public int hashCode() {
        int result = request != null ? request.hashCode() : 0;
        result = 31 * result + (httpServletRequest != null ? httpServletRequest.hashCode() : 0);
        result = 31 * result + (httpServletResponse != null ? httpServletResponse.hashCode() : 0);
        return result;
    }

    public boolean isMultipartRequest() {
        return HttpUtil.isMultipartType(request.getContentType());
    }

    public Cookie getCookie(String name) {
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie c : cookies) {
                if (c.getName().equals(name)) return c;
            }
        }
        return null;
    }
}
