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

import cn.hutool.core.img.ImgUtil;
import net.northking.cctp.upperComputer.deviceManager.IOSDeviceManager;
import net.northking.cctp.upperComputer.entity.PhoneEntity;
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/4/24 16:44
 */
public abstract class IosDeviceHandleHelper {

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

    protected final String LAUNCH_APP_FAIL_FLAG = "invalid code signature"; //应用未信任启动失败标志

    public abstract boolean isAppInstalled(String deviceId, String appPackage);

    public abstract boolean removeApp(String deviceId, String appPackage);

    public abstract String getOldPackageCode(String deviceId, String appPackage);

    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();
    }

    protected File cutImageFile(File sourceFile,String deviceId, Integer startX, Integer startY, Integer cutWidth, Integer cutHeight, Integer screenWidth, Integer screenHeight){
        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");
            //转换比例
            PhoneEntity phoneEntity = IOSDeviceManager.getInstance().getPhoneEntity(deviceId);
            int width = phoneEntity.getScreenWidth() * phoneEntity.getScale();
            int height = phoneEntity.getScreenHeight() * phoneEntity.getScale();

            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("截取图片失败");
        }
    }

    public abstract boolean installApp(String deviceId, String appPath);

    public abstract boolean activateApp(String deviceId, String appPackage);

    public abstract File getScreenShotFile(String deviceId, Integer startX, Integer startY, Integer cutWidth, Integer cutHeight, Integer screenWidth, Integer screenHeight);
}
