package com.multictrl.modules.business.controller;

import com.multictrl.common.annotation.ApiOrder;
import com.multictrl.common.annotation.LogOperation;
import com.multictrl.common.utils.Result;
import com.multictrl.common.validator.ValidatorUtils;
import com.multictrl.modules.business.dto.speaker.TtsText;
import com.multictrl.modules.business.service.PsdkService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

/**
 * psdk负载控制
 *
 * @author Sdy
 * @since 1.0.0 2026/5/7
 */
@RestController
@RequestMapping("business/psdk")
@Tag(name = "psdk负载控制")
@ApiOrder(9)
@RequiredArgsConstructor
public class PsdkController {
    private final PsdkService psdkService;

    @LogOperation("设置音量")
    @PostMapping("/setVolume/{dockSn}")
    @Operation(summary = "设置音量")
    @RequiresPermissions("bus:speaker:setVolume")
    public Result<Object> setVolume(@PathVariable String dockSn,
                                    @Parameter(name = "volume", description = "音量 0-100")
                                    @RequestParam Integer volume) {

        return new Result<>().ok(psdkService.setVolume(dockSn, volume));
    }

    @LogOperation("设置播放模式")
    @PostMapping("/setMode/{dockSn}")
    @Operation(summary = "设置播放模式")
    @RequiresPermissions("bus:speaker:setMode")
    public Result<Object> setMode(@PathVariable String dockSn,
                                  @Parameter(name = "mode", description = "模式 0：单次播放 1：循环播放（单曲）")
                                  @RequestParam Integer mode) {

        return new Result<>().ok(psdkService.setMode(dockSn, mode));
    }

    @LogOperation("停止播放")
    @PostMapping("/stopSpeaker/{dockSn}")
    @Operation(summary = "停止播放")
    @RequiresPermissions("bus:speaker:stopSpeaker")
    public Result<Object> stopSpeaker(@PathVariable String dockSn) {

        return new Result<>().ok(psdkService.stopSpeaker(dockSn));
    }

    @LogOperation("重新播放")
    @PostMapping("/replaySpeaker/{dockSn}")
    @Operation(summary = "重新播放")
    @RequiresPermissions("bus:speaker:replaySpeaker")
    public Result<Object> replaySpeaker(@PathVariable String dockSn) {

        return new Result<>().ok(psdkService.replaySpeaker(dockSn));
    }

    @LogOperation("播放TTS文本")
    @PostMapping("/startSpeakerTts/{dockSn}")
    @Operation(summary = "播放TTS文本")
    @RequiresPermissions("bus:speaker:startSpeakerTts")
    public Result<Object> startSpeakerTts(@PathVariable String dockSn, @RequestBody TtsText ttsText) {
        ValidatorUtils.validateEntity(ttsText);

        return new Result<>().ok(psdkService.startSpeakerTts(dockSn, ttsText));
    }

    @LogOperation("播放模板文本")
    @PostMapping("/startSpeakerTemplate/{dockSn}")
    @Operation(summary = "播放模板文本")
    @RequiresPermissions("bus:speaker:startSpeakerTemplate")
    public Result<Object> startSpeakerTemplate(@PathVariable String dockSn,
                                               @Parameter(name = "id", description = "模版编号")
                                               @RequestParam Long id) {

        return new Result<>().ok(psdkService.startSpeakerTemplate(dockSn, id));
    }

    @LogOperation("播放音频文件")
    @PostMapping("/playAudio/{dockSn}")
    @Operation(summary = "播放音频文件")
    @RequiresPermissions("bus:speaker:playAudio")
    public Result<Object> playAudio(@PathVariable String dockSn,
                                    @Parameter(name = "id", description = "模版编号")
                                    @RequestParam Long id) {

        return new Result<>().ok(psdkService.playAudio(dockSn, id));
    }
}
