package com.fuyuanshen.web.controller.device;

import com.fuyuanshen.app.domain.dto.AppAudioFileDto;
import com.fuyuanshen.app.domain.dto.AppFileRenameDto;
import com.fuyuanshen.app.domain.dto.TextToSpeechRequest;
import com.fuyuanshen.app.service.AudioProcessService;
import com.fuyuanshen.app.service.VideoProcessService;
import com.fuyuanshen.common.core.domain.R;
import com.fuyuanshen.common.idempotent.annotation.RepeatSubmit;
import com.fuyuanshen.common.web.core.BaseController;
import com.fuyuanshen.equipment.domain.vo.AppFileVo;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * APP 视频处理控制器
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/video")
public class WebVideoController extends BaseController {

    private final VideoProcessService videoProcessService;
    private final AudioProcessService audioProcessService;

    /**
     * 上传视频转码code默认1：RGB565 2：BGR565
     */
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS,message = "请勿重复提交！")
    public R<List<String>> uploadVideo(@RequestParam("file") MultipartFile file, @RequestParam(defaultValue = "1") int code) {
        return R.ok(videoProcessService.processVideo(file, code));
    }

    /**
     * 上传音频文件并转码
     */
    @PostMapping(value = "/audio", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS,message = "请勿重复提交！")
    public R<List<String>> uploadAudio(@RequestParam("file") MultipartFile file) {
        return R.ok(audioProcessService.processAudio(file));
    }

    /**
     * 文字转音频TTS服务
     */
    @GetMapping("/audioTTS")
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS,message = "请勿重复提交！")
    public R<List<String>> uploadAudioTTS(@RequestParam String text) throws IOException {
        return R.ok(audioProcessService.generateStandardPcmData(text));
    }


    /**
     * 提取文本内容（只支持txt/docx）
     */
    @PostMapping(value = "/extract", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS,message = "请勿重复提交！")
    public R<String> extract(@RequestParam("file") MultipartFile file) throws Exception {
        return R.ok("Success",audioProcessService.extract(file));
    }


    /**
     * 上传音频文件进行处理
     * 支持MP3、WAV、PCM格式
     */
    @PostMapping("/uploadAudioToOss")
    public R<String> uploadAudioToOss(@ModelAttribute AppAudioFileDto bo) {
        try {
            bo.setSource("web");
            String result = audioProcessService.uploadAudioToOss(bo);
            return R.ok(result);
        } catch (IllegalArgumentException e) {
            return R.fail("文件格式错误: " + e.getMessage());
        } catch (Exception e) {

            return R.fail("上传处理失败: " + e.getMessage());
        }
    }

    /**
     * 文本转语音
     * 支持MP3、WAV、PCM格式
     */
    @PostMapping("/ttsToOss")
    public R<String> textToSpeech(@RequestBody TextToSpeechRequest request) {
        try {
            if (request.getDeviceId() == null) {
                return R.fail("设备ID不能为空");
            }

            String result = audioProcessService.textToSpeech(
                    request.getDeviceId(),
                    request.getText(),
                    request.getFileSuffix(),
                    "web"
            );
            return R.ok(result);
        } catch (Exception e) {
            return R.fail("文本转语音失败: " + e.getMessage());
        }
    }


    /**
     * 查询语音文件列表
     */
    @GetMapping("/queryAudioFileList")
    public R<List<AppFileVo>> queryAudioFileList(Long deviceId) {
        return R.ok(audioProcessService.queryAudioFileList(deviceId,"web"));
    }

    /**
     * 删除语音文件
     */
    @GetMapping("/deleteAudioFile")
    public R<Void> deleteAudioFile(Long id) {
        return audioProcessService.deleteAudioFile(id);
    }

    /**
     * 文件重命名
     */
    @PostMapping("/renameAudioFile")
    public R<Void> renameAudioFile(@RequestBody AppFileRenameDto bo) {
        return audioProcessService.renameAudioFile(bo);
    }
}
