package com.cmvr.test.flow.runtime.operator.edge;

import com.alibaba.fastjson2.JSONObject;
import com.cmvr.common.exception.GlobalException;
import com.cmvr.edge.client.service.EdgeSpeakerService;
import com.cmvr.test.enums.ActionEnum;
import com.cmvr.test.flow.runtime.message.TaskNodeExecuteMessage;
import com.cmvr.test.flow.runtime.message.TaskNodeExecuteResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class EdgeSpeakerOperateService implements EdgeOperateService {

    private final EdgeSpeakerService edgeSpeakerService;

    @Override
    public boolean supports(ActionEnum action) {
        return action.name().startsWith("SPEAKER_");
    }

    @Override
    public TaskNodeExecuteResult execute(TaskNodeExecuteMessage message) {
//        ActionEnum action = message.getAction();
//        JSONObject inputParams = message.getInputParams();
//        String deviceId = inputParams.getString("deviceId");
//        String terminalId = message.getTerminalId();
//        switch (action) {
//            case SPEAKER_PLAYAUDIO: {
//                log.info("speaker play audio");
//                // todo 音频暂时随机播放
//                List<String> all = edgeSpeakerService.getAll();

//                for (int i = 0; i < 3; i++) {
//                    int index = new Random().nextInt(all.size());
//                    String audioUrl = "/home/share/assets/upload/" + all.get(index);
//                    edgeSpeakerService.playAudio(terminalId, deviceId, audioUrl);
//                    // 等待播放完成
//                    while (true) {
//                        try {
//                            Thread.sleep(200);
//                        } catch (InterruptedException e) {
//                            throw new RuntimeException(e);
//                        }
//                        SpeakerCommand.GetSpeakerStateCommand.Feedback status = edgeSpeakerService.getStatus(terminalId, deviceId);
//                        boolean isRunning = status.getState().getIsRunning();
//                        if (!isRunning) {
//                            break;
//                        }
//                    }
//                    try {
//                        Thread.sleep(500);
//                    } catch (InterruptedException e) {
//                        throw new RuntimeException(e);
//                    }
//                }
//                break;
//            }
//
//            default:
//                throw new GlobalException("不支持的扬声器操作类型: " + action);
//        }
//        return TaskNodeExecuteResult.success();
        return null;
    }
}
