package net.northking.cctp.element.mobile.thread;

import io.appium.java_client.AppiumDriver;
import net.northking.cctp.element.core.DeviceDriver;
import net.northking.cctp.element.core.IExecuteContext;
import net.northking.cctp.element.core.exception.ExecuteException;
import net.northking.cctp.element.mobile.utils.CommonUtils;
import net.northking.cctp.element.mobile.utils.phoneUtils.FunctionKeyUtil;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;

public class PressKeyHomeThread implements Callable<Boolean> {

    private Logger logger = LoggerFactory.getLogger(PressKeyHomeThread.class);

    private IExecuteContext context;
    private DeviceDriver deviceDriver;
    private Float preExecuteWait;
    private Float sufExecuteWait;

    public PressKeyHomeThread(IExecuteContext context, DeviceDriver deviceDriver, Float preExecuteWait, Float sufExecuteWait) {
        this.context = context;
        this.deviceDriver = deviceDriver;
        this.preExecuteWait = preExecuteWait;
        this.sufExecuteWait = sufExecuteWait;
    }

    @Override
    public Boolean call() throws Exception {
        CommonUtils.handlePreExecuteWait(preExecuteWait);
        try {
            AppiumDriver<WebElement> driver = (AppiumDriver<WebElement>) deviceDriver;
            FunctionKeyUtil.pressKeyHome(driver, context);
        } catch (NoSuchSessionException e) {
            logger.warn("driver失效了。。。。。");
            throw e;
        } catch (Exception e) {
            logger.error("点击home键失败",e);
            throw new ExecuteException("点击home键失败");
        }
        CommonUtils.handleSufExecuteWait(sufExecuteWait);
        return true;
    }
}
