package com.blueland.common.core.utils;

import com.blueland.common.core.config.InternationConfig;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.tmt.v20180321.TmtClient;
import com.tencentcloudapi.tmt.v20180321.models.TextTranslateBatchRequest;
import com.tencentcloudapi.tmt.v20180321.models.TextTranslateBatchResponse;

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

public class TranslateUtil {

    private static Credential INSTANCE = new Credential(InternationConfig.getSecretId(), InternationConfig.getSecretKey());

    /**
     * 腾讯翻译
     *
     * @param query 需要翻译的内容 {@link List}<{@link String}>
     * @param from  源语言
     * @param to    目标语言
     * @return {@link Map}<{@link String}, {@link String}>
     */
    public static Map<String, String> translate(List<String> query, String from, String to) {
        Map<String, String> infoMap = new HashMap<>();
        try {
            TextTranslateBatchRequest req = new TextTranslateBatchRequest();
            req.setSourceTextList(query.toArray(new String[0]));
            req.setSource(from);
            req.setTarget(to);
            req.setProjectId(0L);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("tmt.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            TmtClient client = new TmtClient(INSTANCE, "ap-beijing", clientProfile);
            TextTranslateBatchResponse resp = client.TextTranslateBatch(req);
            String[] targetTextList = resp.getTargetTextList();
            for (int i = 0; i < targetTextList.length; i++) {
                infoMap.put(query.get(i), targetTextList[i]);
            }
        } catch (TencentCloudSDKException e) {
            throw new RuntimeException(e);
        }
        return infoMap;
    }


}
