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 HTTP MOVE request.
 *
 * @see <a href="http://webdav.org/specs/rfc4918.html#rfc.section.9.9">RFC 4918, Section 9.9</a>
 */
public class Move extends AbstractMethod {

    private final URI dest;

    private final boolean overwrite;

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

    @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");
        }
    }

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

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