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

import cmvr.api.HlcCommand;
import cmvr.api.HlcServiceGrpc;
import com.alibaba.fastjson2.JSON;
import com.cmvr.common.exception.GlobalException;
import com.cmvr.edge.client.manage.GrpcServiceManager;
import com.cmvr.edge.client.model.hlc.EdgeTouchVO;
import com.cmvr.edge.client.service.EdgeHlcService;
import com.cmvr.edge.client.utils.EdgeCommonUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

@Slf4j
@Service
@RequiredArgsConstructor
public class EdgeHlcServiceImpl implements EdgeHlcService {

    private final GrpcServiceManager grpcServiceManager;

    @Override
    public String touch(File image, String terminalId, String deviceId, int x, int y, double maxForce) {
        HlcServiceGrpc.HlcServiceBlockingStub stub = grpcServiceManager.getGrpcClient(terminalId, HlcServiceGrpc.HlcServiceBlockingStub.class);
        HlcCommand.Touch.Request request = HlcCommand.Touch.Request.newBuilder()
                .setHeader(EdgeCommonUtil.buildRequest(deviceId))
                .setU(x)
                .setV(y)
                .setMaxForce(maxForce)
                .build();
        String response = JSON.toJSONString(stub.touch(request).getHeader());
        log.info("触控调用成功，响应结果:{}", response);
        markPoint(image,x,y);
        return response;
    }

    /**
     * 在图片中指定坐标点进行标注
     *
     * @param imageFile 原始图片文件
     * @param x         坐标 X
     * @param y         坐标 Y
     */
    @Override
    public void markPoint(File imageFile, int x, int y) {
        // 读入图片
        BufferedImage image;
        try {
            image = ImageIO.read(imageFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        Graphics2D g = image.createGraphics();

        // 抗锯齿
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        // 设置画笔颜色
        g.setColor(Color.GREEN);

        // 半径
        int radius = 5;
        // 画实心圆
        g.fillOval(x - radius, y - radius, radius * 2, radius * 2);

        g.dispose();

        // 输出文件
        String outPath = imageFile.getParent() + File.separator +
                "marked_" + imageFile.getName();
        File outFile = new File(outPath);

        try {
            ImageIO.write(image, "png", outFile);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        log.info("图片标注成功:{}", outPath);
    }

    @Override
    public String touch(EdgeTouchVO edgeTouchVO) {
        if (edgeTouchVO.getX() == edgeTouchVO.getY() && edgeTouchVO.getX() == 0) {
            throw new GlobalException("触控坐标不能为0");
        }
        HlcServiceGrpc.HlcServiceBlockingStub stub = grpcServiceManager.getGrpcClient(edgeTouchVO.getTerminalId(), HlcServiceGrpc.HlcServiceBlockingStub.class);
        HlcCommand.Touch.Request request = HlcCommand.Touch.Request.newBuilder()
                .setHeader(EdgeCommonUtil.buildRequest(edgeTouchVO.getDeviceId()))
                .setU(edgeTouchVO.getX())
                .setV(edgeTouchVO.getY())
                .setMaxForce(edgeTouchVO.getMaxForce())
                .build();
        return JSON.toJSONString(stub.touch(request).getHeader());
    }
}
