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

import cn.hutool.json.JSONObject;
import com.multictrl.common.config.MinioConfig;
import com.multictrl.common.config.SysConfig;
import com.multictrl.common.constant.BusinessConstant;
import com.multictrl.common.exception.ErrorCode;
import com.multictrl.common.exception.RenException;
import com.multictrl.common.utils.CacheUtils;
import com.multictrl.common.utils.Utils;
import com.multictrl.modules.business.dto.SpeakerDTO;
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.PsdkService;
import com.multictrl.modules.business.service.SpeakerService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * psdk负载控制
 *
 * @author Sdy
 * @since 1.0.0 2026/5/8
 */
@Service
@RequiredArgsConstructor
public class PsdkServiceImpl implements PsdkService {
    private final DJIBaseService djiBaseService;
    private final SpeakerService speakerService;
    private final SysConfig sysConfig;

    @Override
    public String setVolume(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.executeAndReturnResult(dockSn, "speaker_play_volume_set", data);
    }

    @Override
    public String setMode(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.executeAndReturnResult(dockSn, "speaker_play_mode_set", data);
    }

    @Override
    public String stopSpeaker(String dockSn) {
        JSONObject data = getDataIncludePsdkIndex(dockSn);
        return djiBaseService.executeAndReturnResult(dockSn, "speaker_play_stop", data);
    }

    @Override
    public String replaySpeaker(String dockSn) {
        JSONObject data = getDataIncludePsdkIndex(dockSn);
        return djiBaseService.executeAndReturnResult(dockSn, "speaker_replay", data);
    }

    @Override
    public String startSpeakerTts(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.executeAndReturnResult(dockSn, "speaker_tts_play_start", data);
    }

    @Override
    public String startSpeakerTemplate(String dockSn, Long id) {
        SpeakerEntity speakerEntity = speakerService.selectById(id);
        if (speakerEntity == null || speakerEntity.getType() != 1) {
            throw new RenException(ErrorCode.PARAMS_ERROR);
        }
        TtsText ttsText = new TtsText();
        ttsText.setName(speakerEntity.getName());
        ttsText.setText(speakerEntity.getTextContent());
        return startSpeakerTts(dockSn, ttsText);
    }

    @Override
    public String playAudio(String dockSn, Long id) {
        SpeakerEntity speaker = speakerService.getDao().selectById(id);
        if (speaker == null || speaker.getType() != 2) {
            throw new RenException(ErrorCode.PARAMS_ERROR);
        }
        JSONObject data = getDataIncludePsdkIndex(dockSn);
        JSONObject file = new JSONObject();
        file.set("format", "pcm");
        file.set("name", speaker.getName());
        file.set("md5", speaker.getMd5());
        String protocol = BusinessConstant.HTTP_PROTOCOL;
        if (sysConfig.getIsSsl()) {
            protocol = BusinessConstant.HTTPS_PROTOCOL;
        }
        file.set("url", protocol + sysConfig.getIp() + ":" + sysConfig.getPort() + "/" + BusinessConstant.FILE_PATH + speaker.getPcmPath());
        data.set("file", file);
        return djiBaseService.executeAndReturnResult(dockSn, "speaker_audio_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;
    }
}
