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

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

public class GetElementTextThread implements Callable<String> {

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

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

    @Override
    public String call() throws Exception {
        CommonUtils.handlePreExecuteWait(preExecuteWait);
        boolean waitStatus = "1".equals(waitStatusStr);
        AppiumDriver<WebElement> driver = (AppiumDriver<WebElement>) deviceDriver;
        String result = null;
        for (IStepTarget item : targets) {
            if (UsingType.SELECTOR.equalsIgnoreCase(item.getUsing())) {  //xpath的方式
                WebElement webElement = ElementUtil.findWebElementByXpath(driver, item, waitTimeout, waitStatus, swipe, swipeCount);
                if (webElement != null) {
                    result = webElement.getText();
                    break;
                }
            } else if (UsingType.OCR.equalsIgnoreCase(item.getUsing())) {  //ocr方式
                result = ScreenUtil.getTextByOcr(context, driver, item,waitStatus);
            } else if (UsingType.HZOCR.equalsIgnoreCase(item.getUsing())) {  //ocr方式
                result = ScreenUtil.getTextByHzOcr(context, driver, item,waitStatus);
            }
        }
        if (null == result) {
            throw new ExecuteException("未识别到控件");
        }
        CommonUtils.handleSufExecuteWait(sufExecuteWait);
        return result;
    }
}
