package com.zhaohaoting.framework.requset;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.zhaohaoting.framework.mvchelper.mvc.RequestHandle;
import com.zhaohaoting.framework.mvchelper.mvc.ResponseSender;
import com.zhaohaoting.framework.abs.entity.ResultEntity;
import com.zhaohaoting.framework.mvchelper.okhttp.okhttp.DownloadCallBack;
import com.zhaohaoting.framework.mvchelper.okhttp.okhttp.GetMethod;
import com.zhaohaoting.framework.mvchelper.okhttp.okhttp.HttpMethod;
import com.zhaohaoting.framework.mvchelper.okhttp.okhttp.PostFileMethod;
import com.zhaohaoting.framework.mvchelper.okhttp.okhttp.PostMethod;

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

/**
 * author：Created by ZHT on 2017/11/10 13:57
 * email：526309416@qq.com
 * desc：请求方式
 */
public class RequestApi {

    /**
     * 异步post请求
     */
    public static <DATA, RESULT_DATA extends ResultEntity<DATA>> RequestHandle executeAsyncPost(String url, Map<String, Object> params, ResponseSender<DATA> sender, ResultCallback<DATA, RESULT_DATA> responseCallback) {
        PostMethod method = new PostMethod(url);
        putHeaderData(method, params, sender, responseCallback);
        return method;
    }

    /**
     * 异步post文件上传
     */
    public static <DATA, RESULT_DATA extends ResultEntity<DATA>> RequestHandle executeAsyncPostFile(String url, List<File> files, ResponseSender<DATA> sender, ResultCallback<DATA, RESULT_DATA> responseCallback) {
        PostFileMethod method = new PostFileMethod(url);
        method.addParam("file", files);
        method.executeAsync(sender, responseCallback);
        return method;
    }

    /**
     * 异步get请求
     */
    public static <DATA, RESULT_DATA extends ResultEntity<DATA>> RequestHandle executeAsyncGet(String url, Map<String, Object> params, ResponseSender<DATA> sender, ResultCallback<DATA, RESULT_DATA> responseCallback) {
        GetMethod method = new GetMethod(url);
        putHeaderData(method, params, sender, responseCallback);
        return method;
    }

    /**
     * 文件下载
     */
    public static RequestHandle executeDownload(String url, DownloadCallBack downloadCallBack, String fileDir, String fileName) {
        GetMethod method = new GetMethod(url);
        if (TextUtils.isEmpty(fileName)) {
            int separatorIndex = fileDir.lastIndexOf("/");
            fileName = (separatorIndex < 0) ? url : url.substring(separatorIndex + 1, url.length());
        }
        method.downloadAsyn(downloadCallBack, fileDir, fileName);
        return method;
    }


    /**
     * 加密和封装一些必要的数据
     */
    public static <DATA, RESULT_DATA extends ResultEntity<DATA>> void putHeaderData(HttpMethod method, Map<String, Object> params, ResponseSender<DATA> sender, ResultCallback<DATA, RESULT_DATA> responseCallback) {
//        method.addParams(params);
//        method.addHeader("accessToken", TextUtils.isEmpty(MyApplication.getUser().getAccessToken()) ? "" : MyApplication.getUser().getAccessToken());
        String pramsJson = new Gson().toJson(params);//加密操作
        Map<String, Object> postPrams = new HashMap<>();
//        postPrams.put("data", AESCoder.encrypt(pramsJson));
//        method.addHeader("accessToken", TextUtils.isEmpty(MyApplication.getUser().getAccessToken()) ? "" : MyApplication.getUser().getAccessToken());

        method.addParams(postPrams);
        responseCallback.setSender(sender);
        method.executeAsync(responseCallback);
    }
}
