package com.xunmei.common.core.http.service.params;

import okhttp3.FormBody;
import okhttp3.Request;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;


public class HttpParamsPost extends HttpParams {


    private final Map<String, Object> params;

    public HttpParamsPost(final String url, final Map<String, Object> params) {
        this.setUrl(url);
        this.params = Optional.ofNullable(params)
                              .orElse(Collections.emptyMap());
    }

    @Override
    protected Request.Builder toBuild() {
        final FormBody.Builder builder = new FormBody.Builder();
        this.params.forEach((k, v) -> {
            if (v == null) {
                return;
            }
            builder.addEncoded(k, v.toString());
        });
        return new Request.Builder().url(this.getUrl())
                                    .post(builder.build());
    }

    @Override
    public HttpParams put(final String key, final Object page) {
        this.params.put(key, page);
        return this;
    }


    @Override
    public String toString() {
        return this.getUrl() + " " + this.params;
    }
}
