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

import net.northking.cctp.upperComputer.deviceManager.IOSDeviceManager;
import net.northking.cctp.upperComputer.deviceManager.thread.IosDeviceInitThread;
import net.northking.cctp.upperComputer.driver.ios.NKAgent;
import net.northking.cctp.upperComputer.utils.ScreenShotUtils;
import net.northking.cctp.upperComputer.utils.deviceHepler.AbstractDeviceHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * @author : yineng.huang
 * @date : 2024/4/24 16:44
 */
public abstract class IosDeviceHandleHelper extends AbstractDeviceHelper {

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

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

    public File getScreenShotFile(String deviceId) {
        return ScreenShotUtils.getIOSMobileScreenShot(deviceId);
    }

    @Override
    public boolean cleanData(String deviceId, String appPackage) {
        logger.debug("ios设备【{}】无需清除数据...................",deviceId);
        return true;
    }

    @Override
    public boolean terminateApp(String deviceId, String appPackage) {
        IosDeviceInitThread iosInitThread = IOSDeviceManager.getInstance().getIosInitThread(deviceId);
        boolean success = false;
        if (null != iosInitThread && iosInitThread.isAlive() && !iosInitThread.isInterrupted()) {
            NKAgent nkAgent = iosInitThread.getNkAgent();
            if (null != nkAgent) {
                success = nkAgent.terminateApp(appPackage);
            }
        }
        logger.debug("设备【{}】关闭应用【{}】结果：{}", deviceId, appPackage, success);
        return success;
    }
}
