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

import com.alibaba.fastjson2.JSONObject;
import com.cmvr.common.exception.GlobalException;
import com.cmvr.edge.client.model.EdgeCommonVO;
import com.cmvr.edge.client.service.EdgeBioHeadService;
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 EdgeHeadOperateService implements EdgeOperateService {

    private final EdgeBioHeadService edgeBioHeadService;

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

    @Override
    public TaskNodeExecuteResult execute(TaskNodeExecuteMessage message) {
        ActionEnum action = message.getAction();
        JSONObject inputParams = message.getInputParams();
        String terminalId = message.getTerminalId();
        String deviceId = inputParams.getString("deviceId");

        switch (action) {
            case BIO_HEAD_SPEAK_START: {
                EdgeCommonVO edgeCommonVO = new EdgeCommonVO();
                edgeCommonVO.setDeviceId(deviceId);
                edgeCommonVO.setTerminalId(terminalId);
                edgeBioHeadService.speakStart(edgeCommonVO);
                break;
            }
            case BIO_HEAD_SPEAK_STOP: {
                EdgeCommonVO edgeCommonVO = new EdgeCommonVO();
                edgeCommonVO.setDeviceId(deviceId);
                edgeCommonVO.setTerminalId(terminalId);
                edgeBioHeadService.speakStop(edgeCommonVO);
                break;
            }
            case BIO_HEAD_SPECIAL_EXPRESSION: {
                Integer expressKey = inputParams.getInteger("expressKey");
                edgeBioHeadService.specialExpression(terminalId,deviceId,expressKey);
                break;
            }

            default:
                throw new GlobalException("不支持的头部操作类型: " + action);
        }
        return TaskNodeExecuteResult.success();
    }

}
