package com.multictrl.modules.business.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.multictrl.common.constant.BusinessConstant;
import com.multictrl.common.exception.ErrorCode;
import com.multictrl.common.exception.RenException;
import com.multictrl.common.service.impl.CrudServiceImpl;
import com.multictrl.common.utils.CacheUtils;
import com.multictrl.common.utils.Utils;
import com.multictrl.modules.business.dao.SpeakerDao;
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.entity.SpeakerEntity;
import com.multictrl.modules.business.service.DJIBaseService;
import com.multictrl.modules.business.service.SpeakerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 喊话器控制
 *
 * @author Sdy
 * @since 1.0.0 2026/5/6
 */
@Service
@RequiredArgsConstructor
public class SpeakerServiceImpl extends CrudServiceImpl<SpeakerDao, SpeakerEntity, SpeakerDTO> implements SpeakerService {
    private final DJIBaseService djiBaseService;

    @Override
    public QueryWrapper<SpeakerEntity> getWrapper(Map<String, Object> params) {
        return null;
    }

    @Override
    public String drcSetVolume(String dockSn, Integer volume) {
        if (volume == null || volume < 0 || volume > 100) {
            throw new RenException(ErrorCode.PARAMS_ERROR);
        }
        JSONObject data = getDataIncludePsdkIndex(dockSn);
        data.set("play_volume", volume);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_speaker_play_volume_set", data);
    }

    @Override
    public String drcSetMode(String dockSn, Integer mode) {
        if (mode == null || (mode != 0 && mode != 1)) {
            throw new RenException(ErrorCode.PARAMS_ERROR);
        }
        JSONObject data = getDataIncludePsdkIndex(dockSn);
        data.set("play_mode", mode);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_speaker_play_mode_set", data);
    }

    @Override
    public String drcSetTts(String dockSn, SpeakerSet ttsSpeaker) {
        JSONObject data = getDataIncludePsdkIndex(dockSn);
        data.set("volume", ttsSpeaker.getVolume());
        data.set("type", ttsSpeaker.getType());
        data.set("language", ttsSpeaker.getLanguage());
        data.set("speed", ttsSpeaker.getSpeed());
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_speaker_tts_set", data);
    }

    @Override
    public String drcStopSpeaker(String dockSn) {
        JSONObject data = getDataIncludePsdkIndex(dockSn);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_speaker_play_stop", data);
    }

    @Override
    public String drcReplaySpeaker(String dockSn) {
        JSONObject data = getDataIncludePsdkIndex(dockSn);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_speaker_replay", data);
    }

    @Override
    public String drcStartSpeakerTts(String dockSn, TtsText ttsText) {
        JSONObject data = getDataIncludePsdkIndex(dockSn);
        JSONObject tts = new JSONObject();
        tts.set("text", ttsText.getText());
        tts.set("name", ttsText.getName());
        tts.set("md5", Utils.md5Txt(ttsText.getText()));
        data.set("tts", tts);
        return djiBaseService.executeDrcAndReturnResult(dockSn, "drc_speaker_tts_play_start", data);
    }

    //获取包含psdk_index的data
    private JSONObject getDataIncludePsdkIndex(String dockSn) {
        JSONObject data = new JSONObject();
        Object speakerIndex = CacheUtils.get(BusinessConstant.UAV_SPEAKER_INDEX + dockSn);
        if (speakerIndex != null) {
            data.set("psdk_index", speakerIndex);
        } else {
            throw new RenException(ErrorCode.UAV_SPEAKER_INDEX_NOT_EXIST);
        }
        return data;
    }
}
