package com.ruoyi.aicall.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.aicall.domain.CcTtsAliyun;
import com.ruoyi.aicall.service.ICcTtsAliyunService;
import com.ruoyi.cc.service.IFsConfService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Base64;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/aicall/txvoiceclone")
@Slf4j
public class TxVoiceCloneController extends BaseController {
    private static final MediaType JSON_TYPE = MediaType.parse("application/json; charset=utf-8");
    private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneOffset.UTC);
    private static final OkHttpClient HTTP_CLIENT = new OkHttpClient.Builder()
            .connectTimeout(30, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .build();
    private static final String DEFAULT_ENDPOINT = "mps.tencentcloudapi.com";
    private static final String DEFAULT_ACTION = "SyncDubbing";
    private static final String DEFAULT_VERSION = "2019-06-12";
    private static final String DEFAULT_TEXT_LANG = "zh";
    private static final int DEFAULT_SAMPLE_RATE = 8000;
    private static final int DEFAULT_PITCH = 0;
    private final String prefix = "aicall/txvoiceclone";

    @Autowired
    private IFsConfService fsConfService;

    @Autowired
    private ICcTtsAliyunService ttsAliyunService;

    @RequiresPermissions("aicall:txvoiceclone:view")
    @GetMapping("voiceclone")
    public String voiceClone() {
        return prefix + "/voiceclone";
    }

    @RequiresPermissions("aicall:txvoiceclone:uploadAndClone")
    @Log(title = "腾讯音色克隆-上传音频", businessType = BusinessType.IMPORT)
    @PostMapping("/uploadAndClone")
    @ResponseBody
    public AjaxResult uploadAndClone(HttpServletRequest request, @RequestParam("file") MultipartFile file) {
        try {
            if (file == null || file.isEmpty()) {
                return AjaxResult.error("请先选择录音文件");
            }

            String voiceName = trimToEmpty(request.getParameter("voice_name"));
            if (StringUtils.isBlank(voiceName)) {
                return AjaxResult.error("请填写音色名称");
            }

            String audioLang = normalizeLanguage(trimToEmpty(request.getParameter("audio_lang")));
            String gender = normalizeGender(trimToEmpty(request.getParameter("gender")));
            String age = normalizeAge(trimToEmpty(request.getParameter("age")));
            String description = trimToEmpty(request.getParameter("description"));
            Float rangeStart = parseFloat(request.getParameter("range_start"));
            Float rangeEnd = parseFloat(request.getParameter("range_end"));
            JSONObject account = loadTencentAccount();

            JSONObject body = new JSONObject(true);
            body.put("AudioData", Base64.getEncoder().encodeToString(file.getBytes()));
            body.put("AudioLang", audioLang);
            body.put("VoiceProfile", buildVoiceProfile(voiceName, gender, age, audioLang, description));

            String resourceId = config(account, "resource-id", "");
            if (StringUtils.isNotBlank(resourceId)) {
                body.put("ResourceId", resourceId);
            }

            JSONObject extParam = buildCloneExtParam(rangeStart, rangeEnd);
            if (!extParam.isEmpty()) {
                body.put("ExtParam", extParam.toJSONString());
            }

            JSONObject response = doTencentRequest(account, body);
            JSONObject rsp = response.getJSONObject("Response");
            if (rsp == null) {
                return AjaxResult.error("腾讯云返回结果为空");
            }

            String voiceId = trimToEmpty(rsp.getString("VoiceId"));
            if (StringUtils.isBlank(voiceId)) {
                return AjaxResult.error("腾讯云未返回可用的 VoiceId:\n" + response.toJSONString());
            }

            CcTtsAliyun speaker = saveCloneVoice(voiceName, voiceId, audioLang);

            JSONObject data = new JSONObject(true);
            data.put("voiceId", voiceId);
            data.put("voiceName", speaker.getVoiceName());
            data.put("requestId", rsp.getString("RequestId"));
            data.put("saved", true);
            data.put("languageCode", speaker.getLanguageCode());
            data.put("languageName", speaker.getLanguageName());
            return AjaxResult.success("音色克隆成功，已返回 VoiceId 并写入音色表。", data);
        } catch (Exception e) {
            log.error("uploadAndClone error", e);
            return AjaxResult.error("腾讯音色克隆失败:\n" + e.getMessage());
        }
    }

    @RequiresPermissions("aicall:txvoiceclone:ttsTest")
    @PostMapping("/ttsTest")
    @ResponseBody
    public AjaxResult ttsTest(HttpServletRequest request) {
        try {
            String voiceId = trimToEmpty(request.getParameter("voiceId"));
            String text = trimToEmpty(request.getParameter("text"));
            String textLang = normalizeLanguage(trimToEmpty(request.getParameter("text_lang")));
            if (StringUtils.isBlank(voiceId)) {
                return AjaxResult.error("请先完成克隆并获取 VoiceId");
            }
            if (StringUtils.isBlank(text)) {
                return AjaxResult.error("请输入测试文本");
            }

            JSONObject account = loadTencentAccount();
            JSONObject body = new JSONObject(true);
            body.put("Text", text);
            body.put("TextLang", textLang);
            body.put("VoiceId", voiceId);

            String resourceId = config(account, "resource-id", "");
            if (StringUtils.isNotBlank(resourceId)) {
                body.put("ResourceId", resourceId);
            }

            JSONObject extParam = new JSONObject(true);
            JSONObject synExt = new JSONObject(true);
            synExt.put("sampleRate", configInt(account, "sample-rate", DEFAULT_SAMPLE_RATE));
            synExt.put("pitch", configInt(account, "pitch", DEFAULT_PITCH));
            extParam.put("synExt", synExt);
            body.put("ExtParam", extParam.toJSONString());

            JSONObject response = doTencentRequest(account, body);
            JSONObject rsp = response.getJSONObject("Response");
            if (rsp == null) {
                return AjaxResult.error("腾讯云返回结果为空");
            }

            String audioData = trimToEmpty(rsp.getString("AudioData"));
            if (StringUtils.isBlank(audioData)) {
                return AjaxResult.error("腾讯云未返回试听音频:\n" + response.toJSONString());
            }

            JSONObject data = new JSONObject(true);
            data.put("audioBase64", audioData);
            data.put("audioMime", "audio/wav");
            data.put("audioUrl", trimToEmpty(rsp.getString("AudioUrl")));
            data.put("requestId", trimToEmpty(rsp.getString("RequestId")));
            return AjaxResult.success("试听合成成功", data);
        } catch (Exception e) {
            log.error("ttsTest error", e);
            return AjaxResult.error("腾讯在线试听失败:\n" + e.getMessage());
        }
    }

    private JSONObject loadTencentAccount() {
        JSONObject conf = fsConfService.getAsrConf("/autoload_configs/tx_tts1.conf.xml");
        String secretId = trimToEmpty(conf.getString("secret-id"));
        String secretKey = trimToEmpty(conf.getString("secret-key"));
        if (StringUtils.isBlank(secretId) || StringUtils.isBlank(secretKey)) {
            throw new IllegalStateException("请先在“腾讯云TTS配置”中填写 SecretId 和 SecretKey。");
        }

        JSONObject account = new JSONObject(true);
        account.put("secret-id", secretId);
        account.put("secret-key", secretKey);
        account.put("endpoint", config(conf, "endpoint", DEFAULT_ENDPOINT));
        account.put("action", config(conf, "action", DEFAULT_ACTION));
        account.put("version", config(conf, "version", DEFAULT_VERSION));
        account.put("resource-id", config(conf, "resource-id", ""));
        account.put("sample-rate", config(conf, "sample-rate", String.valueOf(DEFAULT_SAMPLE_RATE)));
        account.put("pitch", config(conf, "pitch", String.valueOf(DEFAULT_PITCH)));
        account.put("verify-peer", config(conf, "verify-peer", "false"));
        return account;
    }

    private JSONObject buildCloneExtParam(Float rangeStart, Float rangeEnd) {
        JSONObject extParam = new JSONObject(true);
        if (rangeStart != null && rangeEnd != null && rangeEnd > rangeStart && rangeStart >= 0) {
            JSONObject cloneExt = new JSONObject(true);
            cloneExt.put("timeRanges", JSON.parseArray(String.format("[[%s,%s]]", trimZero(rangeStart), trimZero(rangeEnd))));
            extParam.put("cloneExt", cloneExt);
        }
        return extParam;
    }

    private JSONObject buildVoiceProfile(String voiceName, String gender, String age, String audioLang, String description) {
        JSONObject profile = new JSONObject(true);
        profile.put("Name", voiceName);
        if (StringUtils.isNotBlank(gender)) {
            profile.put("Gender", gender);
        }
        if (StringUtils.isNotBlank(age)) {
            profile.put("Age", age);
        }
        profile.put("Languages", JSON.parseArray("[\"" + audioLang + "\"]"));
        if (StringUtils.isNotBlank(description)) {
            profile.put("Description", description);
        }
        return profile;
    }

    private JSONObject doTencentRequest(JSONObject account, JSONObject bodyJson) throws Exception {
        String endpoint = config(account, "endpoint", DEFAULT_ENDPOINT);
        String action = config(account, "action", DEFAULT_ACTION);
        String version = config(account, "version", DEFAULT_VERSION);
        String body = bodyJson.toJSONString();
        long timestamp = System.currentTimeMillis() / 1000L;

        Request.Builder builder = new Request.Builder()
                .url("https://" + endpoint + "/")
                .post(RequestBody.create(JSON_TYPE, body))
                .addHeader("Content-Type", "application/json; charset=utf-8")
                .addHeader("Host", endpoint)
                .addHeader("Authorization", buildAuthorization(account, body, timestamp, endpoint, action))
                .addHeader("X-TC-Action", action)
                .addHeader("X-TC-Version", version)
                .addHeader("X-TC-Timestamp", String.valueOf(timestamp))
                .addHeader("X-TC-Language", "zh-CN");

        boolean verifyPeer = configBool(account, "verify-peer", false);
        OkHttpClient client = verifyPeer ? HTTP_CLIENT : HTTP_CLIENT.newBuilder()
                .hostnameVerifier((hostname, session) -> true)
                .build();

        try (Response response = client.newCall(builder.build()).execute()) {
            String responseBody = response.body() == null ? "" : response.body().string();
            log.info("txvoiceclone request={}, status={}, body={}", body, response.code(), responseBody);
            if (!response.isSuccessful()) {
                throw new IOException("HTTP " + response.code() + ": " + responseBody);
            }
            JSONObject json = JSON.parseObject(responseBody);
            JSONObject rsp = json.getJSONObject("Response");
            if (rsp == null) {
                throw new IOException("腾讯云返回结构异常: " + responseBody);
            }
            if (rsp.containsKey("Error")) {
                throw new IOException("腾讯云接口错误: " + rsp.getJSONObject("Error").toJSONString());
            }
            Integer errorCode = rsp.getInteger("ErrorCode");
            if (errorCode != null && errorCode != 0) {
                throw new IOException("腾讯云业务错误 code=" + errorCode + ", msg=" + rsp.getString("Msg") + ", body=" + responseBody);
            }
            return json;
        }
    }

    private CcTtsAliyun saveCloneVoice(String inputVoiceName, String voiceId, String audioLang) {
        String voiceName = buildStoredVoiceName(inputVoiceName, voiceId);
        String languageCode = toLanguageCode(audioLang);
        String languageName = toLanguageName(audioLang);

        CcTtsAliyun speaker = ttsAliyunService.selectCcTtsAliyunByVoiceCode(voiceId);
        boolean update = speaker != null;
        if (!update) {
            speaker = new CcTtsAliyun();
        }
        speaker.setVoiceName(voiceName);
        speaker.setVoiceCode(voiceId);
        speaker.setVoiceEnabled(1);
        speaker.setVoiceSource("tx_tts1");
        speaker.setPriority(0);
        speaker.setProvider("tx_tts1");
        speaker.setLanguageCode(languageCode);
        speaker.setLanguageName(languageName);
        speaker.setTtsModels("clone");

        if (update) {
            ttsAliyunService.updateCcTtsAliyun(speaker);
        } else {
            ttsAliyunService.insertCcTtsAliyun(speaker);
        }
        return speaker;
    }

    private String buildStoredVoiceName(String inputVoiceName, String voiceId) {
        String name = trimToEmpty(inputVoiceName).replace("'", "").replace("\"", "");
        if (name.length() > 30) {
            name = name.substring(0, 30);
        }
        String shortId = voiceId.length() > 12 ? voiceId.substring(0, 12) : voiceId;
        return "腾讯克隆-" + name + "-" + shortId;
    }

    private String trimToEmpty(String value) {
        return value == null ? "" : value.trim();
    }

    private String config(JSONObject obj, String key, String defaultValue) {
        if (obj == null) {
            return defaultValue;
        }
        String value = trimToEmpty(obj.getString(key));
        return StringUtils.isBlank(value) ? defaultValue : value;
    }

    private int configInt(JSONObject obj, String key, int defaultValue) {
        try {
            return Integer.parseInt(config(obj, key, String.valueOf(defaultValue)));
        } catch (Exception e) {
            return defaultValue;
        }
    }

    private boolean configBool(JSONObject obj, String key, boolean defaultValue) {
        String value = config(obj, key, String.valueOf(defaultValue));
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return "1".equals(value) || "true".equalsIgnoreCase(value) || "yes".equalsIgnoreCase(value);
    }

    private String normalizeLanguage(String value) {
        if (StringUtils.isBlank(value)) {
            return DEFAULT_TEXT_LANG;
        }
        String lang = value.trim().replace('_', '-');
        if ("zh-CN".equalsIgnoreCase(lang) || "zh".equalsIgnoreCase(lang)) return "zh";
        if ("en-US".equalsIgnoreCase(lang) || "en".equalsIgnoreCase(lang)) return "en";
        if ("ja-JP".equalsIgnoreCase(lang) || "ja".equalsIgnoreCase(lang)) return "ja";
        if ("ko-KR".equalsIgnoreCase(lang) || "ko".equalsIgnoreCase(lang)) return "ko";
        if ("yue-HK".equalsIgnoreCase(lang) || "yue".equalsIgnoreCase(lang)) return "yue";
        int idx = lang.indexOf('-');
        return idx > 0 ? lang.substring(0, idx) : lang;
    }

    private String normalizeGender(String value) {
        String input = trimToEmpty(value).toLowerCase();
        if ("male".equals(input) || "男".equals(input) || "man".equals(input)) {
            return "male";
        }
        if ("female".equals(input) || "女".equals(input) || "woman".equals(input)) {
            return "female";
        }
        return "";
    }

    private String normalizeAge(String value) {
        String input = trimToEmpty(value).toLowerCase();
        if ("child".equals(input) || "儿童".equals(input)) return "child";
        if ("youth".equals(input) || "青年".equals(input)) return "youth";
        if ("middle".equals(input) || "中年".equals(input) || "middleaged".equals(input)) return "middle";
        if ("old".equals(input) || "老年".equals(input) || "senior".equals(input)) return "old";
        return "";
    }

    private String toLanguageCode(String apiLang) {
        if ("en".equalsIgnoreCase(apiLang)) return "en-US";
        if ("ja".equalsIgnoreCase(apiLang)) return "ja-JP";
        if ("ko".equalsIgnoreCase(apiLang)) return "ko-KR";
        if ("yue".equalsIgnoreCase(apiLang)) return "yue-HK";
        return "zh-CN";
    }

    private String toLanguageName(String apiLang) {
        if ("en".equalsIgnoreCase(apiLang)) return "英文";
        if ("ja".equalsIgnoreCase(apiLang)) return "日语";
        if ("ko".equalsIgnoreCase(apiLang)) return "韩语";
        if ("yue".equalsIgnoreCase(apiLang)) return "粤语";
        return "中文";
    }

    private Float parseFloat(String value) {
        try {
            String trimmed = trimToEmpty(value);
            return StringUtils.isBlank(trimmed) ? null : Float.parseFloat(trimmed);
        } catch (Exception e) {
            return null;
        }
    }

    private String trimZero(Float value) {
        if (value == null) {
            return "";
        }
        if (Math.abs(value - value.intValue()) < 0.0001) {
            return String.valueOf(value.intValue());
        }
        return String.valueOf(value);
    }

    private String buildAuthorization(JSONObject account, String body, long timestamp, String endpoint, String action) throws Exception {
        String secretId = config(account, "secret-id", "");
        String secretKey = config(account, "secret-key", "");
        String date = DATE_FORMATTER.format(Instant.ofEpochSecond(timestamp));
        String canonicalHeaders = "content-type:application/json; charset=utf-8\n" +
                "host:" + endpoint + "\n" +
                "x-tc-action:" + action.toLowerCase() + "\n";
        String signedHeaders = "content-type;host;x-tc-action";
        String canonicalRequest = "POST\n/\n\n" +
                canonicalHeaders + "\n" +
                signedHeaders + "\n" +
                sha256Hex(body);
        String credentialScope = date + "/mps/tc3_request";
        String stringToSign = "TC3-HMAC-SHA256\n" +
                timestamp + "\n" +
                credentialScope + "\n" +
                sha256Hex(canonicalRequest);

        byte[] kDate = hmacSha256("TC3" + secretKey, date);
        byte[] kService = hmacSha256(kDate, "mps");
        byte[] kSigning = hmacSha256(kService, "tc3_request");
        String signature = bytesToHex(hmacSha256(kSigning, stringToSign));
        return "TC3-HMAC-SHA256 Credential=" + secretId + "/" + credentialScope +
                ", SignedHeaders=" + signedHeaders +
                ", Signature=" + signature;
    }

    private String sha256Hex(String value) throws Exception {
        MessageDigest digest = MessageDigest.getInstance("SHA-256");
        return bytesToHex(digest.digest(value.getBytes(StandardCharsets.UTF_8)));
    }

    private byte[] hmacSha256(String key, String message) throws Exception {
        return hmacSha256(key.getBytes(StandardCharsets.UTF_8), message);
    }

    private byte[] hmacSha256(byte[] key, String message) throws Exception {
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(key, "HmacSHA256"));
        return mac.doFinal(message.getBytes(StandardCharsets.UTF_8));
    }

    private String bytesToHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }
}
