package com.sobot.network.customhttp;

import com.sobot.network.customhttp.bean.CommCallback;
import com.sobot.network.customhttp.bean.HttpBody;
import com.sobot.network.customhttp.bean.ICommCallback;
import com.sobot.network.customhttp.module.DownLoadHttpRequester;
import com.sobot.network.customhttp.module.GetHttpRequester;
import com.sobot.network.customhttp.module.PostHttpRequester;
import com.sobot.network.customhttp.module.ProvideHttpRequester;
import com.sobot.network.customhttp.module.UpLoadHttpRequester;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;

/**
 * 轻量级网络请求框架MyHttptUtils
 */

public class SobotCustomHttpUtils {
    private static final String TAG = "MyHttpUtils";
    private HttpBody mHttpBody = new HttpBody();//请求体对象
    private ICommCallback callback;

    public static SobotCustomHttpUtils build() {
        return new SobotCustomHttpUtils();
    }

    /**
     * 构造url
     *
     * @param url desc
     * @return this
     */
    public SobotCustomHttpUtils url(String url) {
        mHttpBody.setUrl(url);
        return this;
    }

    /**
     * 构造文件上传的url
     *
     * @param uploadUrl desc
     * @return this
     */
    public SobotCustomHttpUtils uploadUrl(String uploadUrl) {
        mHttpBody.setUploadUrl(uploadUrl);
        return this;
    }

    /**
     * 设置读取时间超时时间，模式30s
     *
     * @param readTimeOut desc
     * @return this
     */
    public SobotCustomHttpUtils setReadTimeOut(int readTimeOut) {
        mHttpBody.setReadTimeOut(readTimeOut);
        return this;
    }

    /**
     * 设置链接时间超时时间，模式5s
     *
     * @param connTimeOut desc
     * @return this
     */
    public SobotCustomHttpUtils setConnTimeOut(int connTimeOut) {
        mHttpBody.setConnTimeOut(connTimeOut);
        return this;
    }

    /**
     * 设置请求体
     *
     * @param mHttpBody desc
     * @return this
     */
    public SobotCustomHttpUtils setHttpBody(HttpBody mHttpBody) {
        this.mHttpBody = mHttpBody;
        return this;
    }

    /**
     * 获取httpbody
     *
     * @return this
     */
    public HttpBody getHttpBody() {
        return mHttpBody;
    }

    /**
     * 添加参数----键值对
     *
     * @param key   desc
     * @param value desc
     * @return this
     */
    public SobotCustomHttpUtils addParam(String key, Object value) {
        mHttpBody.addParam(key, value);
        return this;
    }

    /**
     * 设置请求参数类型参数
     */
    public SobotCustomHttpUtils setContentType(String contentType) {
        mHttpBody.setContentType(contentType);
        return this;
    }

    /**
     * 设置请求参数-----按集合
     *
     * @param params desc
     * @return this
     */
    public SobotCustomHttpUtils addParams(Map<String, Object> params) {
        mHttpBody.setParams(params);
        return this;
    }

    /**
     * 设置请求头参数-----按集合
     *
     * @param headers desc
     * @return this
     */
    public SobotCustomHttpUtils addHeaders(Map<String, String> headers) {
        mHttpBody.setHeaders(headers);
        return this;
    }

    /**
     * 设置文件保存的目录
     *
     * @param dir desc
     * @return this
     */
    public SobotCustomHttpUtils setFileSaveDir(String dir) {
        mHttpBody.setFileSaveDir(dir);
        return this;
    }

    /**
     * 添加文件---按路径
     *
     * @param filePath desc
     * @return this
     */
    public SobotCustomHttpUtils addFile(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            mHttpBody.addFile(filePath);
        } else {
            callback.onFailed(new FileNotFoundException("NOFile"));
        }
        return this;
    }

    /**
     * 添加文件---按文件
     *
     * @param file desc
     * @return this
     */
    public SobotCustomHttpUtils addFile(File file) {
        if (file.exists()) {
            mHttpBody.addFile(file);
        } else {
            callback.onFailed(new FileNotFoundException("NOFile"));
        }
        return this;
    }

    /**
     * 添加文件---按文件列表
     *
     * @param fileList desc
     * @return this
     */
    public SobotCustomHttpUtils addFiles(List<File> fileList) {
        for (File file : fileList) {
            if (file.exists()) {
                mHttpBody.addFile(file);
            } else {
                callback.onFailed(new FileNotFoundException("NOFile"));
            }
        }
        return this;
    }

    /**
     * 添加文件---按文件路径列表
     *
     * @param filePaths desc
     * @return this
     */
    public SobotCustomHttpUtils addFilesByPath(List<String> filePaths) {
        for (String filePath : filePaths) {
            if (new File(filePath).exists()) {
                mHttpBody.addFile(filePath);
            } else {
                callback.onFailed(new FileNotFoundException("NOFile"));
            }
        }
        return this;
    }

    /**
     * 执行Get请求
     *
     * @param callback desc
     * @return this
     */
    public SobotCustomHttpUtils onExecute(CommCallback callback) {
        this.callback = callback;
        ProvideHttpRequester requester = new ProvideHttpRequester(new GetHttpRequester(mHttpBody, callback));
        requester.startRequest();//开始请求
        return this;
    }

    /**
     * 执行Post请求
     *
     * @param callback desc
     * @return this
     */
    public SobotCustomHttpUtils onExecuteByPost(CommCallback callback) {
        this.callback = callback;
        ProvideHttpRequester requester = new ProvideHttpRequester(new PostHttpRequester(mHttpBody, callback));
        requester.startRequest();//开始请求
        return this;
    }

    /**
     * 下载文件
     *
     * @param callback desc
     * @return this
     */
    public SobotCustomHttpUtils onExecuteDwonload(CommCallback callback) {
        this.callback = callback;
        ProvideHttpRequester requester = new ProvideHttpRequester(new DownLoadHttpRequester(mHttpBody, callback));
        requester.startRequest();//开始请求
        return this;
    }

    /**
     * 上传文件
     *
     * @param callback desc
     * @return this
     */
    public SobotCustomHttpUtils onExecuteUpLoad(CommCallback callback) {
        this.callback = callback;
        ProvideHttpRequester requester = new ProvideHttpRequester(new UpLoadHttpRequester(mHttpBody, callback));
        requester.startRequest();//开始请求
        return this;
    }

}
