package com.cmvr.edge.client.service.impl;

import cmvr.api.BioHeadServiceGrpc;
import cmvr.api.BioheadCommand;
import cmvr.api.Common;
import com.alibaba.fastjson2.JSON;
import com.cmvr.common.exception.GlobalException;
import com.cmvr.edge.client.manage.GrpcServiceManager;
import com.cmvr.edge.client.model.EdgeCommonVO;
import com.cmvr.edge.client.model.biohead.EdgeFacialExpressionVO;
import com.cmvr.edge.client.service.EdgeBioHeadService;
import com.cmvr.edge.client.utils.EdgeCommonUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class EdgeBioHeadServiceImpl implements EdgeBioHeadService {

    private final GrpcServiceManager grpcServiceManager;

    @Override
    public String setExpression(EdgeFacialExpressionVO facialExpressionVo) {
        String terminalId = facialExpressionVo.getTerminalId();
        String deviceId = facialExpressionVo.getDeviceId();
        BioHeadServiceGrpc.BioHeadServiceBlockingStub stub = grpcServiceManager.getGrpcClient(terminalId, BioHeadServiceGrpc.BioHeadServiceBlockingStub.class);

        // 请求头
        Common.CommandHeader.Request header = EdgeCommonUtil.buildRequest(deviceId);

        // 映射面部表情参数
        BioheadCommand.FacialExpression.Builder expressionBuilder = BioheadCommand.FacialExpression.newBuilder();

        // 映射 Eyebrow
        EdgeFacialExpressionVO.Eyebrow eyebrow = facialExpressionVo.getEyebrow();
        if (eyebrow != null) {
            expressionBuilder.setEyebrow(
                    BioheadCommand.FacialExpression.Eyebrow.newBuilder()
                            .setLeftOutsideY(eyebrow.getLeftOutsideY())
                            .setLeftInsideY(eyebrow.getLeftInsideY())
                            .setRightOutsideY(eyebrow.getRightOutsideY())
                            .setRightInsideY(eyebrow.getRightInsideY())
                            .build()
            );
        }

        // 映射 Eyelid
        EdgeFacialExpressionVO.Eyelid eyelid = facialExpressionVo.getEyelid();
        if (eyelid != null) {
            expressionBuilder.setEyelid(BioheadCommand.FacialExpression.Eyelid.newBuilder()
                    .setLeftUpperY(eyelid.getLeftUpperY())
                    .setLeftLowerY(eyelid.getLeftLowerY())
                    .setRightUpperY(eyelid.getRightUpperY())
                    .setRightLowerY(eyelid.getRightLowerY())
                    .build()
            );
        }

        // 映射 Eyeball
        EdgeFacialExpressionVO.Eyeball eyeball = facialExpressionVo.getEyeball();
        if (eyeball != null) {
            expressionBuilder.setEyeball(BioheadCommand.FacialExpression.Eyeball.newBuilder()
                    .setLeftX(eyeball.getLeftX())
                    .setLeftY(eyeball.getLeftY())
                    .setRightX(eyeball.getRightX())
                    .setRightY(eyeball.getRightY()).build());
        }

        // 映射 Mouth
        EdgeFacialExpressionVO.Mouth mouth = facialExpressionVo.getMouth();
        if (mouth != null) {
            expressionBuilder.setMouth(BioheadCommand.FacialExpression.Mouth.newBuilder()
                    .setUpperLipY(mouth.getUpperLipY())
                    .setLowerLipY(mouth.getLowerLipY())
                    .build()
            );
        }

        // 映射 Nose
        EdgeFacialExpressionVO.Nose nose = facialExpressionVo.getNose();
        if (nose != null) {
            expressionBuilder.setNose(BioheadCommand.FacialExpression.Nose.newBuilder()
                    .setLeftY(nose.getLeftY())
                    .setRightY(nose.getRightY())
                    .build()
            );
        }

        // 映射 Jaw
        EdgeFacialExpressionVO.Jaw jaw = facialExpressionVo.getJaw();
        if (jaw != null) {
            expressionBuilder.setJaw(BioheadCommand.FacialExpression.Jaw.newBuilder()
                    .setX(jaw.getX())
                    .setY(jaw.getY())
                    .build()
            );
        }

        // 构造完整请求
        BioheadCommand.SetFacialExpression.Request request = BioheadCommand.SetFacialExpression.Request.newBuilder()
                .setHeader(header)
                .setExpression(expressionBuilder.build())
                .build();

        BioheadCommand.SetFacialExpression.Feedback feedback = stub.setExpression(request);
        return JSON.toJSONString(feedback.getHeader());
    }

    @Override
    public String getSystemStatus(EdgeCommonVO edgeCommonVO) {
        String terminalId = edgeCommonVO.getTerminalId();
        String deviceId = edgeCommonVO.getDeviceId();
        BioHeadServiceGrpc.BioHeadServiceBlockingStub stub = grpcServiceManager.getGrpcClient(terminalId, BioHeadServiceGrpc.BioHeadServiceBlockingStub.class);

        // 请求头
        Common.CommandHeader.Request header = EdgeCommonUtil.buildRequest(deviceId);

        BioheadCommand.GetStatus.Request request = BioheadCommand.GetStatus.Request.newBuilder()
                .setHeader(header)
                .build();
        BioheadCommand.GetStatus.Feedback feedback = stub.getSystemStatus(request);
        return JSON.toJSONString(feedback.getHeader());
    }

    @Override
    public String emergencyStop(EdgeCommonVO edgeCommonVO) {
        String terminalId = edgeCommonVO.getTerminalId();
        String deviceId = edgeCommonVO.getDeviceId();
        BioHeadServiceGrpc.BioHeadServiceBlockingStub stub = grpcServiceManager.getGrpcClient(terminalId, BioHeadServiceGrpc.BioHeadServiceBlockingStub.class);

        // 请求头
        Common.CommandHeader.Request header = EdgeCommonUtil.buildRequest(deviceId);

        BioheadCommand.EmergencyStop.Request request = BioheadCommand.EmergencyStop.Request.newBuilder()
                .setHeader(header)
                .build();
        BioheadCommand.EmergencyStop.Feedback feedback = stub.emergencyStop(request);
        return JSON.toJSONString(feedback.getHeader());
    }

    @Override
    public String speakStart(EdgeCommonVO edgeCommonVO) {
        String terminalId = edgeCommonVO.getTerminalId();
        String deviceId = edgeCommonVO.getDeviceId();
        BioHeadServiceGrpc.BioHeadServiceBlockingStub stub = grpcServiceManager.getGrpcClient(terminalId, BioHeadServiceGrpc.BioHeadServiceBlockingStub.class);

        // 请求头
        Common.CommandHeader.Request header = EdgeCommonUtil.buildRequest(deviceId);
        BioheadCommand.SpeakStart.Request request = BioheadCommand.SpeakStart.Request.newBuilder().setHeader(header).build();
        BioheadCommand.SpeakStart.Feedback feedback = stub.speakStart(request);
        return JSON.toJSONString(feedback.getHeader());
    }

    @Override
    public String speakStop(EdgeCommonVO edgeCommonVO) {
        String terminalId = edgeCommonVO.getTerminalId();
        String deviceId = edgeCommonVO.getDeviceId();
        BioHeadServiceGrpc.BioHeadServiceBlockingStub stub = grpcServiceManager.getGrpcClient(terminalId, BioHeadServiceGrpc.BioHeadServiceBlockingStub.class);

        // 请求头
        Common.CommandHeader.Request header = EdgeCommonUtil.buildRequest(deviceId);
        BioheadCommand.SpeakStop.Request request = BioheadCommand.SpeakStop.Request.newBuilder().setHeader(header).build();
        BioheadCommand.SpeakStop.Feedback feedback = stub.speakStop(request);
        return JSON.toJSONString(feedback.getHeader());
    }

    @Override
    public String specialExpression(String terminalId, String deviceId, Integer expressKey) {
        BioHeadServiceGrpc.BioHeadServiceBlockingStub stub = grpcServiceManager.getGrpcClient(terminalId, BioHeadServiceGrpc.BioHeadServiceBlockingStub.class);

        // 请求头
        Common.CommandHeader.Request header = EdgeCommonUtil.buildRequest(deviceId);

        switch (expressKey) {
            case 1:
                // 高兴
                BioheadCommand.Happy.Request happy = BioheadCommand.Happy.Request.newBuilder().setHeader(header).build();
                BioheadCommand.Happy.Feedback happyBack = stub.happy(happy);
                return JSON.toJSONString(happyBack.getHeader());
            case 2:
                // 惊讶
                BioheadCommand.Surprise.Request surprise = BioheadCommand.Surprise.Request.newBuilder().setHeader(header).build();
                BioheadCommand.Surprise.Feedback surpriseBack = stub.surprise(surprise);
                return JSON.toJSONString(surpriseBack.getHeader());
            case 3:
                // 愤怒
                BioheadCommand.ExpressionAngry.Request angry = BioheadCommand.ExpressionAngry.Request.newBuilder().setHeader(header).build();
                BioheadCommand.ExpressionAngry.Feedback angryBack = stub.expressionAngry(angry);
                return JSON.toJSONString(angryBack.getHeader());
            case 4:
                // 悲伤
                BioheadCommand.ExpressionSadness.Request sadness = BioheadCommand.ExpressionSadness.Request.newBuilder().setHeader(header).build();
                BioheadCommand.ExpressionSadness.Feedback sadnessBack = stub.expressionSadness(sadness);
                return JSON.toJSONString(sadnessBack.getHeader());
            case 5:
                // 睡觉
                BioheadCommand.ExpressionTired.Request tired = BioheadCommand.ExpressionTired.Request.newBuilder().setHeader(header).build();
                BioheadCommand.ExpressionTired.Feedback tiredBack = stub.expressionTired(tired);
                return JSON.toJSONString(tiredBack.getHeader());
            case 6:
                // 打哈欠
                BioheadCommand.ExpressionYawn.Request yawn = BioheadCommand.ExpressionYawn.Request.newBuilder().setHeader(header).build();
                BioheadCommand.ExpressionYawn.Feedback yawnBack = stub.expressionYawn(yawn);
                return JSON.toJSONString(yawnBack.getHeader());
            default:
                throw new GlobalException("错误的传参，1:高兴,2:惊讶,3:愤怒,4:悲伤,5:睡觉,6:打哈欠");
        }

    }
}
