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

import java.util.concurrent.Callable;

public class ClickTextDirectionTextThread implements Callable<Boolean> {

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

    private IExecuteContext context;
    private DeviceDriver deviceDriver;
    private Integer waitTimeout;
    private Float preExecuteWait;
    private Float sufExecuteWait;
    private String direction;
    private String text;
    private String text2;
    private Integer index;
    private Integer index2;


    public ClickTextDirectionTextThread(IExecuteContext context, DeviceDriver deviceDriver, Integer waitTimeout, String text, String direction,
                                        Float preExecuteWait, Float sufExecuteWait, Integer index, Integer index2, String text2) {
        this.context = context;
        this.deviceDriver = deviceDriver;
        this.waitTimeout = waitTimeout;
        this.preExecuteWait = preExecuteWait;
        this.sufExecuteWait = sufExecuteWait;
        this.direction = direction;
        this.text = text;
        this.index = index;
        this.index2 = index2;
        this.text2 = text2;
    }

    @Override
    public Boolean call() throws Exception {
        CommonUtils.handlePreExecuteWait(preExecuteWait);
        AppiumDriver driver = (AppiumDriver<WebElement>) deviceDriver;
        PointMessage point = ScreenUtil.clickTextDirectionText(context, deviceDriver, text, direction, index, index2, text2);
        if (point == null) {
            throw new ExecuteException("未找到指定内容");
        }
        HandleUtils.click(context,driver, point);
        CommonUtils.handleSufExecuteWait(sufExecuteWait);
        return true;
    }
}
