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.EnvironmentException;
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.ScreenUtil;
import org.openqa.selenium.NoSuchSessionException;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

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

public class IfTextThread implements Callable<Boolean> {

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

    private IExecuteContext context;
    private DeviceDriver deviceDriver;
    private Integer waitTimeout;
    private Float preExecuteWait;
    private Float sufExecuteWait;
    private String text;
    private String swipe;
    private Integer swipeCount;

    public IfTextThread(IExecuteContext context, DeviceDriver deviceDriver, Integer waitTimeout, String swipe, Integer swipeCount, Float preExecuteWait, Float sufExecuteWait, String text) {
        this.context = context;
        this.deviceDriver = deviceDriver;
        this.waitTimeout = waitTimeout;
        this.preExecuteWait = preExecuteWait;
        this.sufExecuteWait = sufExecuteWait;
        this.text = text;
        this.swipe = swipe;
        this.swipeCount = swipeCount;
    }

    @Override
    public Boolean call() throws Exception {
        CommonUtils.handlePreExecuteWait(preExecuteWait);
        Boolean b = false;
        AppiumDriver<WebElement> driver = (AppiumDriver<WebElement>) deviceDriver;

        Integer perPageTimeout = waitTimeout;
        Integer swipeNum = swipeCount;
        if ((swipeCount <= 0 && swipeCount > 5)/*限定滑屏查找不超过5次*/ || "0".equals(swipe)  /*不滑屏*/) {
            swipeNum = 0;
        }
        if (swipeNum > 0) {
            perPageTimeout = waitTimeout / swipeCount;
        }
        try {
            driver.manage().timeouts().implicitlyWait(perPageTimeout, TimeUnit.SECONDS);
        } catch (NoSuchSessionException e) {
            throw e;
        }
        try {
            while (swipeNum >= 0) {
                List<PointMessage> elements = null;
                try {
                    String existValue = CommonUtils.handleVariable(context, text);
                    elements = ElementUtil.findElementsByText(context, driver, existValue);
                } catch (EnvironmentException | ParamMistakeException | NoSuchSessionException e) {
                    throw e;
                } catch (Exception e) {
                    logger.error("判断文本是否存在出错，文本内容：{}", text, e);
                }
                if (!CollectionUtils.isEmpty(elements)) {
                    logger.info("找到元素{}个", elements.size());
                }
                if (!CollectionUtils.isEmpty(elements)) {   //xpath找到了
                    b = true;
                } else {  //xpath没找到
                    if (swipeNum > 0) {
                        ScreenUtil.standardSwipe(swipe, 2000, driver);
                    }
                }
                swipeNum--;
            }
        } catch (Exception e) {
            logger.error("判断文本是否存在出错", e);
        }
        CommonUtils.handleSufExecuteWait(sufExecuteWait);
        return b;
    }
}
