package net.northking.cctp.element.mobile.thread;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
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.entity.PointMessage;
import net.northking.cctp.element.mobile.entity.StepTarget;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

public class SwipeAndFindTargetElementThread implements Callable<Boolean> {

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

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

    public SwipeAndFindTargetElementThread(IExecuteContext context, DeviceDriver deviceDriver, List<IStepTarget> targets, String referenceStr, Integer waitTimeout, String waitStatusStr, String direction, Integer index, Float preExecuteWait, Float sufExecuteWait) {
        this.context = context;
        this.deviceDriver = deviceDriver;
        this.targets = targets;
        this.referenceStr = referenceStr;
        this.waitTimeout = waitTimeout;
        this.waitStatusStr = waitStatusStr;
        this.direction = direction;
        this.index = index;
        this.preExecuteWait = preExecuteWait;
        this.sufExecuteWait = sufExecuteWait;
    }

    @Override
    public Boolean call() throws Exception {
        CommonUtils.handlePreExecuteWait(preExecuteWait);
        boolean waitStatus = "1".equals(waitStatusStr);
        if (null == waitTimeout || waitTimeout <= 0) {
            waitTimeout = 30;
        }
        logger.debug("执行滑动查找控件的操作,滑动控件：{}", referenceStr);
        List<StepTarget> references = JSONObject.parseArray(referenceStr, StepTarget.class);
        List<IStepTarget> iStepTargets = new ArrayList<>();
        for (StepTarget reference : references) {
            iStepTargets.add(reference);
        }
        AppiumDriver<WebElement> driver = (AppiumDriver<WebElement>) deviceDriver;
        PointMessage point = ElementUtil.findWebElement(context, driver, iStepTargets, waitTimeout, waitStatus, "0", 0);
        if (null == point) {
            logger.warn("没有找到要滑动的控件位置....................");
            throw new ExecuteException("未找到需要滑动的元素");
        }
        logger.debug("找到要滑动的控件位置：{}",JSON.toJSONString(point));
        PointMessage targetPoint = ElementUtil.swipeAndFindTargetElement(point,context, driver, this.targets,waitTimeout,direction,index);
        CommonUtils.handleSufExecuteWait(sufExecuteWait);
        return targetPoint != null;
    }
}
