package com.cmvr.test.service;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.cmvr.common.exception.GlobalException;
import com.cmvr.edge.client.model.EdgeCommonVO;
import com.cmvr.edge.client.model.biohead.EdgeFacialExpressionVO;
import com.cmvr.edge.client.model.hlc.EdgeTouchVO;
import com.cmvr.edge.client.service.EdgeBioHeadService;
import com.cmvr.edge.client.service.EdgeCameraService;
import com.cmvr.edge.client.service.EdgeHlcService;
import com.cmvr.edge.client.service.EdgeMicrophoneService;
import com.cmvr.edge.client.service.EdgeSpeakerService;
import com.cmvr.llm.service.LLMAiAgentPlatformService;
import com.cmvr.llm.service.LLMAiTtsService;
import com.cmvr.test.enums.ActionEnum;
import com.cmvr.test.model.vo.FlowActionRequestVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class FlowActionExecutorService {
    private final EdgeCameraService edgeCameraService;
    private final EdgeBioHeadService edgeBioHeadService;
    private final EdgeMicrophoneService edgeMicrophoneService;
    private final EdgeSpeakerService edgeSpeakerService;
    private final EdgeHlcService edgeHlcService;
    private final LLMAiTtsService llmAiTtsService;
    private final LLMAiAgentPlatformService llmAiAgentPlatformService;

    public String actionExecute(FlowActionRequestVO req) {

        ActionEnum action = ActionEnum.fromAction(req.getAction());

        if (ObjUtil.isEmpty(action)) {
            throw new GlobalException("不存在此行为!");
        }

        switch (action.getOperate()) {
            case "EDGE":
                return executeEdgeAction(action, req);

            case "LLM":
                return executeLLMAction(action, req);

            default:
                throw new GlobalException("仅设备和大模型 Action 可调用!");
        }
    }

    private String executeEdgeAction(ActionEnum action, FlowActionRequestVO req) {
        JSONObject payload = req.getPayload();
        String terminalId = payload.getString("terminalId");
        String deviceId = payload.getString("deviceId");

        if (StrUtil.isEmpty(terminalId) || StrUtil.isEmpty(deviceId)) {
            throw new GlobalException("EDGE 类型动作必须提供 terminalId 和 deviceId");
        }

        EdgeCommonVO edgeCommonVO = new EdgeCommonVO();
        edgeCommonVO.setTerminalId(terminalId);
        edgeCommonVO.setDeviceId(deviceId);
        switch (action) {
            // ==== 相机 ====
            case CAMERA_START:
                return edgeCameraService.start(edgeCommonVO);
            case CAMERA_STOP:
                return edgeCameraService.stop(edgeCommonVO);
            case CAMERA_GETRGBIMAGE:
                return edgeCameraService.getRGBImage(edgeCommonVO);
            case CAMERA_RECORDING_START:
                return edgeCameraService.startRecording(edgeCommonVO);
            case CAMERA_RECORDING_STOP:
                return edgeCameraService.stopRecording(edgeCommonVO);

            // ==== 麦克风 ====
            case MICROPHONE_START:
                return edgeMicrophoneService.startRecord(edgeCommonVO);
            case MICROPHONE_STOP:
                return edgeMicrophoneService.stopRecord(edgeCommonVO);

            // ==== 触控 ====
            case TOUCH:
                EdgeTouchVO edgeTouchVO = payload.to(EdgeTouchVO.class);
                return edgeHlcService.touch(edgeTouchVO);

            // ==== 语料 ====
            case VI_PLAY_CORPUS:
                String audioPath = payload.getString("audioPath");
                return edgeSpeakerService.playAudio(terminalId, deviceId, audioPath);

            // ==== 头部 ====
            case BIO_HEAD_SPEAK_START:
                return edgeBioHeadService.speakStart(edgeCommonVO);
            case BIO_HEAD_SPEAK_STOP:
                return edgeBioHeadService.speakStop(edgeCommonVO);
            case BIO_HEAD_SPECIAL_EXPRESSION:
                EdgeFacialExpressionVO edgeFacialExpressionVO = payload.to(EdgeFacialExpressionVO.class);
                edgeFacialExpressionVO.setTerminalId(terminalId);
                edgeFacialExpressionVO.setDeviceId(deviceId);
                return edgeBioHeadService.setExpression(edgeFacialExpressionVO);

            default:
                throw new UnsupportedOperationException("未实现的 EDGE Action: " + action.name());
        }
    }

    private String executeLLMAction(ActionEnum action, FlowActionRequestVO req) {
        log.info("LLM 执行动作: {}", action);

        switch (action) {
            case TOUCH_COORDINATES:
                return "LLM 触控坐标 OK";
            case INTENT_RECOGNITION:
                return "LLM 意图识别 OK";
            case GENERATE_ADVANCED_AUDIO:
                return "LLM 高级音频生成 OK";
            case AI_AGENT_PLATFORM:
                return llmAiAgentPlatformService.query(action.getAction(), req.getPayload().getJSONObject("config").getString("text"), req.getPayload().getJSONObject("config").getString("apiKey"), req.getPayload().getBoolean("invokeTts"), req.getPayload().getJSONObject("tts")).toString();
            default:
                throw new UnsupportedOperationException("未实现的 LLM Action: " + action);
        }
    }
}

