package w.soulofpluto.camera;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class APICall1 extends AsyncTask<Void, Void, Void> {
    private final Context mContext;
    private HashMap<String, String> hashMapJson = null;
    private HashMap<String, File> hashMapFiles = null;
    private HashMap<String, String> hashMap = null;
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    //private final String credential;
    //OkHttpClient client = new OkHttpClient();
    OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(1000, TimeUnit.SECONDS)
            .writeTimeout(1000, TimeUnit.SECONDS)
            .readTimeout(1000, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
            .build();
    private String json;
    private String url;
    Object object;
    private static String result;
    private boolean isDialogCancelled;
    APIRespose apiRespose;
    //private ACProgressFlower dialog;
    // basic methos for call back
    /**
     * @param url
     * @param hashMapFiles
     * @param hashMapJson
     * @param object       view context
     * @param apiRespose
     */
    public APICall1(String url, HashMap<String, File> hashMapFiles, HashMap<String, String> hashMapJson, Object object, APIRespose apiRespose) {
        this.url = url;
        this.object = object;
        this.apiRespose = apiRespose;
        mContext = (Context) object;
        this.hashMapJson = hashMapJson;
        this.hashMapFiles = hashMapFiles;
    }
    String postMultiPart(String url, HashMap<String, File> hashMapFiles, HashMap<String, String> hashMapJson) throws IOException {
        MultipartBody.Builder multiBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        // add type entity
        for (String key : hashMapJson.keySet()) {
            multiBuilder.addFormDataPart(key, hashMapJson.get(key));
        }
        // add type entity
        if (hashMapFiles != null) {
            MediaType mediaType = MediaType.parse("image/jpg");
            for (String key : hashMapFiles.keySet()) {
                if (hashMapFiles.get(key) != null) {
                    String fileName = hashMapFiles.get(key).getName();
                    multiBuilder.addFormDataPart("base64OfJpeg", fileName, RequestBody.create(mediaType, hashMapFiles.get(key)));
                }
            }
        }
        //Response response = null;
           //System.out.println("Data =====>"+multiBuilder.toString());
            RequestBody requestBody = multiBuilder.build();
            Request request = new Request.Builder()
                    //.header("Authorization", credential)
                    .url(url)
                    .post(requestBody)
                    .build();
            Response response  = client.newCall(request).execute();
            if(response==null || response.body()== null){
                System.out.println("Called =============>");
                 response  = client.newCall(request).execute();
            }
            return response.body().string();
    }
   @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Void... voids){
        result = "";
        //.if multipart
        if (hashMapJson != null) {
                //Thread.sleep(400);
                try{
                    result = postMultiPart(url, hashMapFiles, hashMapJson);
                }catch(IOException e) {
                    e.printStackTrace();
                }
         }
        return null;
    }
    @Override
    protected void onPostExecute(Void aVoid) {
        System.out.println("Response ====>" + result);
        apiRespose.onResponse(result);
        super.onPostExecute(aVoid);
    }
    public interface APIRespose {
        void onResponse(String result);
    }
}
