package cn.octopusyan.dmt.translate.processor;


import cn.octopusyan.dmt.common.util.JsonUtil;
import cn.octopusyan.dmt.translate.TranslateApi;
import tools.jackson.databind.JsonNode;

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

/**
 * 谷歌 免费翻译接口
 *
 * @author octopus_yan@foxmail.com
 */
public class FreeGoogleTranslateProcessor extends AbstractTranslateProcessor {

    public FreeGoogleTranslateProcessor() {
        super(TranslateApi.FREE_GOOGLE);
    }

    @Override
    public String url() {
        return "https://translate.googleapis.com/translate_a/single";
    }

    /**
     * 翻译处理
     *
     * @param source 待翻译单词
     * @return 翻译结果
     */
    @Override
    public String customTranslate(String source) throws IOException, InterruptedException {

        Map<String, Object> header = new HashMap<>();
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 11.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0");
        header.put("Referer", "https://translate.google.com/");
        header.put("Host", "translate.googleapis.com");

        Map<String, Object> form = new HashMap<>();
        form.put("client", "gtx");
        form.put("dt", "t");
        form.put("sl", "auto");
        form.put("tl", "zh-CN");

        Map<String, Object> body = new HashMap<>();
        body.put("q", source);

        StringBuilder retStr = new StringBuilder();
        // TODO 短时大量请求会被ban，需要浏览器验证添加cookie

        String resp = httpUtil.postForm(url(), JsonUtil.parseJsonObject(header), JsonUtil.parseJsonObject(form), JsonUtil.parseJsonObject(body));
        JsonNode json = JsonUtil.parseJsonObject(resp);

        for (JsonNode o : json.get(0)) {
            retStr.append(o.get(0).asString());
        }

        return retStr.toString();
    }
}
