package net.northking.cctp.upperComputer.utils.deviceHepler;

import cn.hutool.core.img.ImgUtil;
import net.northking.cctp.upperComputer.exception.ExecuteException;
import net.northking.cctp.upperComputer.utils.HttpUtils;
import net.northking.cctp.upperComputer.utils.SpringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.UUID;

/**
 * @author : yineng.huang
 * @date : 2024/10/29 17:47
 */
public abstract class AbstractDeviceHelper implements DeviceHelper{

    private final Logger logger = LoggerFactory.getLogger(AbstractDeviceHelper.class);

    public String downloadAppToLocal(String appId, String appName) {
        File fileDir = new File(System.getProperty("user.dir") + "/tempApp/");
        if (!fileDir.exists()) {
            fileDir.mkdirs();
        }
        File app = new File(fileDir.getAbsolutePath() + "/" + appName);
        String path = SpringUtils.getProperties("nk.mobile-computer.appDownloadAddr");
        String server = SpringUtils.getProperties("nk.mobile-computer.serverAddr");
        String url = server + path.replace("{appId}", appId);
        HttpUtils.downloadFileToLocal(url, fileDir.getAbsolutePath(), appName);
        logger.info("应用[{}]下载完成，所在目录：{}。。。。。。。。。。。。。。。。。。。。", appName, app.getAbsolutePath());
        return app.getAbsolutePath();
    }

    @Override
    public File getScreenShotFile(String deviceId, Integer startX, Integer startY, int width, int height) {
        File sourceFile = getScreenShotFile(deviceId);
        File cutFilePath = new File(System.getProperty("user.dir") + "/tempPic");
        if (!cutFilePath.exists()) {
            cutFilePath.mkdirs();
        }
        File cutFile = new File(cutFilePath.getAbsolutePath() + "/" + UUID.randomUUID().toString() + ".png");
        ImgUtil.cut(sourceFile, cutFile, new java.awt.Rectangle(startX, startY, width, height));
        logger.debug("设备【{}】截取区域完成,地址：{}",deviceId,cutFile.getAbsolutePath());
        return cutFile;
    }

    protected File cutImageFile(File sourceFile,String deviceId, Integer startX, Integer startY, Integer cutWidth, Integer cutHeight, Integer screenWidth, Integer screenHeight,int width,int height){
        try {
            File cutFilePath = new File(System.getProperty("user.dir") + "/tempPic");
            if (!cutFilePath.exists()) {
                cutFilePath.mkdirs();
            }
            File cutFile = new File(cutFilePath.getAbsolutePath() + "/" + UUID.randomUUID().toString() + ".png");
            int realityWidth = width;
            int realityHeight = height;
            if (width > height) {  //宽高反了的情况，调换
                realityWidth = height;
                realityHeight = width;
            }
            Double realityCutWidth = cutWidth.doubleValue() / screenWidth.doubleValue() * realityWidth;
            Double realityCutHeight = cutHeight.doubleValue() / screenHeight.doubleValue() * realityHeight;
            Double realityX = startX.doubleValue() / screenWidth.doubleValue() * realityWidth;
            Double realityY = startY.doubleValue() / screenHeight.doubleValue() * realityHeight;
            ImgUtil.cut(sourceFile, cutFile, new java.awt.Rectangle(realityX.intValue(), realityY.intValue(), realityCutWidth.intValue(), realityCutHeight.intValue()));
            return cutFile;
        } catch (ExecuteException e) {
            logger.error("截图失败", e);
            throw e;
        } catch (Exception e) {
            logger.error("截图失败", e);
            throw new ExecuteException("截取图片失败");
        }
    }
}
