package org.xbib.io.webdav.client.methods;

import org.xbib.io.webdav.api.DavConstants;
import java.net.URI;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

/**
 * Represents an COPY request.
 *
 * @see <a href="http://webdav.org/specs/rfc4918.html#rfc.section.9.8">RFC 4918, Section 9.8</a>
 */
public class Copy extends AbstractMethod {

    private final URI dest;

    private final boolean overwrite;

    private final boolean shallow;

    public Copy(URI dest, boolean overwrite, boolean shallow) {
        this.dest = dest;
        this.overwrite = overwrite;
        this.shallow = shallow;
    }

    @Override
    public void accept(HttpRequest.Builder httpRequest) {
        super.accept(httpRequest);
        httpRequest.setHeader(DavConstants.HEADER_DESTINATION, dest.toASCIIString());
        if (!overwrite) {
            httpRequest.setHeader(DavConstants.HEADER_OVERWRITE, "F");
        }
        if (shallow) {
            httpRequest.setHeader("Depth", "0");
        }
    }

    @Override
    public String method() {
        return "COPY";
    }

    @Override
    public boolean succeeded(HttpResponse<?> response) {
        return response.statusCode() == 201 || response.statusCode() == 204;
    }
}
