package com.multictrl.modules.business.controller;

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

import java.util.Map;

/**
 * 喊话器控制 大疆官方AS1喊话器
 *
 * @author Sdy
 * @since 1.0.0 2026/5/6
 */
@RestController
@RequestMapping("business/speaker")
@Tag(name = "喊话器控制")
@ApiOrder(8)
@RequiredArgsConstructor
public class SpeakerController {
    private final SpeakerService speakerService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数"),
            @Parameter(name = "type", description = "类型 1:文本 2:音频"),
            @Parameter(name = "dockSn", description = "机库SN码")
    })
    @RequiresPermissions("bus:speaker:page")
    public Result<PageData<SpeakerDTO>> page(@Parameter(hidden = true) @RequestParam Map<String, Object> params) {
        PageData<SpeakerDTO> page = speakerService.page(params);

        return new Result<PageData<SpeakerDTO>>().ok(page);
    }

    @LogOperation("添加喊话模版")
    @PostMapping("/addTemplate")
    @Operation(summary = "添加喊话模版")
    @RequiresPermissions("bus:speaker:addTemplate")
    public Result<Object> addTemplate(@RequestBody SpeakerDTO speaker) {
        ValidatorUtils.validateEntity(speaker);
        speakerService.addTemplate(speaker);

        return new Result<>();
    }

    @LogOperation("删除模版")
    @DeleteMapping("/deleteTemplate")
    @Operation(summary = "删除模版")
    @RequiresPermissions("bus:speaker:deleteTemplate")
    public Result<Object> deleteTemplate(@Parameter(name = "id", description = "模版编号")
                                         @RequestParam Long id) {
        speakerService.deleteTemplate(id);

        return new Result<>();
    }

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

        return new Result<>().ok(speakerService.drcSetVolume(dockSn, volume));
    }

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

        return new Result<>().ok(speakerService.drcSetMode(dockSn, mode));
    }

    @LogOperation("喊话设置")
    @PostMapping("/drcSetTts/{dockSn}")
    @Operation(summary = "喊话设置")
    @RequiresPermissions("bus:speaker:drcSetTts")
    public Result<Object> drcSetTts(@PathVariable String dockSn, @RequestBody SpeakerSet ttsSpeaker) {
        ValidatorUtils.validateEntity(ttsSpeaker);

        return new Result<>().ok(speakerService.drcSetTts(dockSn, ttsSpeaker));
    }

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

        return new Result<>().ok(speakerService.drcStopSpeaker(dockSn));
    }

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

        return new Result<>().ok(speakerService.drcReplaySpeaker(dockSn));
    }

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

        return new Result<>().ok(speakerService.drcStartSpeakerTts(dockSn, ttsText));
    }

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

        return new Result<>().ok(speakerService.drcStartSpeakerTemplate(dockSn, id));
    }
}
