package com.sobot.network.http.builder;

import com.sobot.network.http.request.PostMultipartFormRequest;
import com.sobot.network.http.request.ProgressRequestBody;
import com.sobot.network.http.request.RequestCall;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class PostMultipartFormBuilder extends OkHttpRequestBuilder implements HasParamsable
{
    private List<FileInput> files = new ArrayList<FileInput>();

    private ProgressRequestBody.UploadInterceptor uploadInterceptor;

    @Override
    public RequestCall build()
    {
        return new PostMultipartFormRequest(url, tag, params, headers, files).uploadInterceptor(uploadInterceptor).build();
    }

    public PostMultipartFormBuilder addFile(String name, String filename, File file)
    {
        files.add(new FileInput(name, filename, file));
        return this;
    }

    public static class FileInput
    {
        public String key;
        public String filename;
        public File file;

        public FileInput(String name, String filename, File file)
        {
            this.key = name;
            this.filename = filename;
            this.file = file;
        }

        @Override
        public String toString()
        {
            return "FileInput{" +
                    "key='" + key + '\'' +
                    ", filename='" + filename + '\'' +
                    ", file=" + file +
                    '}';
        }
    }

    //
    @Override
    public PostMultipartFormBuilder url(String url)
    {
        this.url = url;
        return this;
    }

    @Override
    public PostMultipartFormBuilder tag(Object tag)
    {
        this.tag = tag;
        return this;
    }

    @Override
    public PostMultipartFormBuilder params(Map<String, String> params)
    {
        this.params = params;
        return this;
    }

    @Override
    public PostMultipartFormBuilder addParams(String key, String val)
    {
        if (this.params == null)
        {
            params = new LinkedHashMap<String,String>();
        }
        params.put(key, val);
        return this;
    }

    @Override
    public PostMultipartFormBuilder headers(Map<String, String> headers)
    {
        this.headers = headers;
        return this;
    }


    @Override
    public PostMultipartFormBuilder addHeader(String key, String val)
    {
        if (this.headers == null)
        {
            headers = new LinkedHashMap<String,String>();
        }
        headers.put(key, val);
        return this;
    }

    public PostMultipartFormBuilder uploadInterceptor(ProgressRequestBody.UploadInterceptor uploadInterceptor) {
        this.uploadInterceptor = uploadInterceptor;
        return this;
    }


}
