package net.northking.cctp.element.mobile.utils.phoneUtils;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.ios.IOSDriver;
import net.northking.cctp.element.core.IExecuteContext;
import net.northking.cctp.element.core.exception.ExecuteException;
import net.northking.cctp.element.mobile.utils.ThreadUtil;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.*;

public class AppUtil {

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

    public static boolean handleApp(IExecuteContext context, AppiumDriver<WebElement> driver, String appPackage, Integer waitTimeout, String type){
        ExecutorService executor = ThreadUtil.getExecutor();
        Future<Boolean> future = executor.submit(() -> {
            if (driver instanceof AndroidDriver) {
                if ("0".equalsIgnoreCase(type)) {
                    logger.info("关闭android的app:{}",appPackage);
                    try {
                        driver.terminateApp(appPackage);
                    } catch (WebDriverException e) {
                        logger.warn("driver抽风了。。。。,休息一下再来");
                        Thread.sleep(1000);
                        driver.terminateApp(appPackage);
                    }
                } else if ("1".equalsIgnoreCase(type)) {
                    logger.info("启动android的app:{}",appPackage);
                    try {
                        AndroidUtil.activeAndroidApp(context,driver,appPackage);
                    } catch (WebDriverException e) {
                        logger.warn("driver抽风了。。。。,休息一下再来");
                        Thread.sleep(1000);
                        AndroidUtil.activeAndroidApp(context,driver,appPackage);
                    }
                } else {
                    logger.info("重启android的app:{}",appPackage);
                    try {
                        driver.terminateApp(appPackage);
                    } catch (WebDriverException e) {
                        logger.warn("driver抽风了。。。。,休息一下再来");
                        Thread.sleep(1000);
                        driver.terminateApp(appPackage);
                    }
                    Thread.sleep(1500);
                    try {
                        AndroidUtil.activeAndroidApp(context,driver,appPackage);
                    } catch (WebDriverException e) {
                        logger.warn("driver抽风了。。。。,休息一下再来");
                        Thread.sleep(1000);
                        AndroidUtil.activeAndroidApp(context,driver,appPackage);
                    }
                }
            } else if (driver instanceof IOSDriver) {
                if ("0".equalsIgnoreCase(type)) {
                    logger.info("关闭ios的app:{}",appPackage);
                    try {
                        driver.terminateApp(appPackage);
                    } catch (WebDriverException e) {
                        logger.warn("driver抽风了。。。。,休息一下再来");
                        Thread.sleep(1000);
                        driver.terminateApp(appPackage);
                    }
                } else if ("1".equalsIgnoreCase(type)) {
                    logger.info("启动ios的app:{}",appPackage);
                    IosUtil.activeIosApp(context,driver,appPackage);
                } else {
                    logger.info("重启ios的app:{}",appPackage);
                    try {
                        driver.terminateApp(appPackage);
                    } catch (WebDriverException e) {
                        logger.warn("driver抽风了。。。。,休息一下再来");
                        Thread.sleep(1000);
                        driver.terminateApp(appPackage);
                    }
                    IosUtil.activeIosApp(context,driver,appPackage);
                }
            }
            return true;
        }
        );
        if (null == waitTimeout || waitTimeout <= 0 || waitTimeout > 120) {
            waitTimeout = 30;
        }
        try {
            Boolean aBoolean = future.get(waitTimeout, TimeUnit.SECONDS);
            return aBoolean;
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            logger.warn("app操作失败：",e);
            return false;
        } catch (ExecutionException e) {
            Throwable cause = e.getCause();
            if (cause instanceof NoSuchSessionException) {
                logger.warn("driver失效了。。。。");
                NoSuchSessionException nsse = (NoSuchSessionException) cause;
                throw nsse;
            }
            logger.warn("app操作失败：{}",e);
            return false;
        } catch (TimeoutException e) {
            logger.warn("app操作失败：{}",e);
            throw new ExecuteException(String.format("app【%s】操作失败，请稍后再试", appPackage));
        }
    }

}
