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.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.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.Callable;

public class ClickTextThreadNew implements Callable<Boolean> {

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

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

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

    @Override
    public Boolean call() throws Exception {
        CommonUtils.handlePreExecuteWait(preExecuteWait);
        AppiumDriver<WebElement> driver = (AppiumDriver<WebElement>) deviceDriver;
        if (text.length() > 20) {
            throw new ParamMistakeException("点击的文本过长");
        }
        String value = CommonUtils.handleVariable(context, text);
        long startTime = System.currentTimeMillis();
        long endTime = System.currentTimeMillis();
        PointMessage element = null;
        while (null == element && (endTime - startTime) / 1000 < waitTimeout) {
            element = ElementUtil.findTextByOcr(context,driver,value,index);
            endTime = System.currentTimeMillis();
        }
        Object isDebugObj = context.getContextVariable("__isDebug");
        boolean isDebug = false;
        if (null == isDebugObj) {
            logger.warn("isDebug获取为空！！");
        }else {
            isDebug = (boolean) isDebugObj;
        }
        if (!isDebug) {
            CommonUtils.screenShot(context,driver);
        }

        if (null == element) {
            throw new ExecuteException(String.format("文本【%s】不存在", text));
        }
        if (index <= 0) {
            throw new ExecuteException("选择文本的位置不符合要求");
        }
        Map<String, Object> deviceInfo = context.getDeviceInfo();
        String resolution = (String) deviceInfo.get("resolution");  //分辨率
        HandleUtils.click(context,driver, element);
        CommonUtils.handleSufExecuteWait(sufExecuteWait);
        return true;
    }
}
