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.exception.EnvironmentException;
import net.northking.cctp.element.core.exception.ExecuteException;
import net.northking.cctp.element.core.exception.ParamMistakeException;
import net.northking.cctp.element.mobile.utils.CommonUtils;
import net.northking.cctp.element.mobile.utils.phoneUtils.ScreenUtil;
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 StandardSwipeThread implements Callable<Boolean> {

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

    private DeviceDriver deviceDriver;
    private String direction;
    private Integer size;
    private Float preExecuteWait;
    private Float sufExecuteWait;

    public StandardSwipeThread(DeviceDriver deviceDriver, String direction, Integer size, Float preExecuteWait, Float sufExecuteWait) {
        this.deviceDriver = deviceDriver;
        this.direction = direction;
        this.size = size;
        this.preExecuteWait = preExecuteWait;
        this.sufExecuteWait = sufExecuteWait;
    }

    @Override
    public Boolean call() throws Exception {
        CommonUtils.handlePreExecuteWait(preExecuteWait);
        AppiumDriver driver = (AppiumDriver<WebElement>) deviceDriver;
        try {
            if (null != size && size > 0) {
                ScreenUtil.standardSwipe(direction, 2000, driver, size);
            } else {
                ScreenUtil.standardSwipe(direction, 2000, driver);
            }
        } catch (ParamMistakeException | EnvironmentException | ExecuteException | NoSuchSessionException e) {
            throw e;
        } catch (Exception e) {
            logger.error("滑屏失败", e);
            throw new ExecuteException("滑屏失败");
        }
        CommonUtils.handleSufExecuteWait(sufExecuteWait);
        return true;
    }
}
