package com.cmvr.web.controller.api;

import cmvr.api.SpeakerCommand;
import com.cmvr.edge.client.service.EdgeSpeakerService;
import com.cmvr.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 扬声器管理
 * 
 * @author cmvr
 */
@Api(tags = "边缘--扬声器")
@RestController
@RequestMapping("/api/Speaker")
@RequiredArgsConstructor
public class EdgeSpeakerController {

    private final EdgeSpeakerService speakerService;

    @ApiOperation("获取扬声器状态")
    @PreAuthorize("@ss.hasPermi('system:speaker:query')")
    @GetMapping("/status")
    public AjaxResult getStatus(
            @ApiParam(value = "终端设备ID", required = true)
            @RequestParam String terminalId,
            @ApiParam(value = "设备ID", required = true)
            @RequestParam String deviceId) {
        try {
            // 获取扬声器状态
            SpeakerCommand.GetSpeakerStateCommand.Feedback feedback = speakerService.getStatus(terminalId, deviceId);
            SpeakerCommand.SpeakerState state = feedback.getState();

            // 构建返回结果
            Map<String, Object> result = new HashMap<>();
            // 添加 header 信息
            Map<String, Object> header = new HashMap<>();
            header.put("deviceId", deviceId);
            header.put("timestamp", feedback.getHeader().getTimestamp().getSeconds());
            result.put("header", header);

            // 添加 state 信息
            Map<String, Object> stateMap = new HashMap<>();
            stateMap.put("isInitialized", state.getIsInitialized());
            stateMap.put("isRunning", state.getIsRunning());
            stateMap.put("isDecoding", state.getIsDecoding());
            stateMap.put("isPaused", state.getIsPaused());
            stateMap.put("volume", state.getVolume());
            stateMap.put("errorMessage", state.getErrorMessage());
            result.put("state", stateMap);

            return AjaxResult.success(result);
        } catch (Exception e) {
            return AjaxResult.error("获取扬声器状态失败：" + e.getMessage());
        }
    }

    /**
     * 播放音频
     */
    @ApiOperation("播放音频")
    @PreAuthorize("@ss.hasPermi('system:speaker:play')")
    @PostMapping("/play")
    public AjaxResult playAudio(
            @ApiParam(value = "终端设备ID", required = true)
            @RequestParam String terminalId,
            @ApiParam(value = "设备ID", required = true)
            @RequestParam String deviceId,
            @ApiParam(value = "音频文件路径", required = true)
            @RequestParam String audioPath) {
        speakerService.playAudio(terminalId, deviceId, audioPath);
        return AjaxResult.success();
    }

    /**
     * 停止播放
     */
    @ApiOperation("停止播放")
    @PreAuthorize("@ss.hasPermi('system:speaker:stop')")
    @PostMapping("/stop")
    public AjaxResult stopPlayback(
            @ApiParam(value = "终端设备ID", required = true)
            @RequestParam String terminalId,
            @ApiParam(value = "设备ID", required = true)
            @RequestParam String deviceId) {
        speakerService.stopPlayback(terminalId, deviceId);
        return AjaxResult.success();
    }

    /**
     * 暂停播放
     */
    @ApiOperation("暂停播放")
    @PreAuthorize("@ss.hasPermi('system:speaker:pause')")
    @PostMapping("/pause")
    public AjaxResult pausePlayback(
            @ApiParam(value = "终端设备ID", required = true)
            @RequestParam String terminalId,
            @ApiParam(value = "设备ID", required = true)
            @RequestParam String deviceId) {
        speakerService.pausePlayback(terminalId, deviceId);
        return AjaxResult.success();
    }

    /**
     * 恢复播放
     */
    @ApiOperation("恢复播放")
    @PreAuthorize("@ss.hasPermi('system:speaker:resume')")
    @PostMapping("/resume")
    public AjaxResult resumePlayback(
            @ApiParam(value = "终端设备ID", required = true)
            @RequestParam String terminalId,
            @ApiParam(value = "设备ID", required = true)
            @RequestParam String deviceId) {
        speakerService.resumePlayback(terminalId, deviceId);
        return AjaxResult.success();
    }

    /**
     * 设置音量
     */
    @ApiOperation("设置音量")
    @PreAuthorize("@ss.hasPermi('system:speaker:volume')")
    @PostMapping("/volume")
    public AjaxResult setVolume(
            @ApiParam(value = "终端设备ID", required = true)
            @RequestParam String terminalId,
            @ApiParam(value = "设备ID", required = true)
            @RequestParam String deviceId,
            @ApiParam(value = "音量大小（0-100）", required = true)
            @RequestParam int volume) {
        speakerService.setVolume(terminalId, deviceId, volume);
        return AjaxResult.success();
    }

    /**
     * 获取音量
     */
    @ApiOperation("获取音量")
    @PreAuthorize("@ss.hasPermi('system:speaker:volume')")
    @GetMapping("/volume")
    public AjaxResult getVolume(
            @ApiParam(value = "终端设备ID", required = true)
            @RequestParam String terminalId,
            @ApiParam(value = "设备ID", required = true)
            @RequestParam String deviceId) {
        return AjaxResult.success(speakerService.getVolume(terminalId, deviceId));
    }

    @ApiOperation("获取所有音频")
    @PreAuthorize("@ss.hasPermi('system:speaker:getAll')")
    @GetMapping("/getAll")
    public AjaxResult getAll() {
        return AjaxResult.success(speakerService.getAll());
    }
}
