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.mobile.utils.CommonUtils;
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 GetCodeByOcrThread implements Callable<String> {
    private Logger logger = LoggerFactory.getLogger(GetCodeByOcrThread.class);

    private IExecuteContext context;
    private DeviceDriver deviceDriver;
    private List<IStepTarget> targets;
    private Integer waitTimeout;
    private String waitStatusStr;
    private Float preExecuteWait;
    private Float sufExecuteWait;

    public GetCodeByOcrThread(IExecuteContext context, DeviceDriver deviceDriver, List<IStepTarget> targets, Integer waitTimeout, String waitStatusStr, Float preExecuteWait, Float sufExecuteWait) {
        this.context = context;
        this.deviceDriver = deviceDriver;
        this.targets = targets;
        this.waitTimeout = waitTimeout;
        this.waitStatusStr = waitStatusStr;
        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 code = ScreenUtil.getCodeByOcr(context, driver, targets, waitTimeout, waitStatus);
        CommonUtils.handleSufExecuteWait(sufExecuteWait);
        return code;
    }
}
