package org.xbib.io.webdav.client;

import org.xbib.io.webdav.api.DavConstants;
import org.xbib.io.webdav.common.DavPropertySet;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class WebDavFile implements WebDavElement {

    private final URI uri;

    private final DavPropertySet davPropertySet;

    public WebDavFile(URI uri, DavPropertySet davPropertySet) {
        this.uri = uri;
        this.davPropertySet = davPropertySet;
    }

    public String getCreated() {
        return davPropertySet.get(DavConstants.PROPERTY_CREATIONDATE).getValue().toString();
    }

    public String getLastModified() {
        return davPropertySet.get(DavConstants.PROPERTY_GETLASTMODIFIED).getValue().toString();
    }

    public String getContentType() {
        return davPropertySet.get(DavConstants.PROPERTY_GETCONTENTTYPE).getValue().toString();
    }

    public Integer getLength() {
        return Integer.valueOf(davPropertySet.get(DavConstants.PROPERTY_GETCONTENTLENGTH).getValue().toString());
    }

    public String getNameUrlEncoded() {
        try {
            return URLEncoder.encode(getName(), StandardCharsets.UTF_8.toString()).replaceAll("\\+", "%20");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public URI getUri() {
        return uri;
    }

    @Override
    public DavPropertySet getDavPropertySet() {
        return davPropertySet;
    }
}
