package com.sonic.cow.controller.mock;

import com.mashape.unirest.http.exceptions.UnirestException;
import com.sonic.common.auth.domains.Session;
import com.sonic.common.rpc.Result;
import com.sonic.cow.domain.input.TtsDeAmountInput;
import com.sonic.cow.domain.input.VoiceAsrInput;
import com.sonic.cow.domain.input.VoiceTtsV2Input;
import com.sonic.cow.domain.output.AsrOutput;
import com.sonic.cow.enums.ToastResultCode;
import com.sonic.cow.limit.RequestLimit;
import com.sonic.cow.service.VoiceService;
import com.sonic.cow.service.VoiceV2Service;
import com.sonic.cow.utils.Base64Utils;
import com.sonic.sdk.api.annotation.IgnoreAuth;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;

/**
 * Web-语音处理
 */
@RestController
@Slf4j
public class VoiceMockController {

    @Autowired
    private VoiceService voiceService;
    @Autowired
    private VoiceV2Service voiceV2Service;

    @IgnoreAuth
    @RequestLimit(count = 100, time = 86400000)
    @ApiOperation(value = "语音转文本V2版本", tags = {"Web-语音处理"})
    @PostMapping(value = "/mock/voice/asr-v2")
    public Result<AsrOutput> asrV2(@RequestBody VoiceAsrInput input, @ApiParam(hidden = true) Session session, @ApiParam(hidden = true) HttpServletRequest request) throws UnirestException, InterruptedException {
        session.setUserId(session.getUserId() == null ? -1L : session.getUserId());
        return Result.success(voiceService.asr(session.getUserId(), input));
    }

    @IgnoreAuth
    @RequestLimit(count = 100, time = 86400000)
    @ApiOperation(value = "语音转文本V3版本", tags = {"Web-语音处理"})
    @PostMapping(value = "/mock/voice/asr-v3")
    public Result<AsrOutput> asrV3(@RequestBody VoiceAsrInput input, @ApiParam(hidden = true) Session session, @ApiParam(hidden = true) HttpServletRequest request) throws UnirestException {
        //保存到临时 File
        File tempFile = Base64Utils.decodeToTempFile(input.getData(), UUID.randomUUID() + ".mp3");
        return Result.success(voiceV2Service.asr(session.getUserId(), input.getAiId(), tempFile));
    }

    @IgnoreAuth
    @RequestLimit(count = 1000, time = 86400000)
    @ApiOperation(value = "生成语音V2版本", tags = {"Web-语音处理"})
    @PostMapping(value = "/mock/voice/tts-v2")
    public Result<String> ttsV2(@RequestBody VoiceTtsV2Input input, @ApiParam(hidden = true) Session session, @ApiParam(hidden = true) HttpServletRequest request) {
        session.setUserId(session.getUserId() == null ? -1L : session.getUserId());
        return Result.success(voiceService.tts(session.getUserId(), input));
    }

    @IgnoreAuth
    @RequestLimit(count = 1000, time = 86400000)
    @ApiOperation(value = "生成语音扣费", tags = {"Web-语音处理"})
    @PostMapping(value = "/mock/voice/tts-de-amount")
    public Result<String> ttsDeAmount(@RequestBody TtsDeAmountInput input, @ApiParam(hidden = true) HttpServletRequest request) {
        //验证密钥是否正确
        ToastResultCode.SYS_SYSTEM_EXCEPTION.check(!input.getSk().equals("kILi*&u878u*(H778GB6FVYB0@#9jm79ygvbnj%$Rtb*&Gob"));
        voiceService.ttsDeAmount(input.getUserId(), input.getAiId());
        return Result.success();
    }
}
