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.constants.UsingType;
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.WebDriverException;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class InputTextElementThread implements Callable<Boolean> {

    private Logger logger = LoggerFactory.getLogger(InputTextElementThread.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;

    public InputTextElementThread(IExecuteContext context, DeviceDriver deviceDriver, List<IStepTarget> targets, Integer waitTimeout, String waitStatusStr, String swipe, Integer swipeCount, Float preExecuteWait, Float sufExecuteWait, String cleanStr, String text) {
        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;
    }

    @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;
        IStepTarget target = null;
        for (IStepTarget item : targets) {
            if (UsingType.SELECTOR.equalsIgnoreCase(item.getUsing())) {
                target = item;
                break;
            }
        }
        if (null == target) {
            throw new ExecuteException("该操作只支持xpath方式查找元素");
        }
        WebElement element = ElementUtil.findWebElementByXpath(driver, target, waitTimeout, waitStatus, swipe, swipeCount);
        if (element == null) {
            logger.error("查找不到控件");
            throw new ExecuteException("查找不到控件");
        }
        try {
            String value = CommonUtils.handleVariable(context, text);
            logger.info("输入文本值为：{}", value);
            ElementUtil.inputText(context,driver,element,value,"text",0,clean);
        } catch (ParamMistakeException | ExecuteException | WebDriverException e) {
            throw e;
        } catch (Exception e) {
            logger.error("控件输入失败", e);
            throw new ExecuteException("控件输入失败");
        }
        CommonUtils.handleSufExecuteWait(sufExecuteWait);
        return true;
    }
}
