package com.assimilate.alltrans.http;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

import java.io.IOException;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.http.GET;
import retrofit2.http.Query;
import retrofit2.http.Url;

public class GoogleTranslator extends Translator<GoogleTranslator.GoogleTranslateCallback> {

    private final static String URL = "https://translate.googleapis.com/translate_a/single";
    private final static String CLIENT = "gtx";
    private final static String DT = "t";

    @Override
    public void translate(HashMap<String, String> params, GoogleTranslateCallback googleTranslateCallback) {
        if (null == googleTranslateCallback) {
            return;
        }
        if (null == params || params.isEmpty()) {
            googleTranslateCallback.onResponse(null, "Params are null or empty");
            return;
        }

        String text = params.get("text");
        if (TextUtils.isEmpty(text) || text.trim().isEmpty()) {
            googleTranslateCallback.onResponse(null, "Text is empty");
            return;
        } else {
            text = text.trim();
        }
        String sl = params.get("sourceLanguage");
        if (TextUtils.isEmpty(sl)) {
            googleTranslateCallback.onResponse(null, "Source language is empty");
            return;
        }
        String tl = params.get("translationLanguage");
        if (TextUtils.isEmpty(tl)) {
            googleTranslateCallback.onResponse(null, "Translation language is empty");
            return;
        }

        String ie = params.get("inputEncode");
        ie = TextUtils.isEmpty(ie) ? "UTF-8" : ie;
        String oe = params.get("outputEncode");
        oe = TextUtils.isEmpty(oe) ? "UTF-8" : oe;

        RetrofitClient retrofitClient = RetrofitClient.getInstance();
        Retrofit retrofit = retrofitClient.getRetrofitClient();
        GoogleTranslateApi googleTranslateApi = retrofit.create(GoogleTranslateApi.class);
        googleTranslateApi.api(
                URL, CLIENT, DT, ie, oe, sl, tl, text
        ).enqueue(new Callback<JsonArray>() {
            @Override
            public void onResponse(Call<JsonArray> call, Response<JsonArray> response) {
                if (response.isSuccessful()) {
                    JsonArray jsonArray = response.body();
                    if (null != jsonArray && !jsonArray.isEmpty()) {
                        try {
                            StringBuilder builder = new StringBuilder();
                            JsonArray array = jsonArray.get(0).getAsJsonArray();
                            if (null != array && !array.isEmpty()) {
                                for (JsonElement jsonElement : array) {
                                    builder.append(jsonElement.getAsJsonArray().get(0).getAsString());
                                }
                                googleTranslateCallback.onResponse(builder.toString(), null);
                            } else {
                                googleTranslateCallback.onResponse(null, "Empty translation array");
                            }
                        } catch (Exception e) {
                            googleTranslateCallback.onResponse(null, "Parsing error: " + e.getMessage());
                        }
                    } else {
                        googleTranslateCallback.onResponse(null, "Empty response body");
                    }
                } else {
                    googleTranslateCallback.onResponse(null, "Response not successful: " + response.message());
                }
            }

            @Override
            public void onFailure(Call<JsonArray> call, Throwable t) {
                if (t instanceof IOException) {
                    Log.e("GoogleTranslator", "Network error", t);
                    googleTranslateCallback.onFailure("Network error: " + t.getMessage());
                } else {
                    Log.e("GoogleTranslator", "Conversion error", t);
                    googleTranslateCallback.onFailure("Conversion error: " + t.getMessage());
                }
            }
        });
    }

    public interface GoogleTranslateApi {
        @GET
        Call<JsonArray> api(
                @Url String url,
                @Query("client") String client,
                @Query("dt") String dt,
                @Query("ie") String ie,
                @Query("oe") String oe,
                @Query("sl") String sl,
                @Query("tl") String tl,
                @Query("text") String text);
    }

    public interface GoogleTranslateCallback {
        void onResponse(String val, String errorMessage);
        void onFailure(String errorMessage);
    }
}
