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.IStepTarget;
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.entity.PointMessage;
import net.northking.cctp.element.mobile.utils.CommonUtils;
import net.northking.cctp.element.mobile.utils.phoneUtils.ElementUtil;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.Callable;

public class ElementSwipeThread implements Callable<Boolean> {

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

    private IExecuteContext context;
    private DeviceDriver deviceDriver;
    private List<IStepTarget> targets;
    private Integer waitTimeout;
    private String waitStatusStr;
    private String swipe;
    private Integer swipeCount;
    private Float preExecuteWait;
    private Float sufExecuteWait;
    private String direction;
    private Integer size;

    public ElementSwipeThread(IExecuteContext context, DeviceDriver deviceDriver, List<IStepTarget> targets, Integer waitTimeout, String waitStatusStr, String swipe, Integer swipeCount, Float preExecuteWait, Float sufExecuteWait, String direction, Integer size) {
        this.context = context;
        this.deviceDriver = deviceDriver;
        this.targets = targets;
        this.waitTimeout = waitTimeout;
        this.waitStatusStr = waitStatusStr;
        this.swipe = swipe;
        this.swipeCount = swipeCount;
        this.preExecuteWait = preExecuteWait;
        this.sufExecuteWait = sufExecuteWait;
        this.direction = direction;
        this.size = size;
    }

    @Override
    public Boolean call() throws Exception {
        CommonUtils.handlePreExecuteWait(preExecuteWait);
        boolean waitStatus = "1".equals(waitStatusStr);
        AppiumDriver driver = (AppiumDriver<WebElement>) deviceDriver;
        PointMessage point = ElementUtil.findWebElement(context, driver, targets, waitTimeout, waitStatus, swipe, swipeCount);
        if (point != null) {
            logger.info("查找到控件的位置=====>x:{},y:{}", point.getX(), point.getY());
            try {
                if (null != size && size > 0) {
                    ElementUtil.elementSwipe(context,point, direction, driver,size);
                } else {
                    ElementUtil.elementSwipe(context,point, direction, driver);
                }
            } catch (ParamMistakeException | EnvironmentException | ExecuteException | NoSuchSessionException e) {
                throw e;
            } catch (Exception e) {
                logger.error("滑屏失败", e);
                throw new ExecuteException("滑屏失败");
            }
        } else {
            logger.error("查找不到控件");
            throw new ExecuteException("查找不到控件");
        }
        CommonUtils.handleSufExecuteWait(sufExecuteWait);
        return true;
    }
}
