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.ExecuteException;
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 net.northking.cctp.element.mobile.utils.phoneUtils.HandleUtils;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class ClickElementThread implements Callable<Boolean> {

    private Logger logger = LoggerFactory.getLogger(ClickElementThread.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 Integer xMove;
    private Integer yMove;

    public ClickElementThread(IExecuteContext context, DeviceDriver deviceDriver, List<IStepTarget> targets, Integer waitTimeout, String waitStatusStr, String swipe, Integer swipeCount, Float preExecuteWait, Float sufExecuteWait, Integer xMove, Integer yMove) {
        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.xMove = xMove;
        this.yMove = yMove;
    }

    @Override
    public Boolean call() throws Exception {
        CommonUtils.handlePreExecuteWait(preExecuteWait);
        boolean waitStatus = "1".equals(waitStatusStr);
        if (null == waitTimeout || waitTimeout <= 0) {
            waitTimeout = 30;
        }
        logger.debug("执行点击控件操作,args:控件位置{},等待界面稳定{},滑动方向{},滑动次数{}", targets, waitStatus, swipe, swipeCount);
        AppiumDriver<WebElement> driver = (AppiumDriver<WebElement>) deviceDriver;
        PointMessage point = ElementUtil.findWebElement(context, driver, targets, waitTimeout, waitStatus, swipe, swipeCount);
        Object isDebugObj = context.getContextVariable("__isDebug");
        boolean isDebug = false;
        if (null == isDebugObj) {
            logger.warn("isDebug获取为空！！");
        }else {
            isDebug = (boolean) isDebugObj;
        }
        if (!isDebug) {
            CommonUtils.screenShot(context,driver);
        }
        if (point == null) {
            logger.error("查找不到控件");
            throw new ExecuteException("查找不到控件");
        }
        /**
         * 设置偏移量
         */
        point.setxMove(xMove);
        point.setyMove(yMove);


        /**
         * 设置偏移量
         */
        point.setxMove(xMove);
        point.setyMove(yMove);

        HandleUtils.click(context,driver, point);
        CommonUtils.handleSufExecuteWait(sufExecuteWait);
        return true;
    }
}
