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.mobile.utils.CommonUtils;
import net.northking.cctp.element.mobile.utils.phoneUtils.ScreenUtil;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;

public class GetTextDirectionTextThread implements Callable<String> {

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

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


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

    @Override
    public String call() throws Exception {
        CommonUtils.handlePreExecuteWait(preExecuteWait);
        AppiumDriver driver = (AppiumDriver<WebElement>) deviceDriver;
        String result = ScreenUtil.getTextDirectionText(context, deviceDriver, text, direction, index, ifMoney);
        if (StringUtils.isNotEmpty(result) && "1".equals(ifMoney)) {
            result = result.replace("，", ",");
            result = result.replace("。", ".");
            result = result.replace(" ", "");
        }
        CommonUtils.handleSufExecuteWait(sufExecuteWait);
        return result;
    }
}
