package org.jeecg.modules.okki.translate.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHeaders;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.okki.translate.dto.TranslateRequestDto;
import org.jeecg.modules.okki.translate.dto.TranslateResults;
import org.jeecg.modules.okki.translate.param.AwsParaphraseParam;
import org.jeecg.modules.okki.translate.service.AwsParaphraseService;
import org.jeecg.modules.okki.translate.service.AwsTranslateService;
import org.jeecg.modules.okki.translate.service.TranslateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/website")
public class TranslateApiController {

    @Autowired
    private TranslateService translateService;

    @Autowired
    private AwsTranslateService awsTranslateService;

    @Autowired
    private AwsParaphraseService awsParaphraseService;

    public static final Map<String, String> LANGUAGE_MAP;

    @PostMapping(value = "/translate")
    public Result<TranslateResults> add(@RequestBody TranslateRequestDto translateRequestDto, @RequestHeader(HttpHeaders.AUTHORIZATION) String authorization) throws UnsupportedEncodingException {
        log.info("请求参数 param{} authorization{}", translateRequestDto.toString(), authorization);
        String Authorization = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImRlYW4iLCJleHAiOjE3MTQ0NjU5Njl9.QdCBYR8mioXcKGi1n5RdMQ5RbJHCSGbE8dkdwYuOXh4";

        if (!authorization.equals(Authorization)) {
            return Result.error("sign 验证不通过");
        }
        // 判断aws语言翻译是否支持
        boolean isExist = true;
        if (!LANGUAGE_MAP.containsKey(translateRequestDto.getFrom())) {
            isExist = false;
        }
        ArrayList<String> translateRequestDtoTo = translateRequestDto.getTo();
        for (String s : translateRequestDtoTo) {
            if (!LANGUAGE_MAP.containsKey(s)) {
                isExist = false;
            }
        }
        TranslateResults translateResults = new TranslateResults();
        if (isExist) {
            translateResults = awsTranslateService.doTranslate(translateRequestDto);
        }else {
            translateResults = translateService.doTranslate(translateRequestDto);
        }
        return Result.OK(translateResults);
    }

    @PostMapping(value = "/awsTranslate")
    public Result<TranslateResults> awsTranslate(@RequestBody TranslateRequestDto translateRequestDto, @RequestHeader(HttpHeaders.AUTHORIZATION) String authorization) throws UnsupportedEncodingException {
        log.info("请求参数 param{} authorization{}", translateRequestDto.toString(), authorization);
        String Authorization = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImRlYW4iLCJleHAiOjE3MTQ0NjU5Njl9.QdCBYR8mioXcKGi1n5RdMQ5RbJHCSGbE8dkdwYuOXh4";

        if (!authorization.equals(Authorization)) {
            return Result.error("sign 验证不通过");
        }
        TranslateResults translateResults = awsTranslateService.doTranslate(translateRequestDto);
        return Result.OK(translateResults);
    }

    @PostMapping(value = "paraphrase")
    public Result<String> paraphrase(@RequestBody AwsParaphraseParam awsParaphraseParam) throws UnsupportedEncodingException {
        log.info("请求参数 param{} authorization{}", awsParaphraseParam.toString());
        String result = awsParaphraseService.doParaphrase(awsParaphraseParam);
        return Result.OK(result);
    }

    static {
        LANGUAGE_MAP = new HashMap<>();
        LANGUAGE_MAP.put("zh", "ZH_CN");
        LANGUAGE_MAP.put("en", "EN_US");
        LANGUAGE_MAP.put("ar", "AR");
        LANGUAGE_MAP.put("bg", "BG");
        LANGUAGE_MAP.put("hr", "HR");
        LANGUAGE_MAP.put("cs", "CS");
        LANGUAGE_MAP.put("da", "DA");
        LANGUAGE_MAP.put("nl", "NL");
        LANGUAGE_MAP.put("fi", "FI");
        LANGUAGE_MAP.put("fr", "FR");
        LANGUAGE_MAP.put("de", "DE");
        LANGUAGE_MAP.put("el", "EL");
        LANGUAGE_MAP.put("hi", "HI");
        LANGUAGE_MAP.put("it", "IT");
        LANGUAGE_MAP.put("ja", "JA");
        LANGUAGE_MAP.put("ko", "KO");
        LANGUAGE_MAP.put("no", "NB");
        LANGUAGE_MAP.put("pl", "PL");
        LANGUAGE_MAP.put("pt", "PT");
        LANGUAGE_MAP.put("ro", "RO");
        LANGUAGE_MAP.put("ru", "RU");
        LANGUAGE_MAP.put("es", "ES");
        LANGUAGE_MAP.put("sv", "SV");
        LANGUAGE_MAP.put("ca", "CA");
        LANGUAGE_MAP.put("tl", "FIL");
        LANGUAGE_MAP.put("iw", "HE");
        LANGUAGE_MAP.put("id", "ID");
        LANGUAGE_MAP.put("lv", "LV");
        LANGUAGE_MAP.put("lt", "LT");
        LANGUAGE_MAP.put("sr", "SR");
        LANGUAGE_MAP.put("sk", "SK");
        LANGUAGE_MAP.put("sl", "SL");
        LANGUAGE_MAP.put("uk", "UK");
        LANGUAGE_MAP.put("vi", "VI");
        LANGUAGE_MAP.put("gl", "GL");
        LANGUAGE_MAP.put("hu", "HU");
        LANGUAGE_MAP.put("mt", "MT");
        LANGUAGE_MAP.put("th", "TH");
        LANGUAGE_MAP.put("tr", "TR");
        LANGUAGE_MAP.put("fa", "FA");
        LANGUAGE_MAP.put("af", "AF");
        LANGUAGE_MAP.put("ms", "MS");
        LANGUAGE_MAP.put("sw", "SW");
        LANGUAGE_MAP.put("ga", "GA");
        LANGUAGE_MAP.put("cy", "CY");
        LANGUAGE_MAP.put("is", "IS");
        LANGUAGE_MAP.put("mk", "MK");
        LANGUAGE_MAP.put("sq", "SQ");
        LANGUAGE_MAP.put("et", "ET");
    }
}
