package leigh.esp.sdk;

import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;

import java.io.InputStream;
import java.util.Objects;

/**
 * A simple class to hold a content part.
 *
 * @author Alex Leigh
 */
public class ContentPart {
    private final InputStream inputStream;
    private final String name;
    private final String mimeType;
    private final boolean isText;

    public ContentPart(InputStream inputStream, String name, String mimeType, boolean isText) {
        this.inputStream = inputStream;
        this.name = name;
        this.mimeType = mimeType;
        this.isText = isText;
    }

    public void addToBuilder(MultipartEntityBuilder builder) {
        builder.addBinaryBody(name, inputStream, ContentType.create(mimeType), null);
    }

    public String getName() {
        return name;
    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public String getMimeType() {
        return mimeType;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ContentPart that = (ContentPart) o;
        return isText == that.isText && Objects.equals(inputStream, that.inputStream)
                && Objects.equals(name, that.name) && Objects.equals(mimeType, that.mimeType);
    }

    @Override
    public String toString() {
        return "ContentPart{" +
                "inputStream=" + inputStream +
                ", name='" + name + '\'' +
                ", mimeType='" + mimeType + '\'' +
                ", isText=" + isText +
                '}';
    }

    @Override
    public int hashCode() {
        return Objects.hash(inputStream, name, mimeType, isText);
    }

    public boolean isText() {
        return isText;
    }
}
