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.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 net.northking.cctp.element.mobile.utils.phoneUtils.HandleUtils;
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 InputTextThread implements Callable<Boolean> {
    private Logger logger = LoggerFactory.getLogger(InputTextThread.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 cleanStr;
    private String text;

    private Integer xMove;
    private Integer yMove;

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

    @Override
    public Boolean call() throws Exception {
        CommonUtils.handlePreExecuteWait(preExecuteWait);
        boolean clean = "1".equals(cleanStr);
        boolean waitStatus = "1".equals(waitStatusStr);
        AppiumDriver<WebElement> driver = (AppiumDriver<WebElement>) deviceDriver;
        PointMessage point = ElementUtil.findWebElement(context, driver, targets, waitTimeout, waitStatus, swipe, swipeCount);
        if (point == null) {
            logger.error("查找不到控件");
            throw new ExecuteException("查找不到控件");
        }
        try {
            String value = CommonUtils.handleVariable(context, text);
            logger.info("输入文本值为：{}", value);
            try {
                /**
                 * 设置偏移量
                 */
                point.setxMove(xMove);
                point.setyMove(yMove);
                HandleUtils.click(context,driver, point);
            } catch (Exception e) {
                logger.error("输入前点击控件失败", e);
                throw new ExecuteException("输入前点击控件失败");
            }
            ElementUtil.inputTextDirect(driver,value,clean);
        } catch (ParamMistakeException | ExecuteException | NoSuchSessionException e) {
            throw e;
        } catch (Exception e) {
            logger.error("控件输入失败", e);
            throw new ExecuteException("控件输入失败");
        }
        CommonUtils.handleSufExecuteWait(sufExecuteWait);
        return true;
    }
}
