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

import com.alibaba.fastjson.JSON;
import net.northking.cctp.upperComputer.deviceManager.IOSDeviceManager;
import net.northking.cctp.upperComputer.deviceManager.common.PyMobileDevice;
import net.northking.cctp.upperComputer.deviceManager.entity.AppleApplicationInfo;
import net.northking.cctp.upperComputer.exception.ExecuteException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;

/**
 * @author : yineng.huang
 * @date : 2024/6/12 14:18
 */
public class MacIosHandleHelper extends IosDeviceHandleHelper {

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

    @Override
    public boolean isAppInstalled(String deviceId, String appPackage) {
        PyMobileDevice.SpecificAppleDeviceInfo specificAppleDeviceInfo = IOSDeviceManager.getInstance().getSpecificAppleDeviceInfo(deviceId);
        HashMap<String, AppleApplicationInfo> appMap = PyMobileDevice.getInstance().listApp(specificAppleDeviceInfo, false, true);
        if (null == appMap) {
            logger.warn("没有在设备【{}】上找到app【{}】信息",deviceId,appPackage);
            return false;
        }
        boolean contain = appMap.containsKey(appPackage);
        if (contain) {
            AppleApplicationInfo appleApplicationInfo = appMap.get(appPackage);
            logger.debug("设备【{}】上的app信息：{}",deviceId,JSON.toJSONString(appleApplicationInfo));
            return true;
        }
        return false;
    }

    @Override
    public boolean removeApp(String deviceId, String appPackage) {
        PyMobileDevice.SpecificAppleDeviceInfo specificAppleDeviceInfo = IOSDeviceManager.getInstance().getSpecificAppleDeviceInfo(deviceId);
        boolean success = PyMobileDevice.getInstance().uninstallApp(specificAppleDeviceInfo, appPackage, null);
        logger.debug("设备【{}】卸载app【{}】的结果：{}",deviceId,appPackage,success);
        return success;
    }

    @Override
    public String getOldPackageCode(String deviceId, String appPackage) {
        PyMobileDevice.SpecificAppleDeviceInfo specificAppleDeviceInfo = IOSDeviceManager.getInstance().getSpecificAppleDeviceInfo(deviceId);
        HashMap<String, AppleApplicationInfo> appMap = PyMobileDevice.getInstance().listApp(specificAppleDeviceInfo, false, true);
        if (null == appMap) {
            logger.warn("没有在设备【{}】上找到app【{}】信息",deviceId,appPackage);
            return null;
        }
        boolean contain = appMap.containsKey(appPackage);
        if (contain) {
            AppleApplicationInfo appleApplicationInfo = appMap.get(appPackage);
            logger.debug("设备【{}】上的app信息：{}",deviceId,JSON.toJSONString(appleApplicationInfo));
            return appleApplicationInfo.getCFBundleShortVersionString();
        } else {
            logger.warn("没有在设备【{}】上找到app【{}】信息",deviceId,appPackage);
        }
        return null;
    }

    @Override
    public boolean installApp(String deviceId, String appPath) {
        PyMobileDevice.SpecificAppleDeviceInfo specificAppleDeviceInfo = IOSDeviceManager.getInstance().getSpecificAppleDeviceInfo(deviceId);
        boolean success = PyMobileDevice.getInstance().installApp(specificAppleDeviceInfo, appPath, null);
        logger.debug("设备【{}】安装app【{}】的结果：{}",deviceId,appPath,success);
        return success;
    }

    @Override
    public boolean activateApp(String deviceId, String appPackage) {
        PyMobileDevice.SpecificAppleDeviceInfo specificAppleDeviceInfo = IOSDeviceManager.getInstance().getSpecificAppleDeviceInfo(deviceId);
        boolean success = PyMobileDevice.getInstance().launchApp(specificAppleDeviceInfo,appPackage);
        logger.debug("设备【{}】启动app【{}】的结果：{}",deviceId,appPackage,success);
        return success;
    }

    @Override
    public File getScreenShotFile(String deviceId, Integer startX, Integer startY, Integer cutWidth, Integer cutHeight, Integer screenWidth, Integer screenHeight) {
        if (cutWidth <= 0 || cutHeight <= 0) {
            throw new ExecuteException("截取图片的宽高不满足要求");
        }
        PyMobileDevice.SpecificAppleDeviceInfo specificAppleDeviceInfo = IOSDeviceManager.getInstance().getSpecificAppleDeviceInfo(deviceId);
        File file = null;
        File screenShotFile = null;
        try {
            screenShotFile = PyMobileDevice.getInstance().takeScreenshotSaveAsFile(specificAppleDeviceInfo,System.getProperty("user.dir") + "/tempPic");
            if (screenShotFile == null) {
                logger.error("设备【{}】未截出来图...............", deviceId);
                throw new ExecuteException(String.format("设备【%s】截图失败", deviceId));
            } else {
                logger.debug("设备【{}】截图的结果：{}",deviceId,screenShotFile.getAbsolutePath());
            }
            file = cutImageFile(screenShotFile, deviceId, startX, startY, cutWidth, cutHeight, screenWidth, screenHeight);
            logger.debug("截取出来的图像为：{}",file.getAbsolutePath());
        } finally {
            if (null != screenShotFile) {
                //删除
            }
            if (null != file) {
                //删除
            }
        }
        return file;
    }
}
