package com.cmvr.web.controller.show;

import cn.hutool.core.util.StrUtil;
import com.cmvr.common.annotation.Log;
import com.cmvr.common.core.controller.BaseController;
import com.cmvr.common.core.domain.AjaxResult;
import com.cmvr.common.enums.BusinessType;
import com.cmvr.llm.model.CallAgentVO;
import com.cmvr.llm.model.CallAgentAdvancedVO;
import com.cmvr.llm.model.CallTTSAdvancedVO;
import com.cmvr.llm.model.CallTTSVO;
import com.cmvr.llm.service.ShowIntelligentCockpitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;

@Api(tags = "演示--智能座舱")
@RestController
@RequestMapping("/show")
@RequiredArgsConstructor
public class ShowIntelligentCockpitController extends BaseController {

    private final ShowIntelligentCockpitService showIntelligentCockpitService;

    // ----------------------------- 语音合成 -----------------------------

    @Log(title = "演示-智能座舱-创建音色", businessType = BusinessType.OTHER)
    @ApiOperation("创建音色")
    @PostMapping("/tts/create")
    public AjaxResult create(@RequestPart(value = "file") MultipartFile file) throws IOException {
        return success(showIntelligentCockpitService.createTimbre(file));
    }

    @ApiOperation("查询音色")
    @GetMapping("/tts/query")
    public AjaxResult query() {
        return success(showIntelligentCockpitService.queryTimbre());
    }

    @Log(title = "演示-智能座舱-生成语料", businessType = BusinessType.OTHER)
    @ApiOperation("生成语料")
    @PostMapping("/tts/corpus")
    public AjaxResult corpus(@Valid @RequestBody CallAgentVO callAgentVO) {
        callAgentVO.setConvertWords(StrUtil.trim(callAgentVO.getConvertWords()));
        return success(showIntelligentCockpitService.genCorpus(callAgentVO));
    }

    @Log(title = "演示-智能座舱-语音合成", businessType = BusinessType.OTHER)
    @ApiOperation("语音合成")
    @PostMapping("/tts/synthesize")
    public ResponseEntity<byte[]> synthesize(@Valid @RequestBody CallTTSVO callTTSVO) {
        callTTSVO.setText(StrUtil.trim(callTTSVO.getText()));
        return showIntelligentCockpitService.synthesizeAudio(callTTSVO);
    }

    @Log(title = "演示-智能座舱-生成语料-增强", businessType = BusinessType.OTHER)
    @ApiOperation("生成语料-增强")
    @PostMapping("/tts/corpus/advanced")
    public AjaxResult corpusAdvanced(@Valid @RequestBody CallAgentAdvancedVO callAgentAdvancedVO) {
        callAgentAdvancedVO.setConvertWords(StrUtil.trim(callAgentAdvancedVO.getConvertWords()));
        return success(showIntelligentCockpitService.genCorpusAdvanced(callAgentAdvancedVO));
    }

    @Log(title = "演示-智能座舱-语音合成-增强", businessType = BusinessType.OTHER)
    @ApiOperation("语音合成-增强")
    @PostMapping("/tts/synthesize/advanced")
    public ResponseEntity<byte[]> synthesizeAdvanced(@Valid @RequestBody CallTTSAdvancedVO callTTSAdvancedVO) {
        callTTSAdvancedVO.setText(StrUtil.trim(callTTSAdvancedVO.getText()));
        return showIntelligentCockpitService.synthesizeAudioAdvanced(callTTSAdvancedVO);
    }

    // ----------------------------- 触控交互 -----------------------------

    @Log(title = "演示-智能座舱-触控坐标", businessType = BusinessType.OTHER)
    @ApiOperation("触控坐标")
    @PostMapping("/touch/coordinates")
    public AjaxResult coordinates(
            @RequestPart(value = "image") MultipartFile image,
            @RequestParam("words") String words
    ) throws Exception {
        return success(showIntelligentCockpitService.getCoordinates(image.getBytes(), words));
    }
}
