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

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.cmvr.common.exception.GlobalException;
import com.cmvr.edge.client.model.humanoid.EdgeMoveJVO;
import com.cmvr.edge.client.service.EdgeHumanoidRobotService;
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 EdgeTouchOperateService implements EdgeOperateService {

    private final EdgeHumanoidRobotService edgeHumanoidRobotService;

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

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

        switch (action) {
            case TOUCH: {
                EdgeMoveJVO edgeMoveJVO = JSON.parseObject(touchParams, EdgeMoveJVO.class);
                edgeMoveJVO.setTerminalId(terminalId);
                edgeMoveJVO.setDeviceId(deviceId);

                edgeHumanoidRobotService.moveJ(edgeMoveJVO);
                break;
            }

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

}
