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

import com.alibaba.fastjson.JSON;
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.constants.UsingType;
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 net.northking.cctp.element.mobile.utils.phoneUtils.ScreenUtil;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.Duration;
import java.util.*;
import java.util.concurrent.Callable;

public class PressAndSwipeThread implements Callable<Boolean> {

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

    private DeviceDriver deviceDriver;
    private List<Integer> passingPoints;
    private Float preExecuteWait;
    private Float sufExecuteWait;
    private List<IStepTarget> targets;

    public PressAndSwipeThread(DeviceDriver deviceDriver, List<IStepTarget> targets,List<Integer> passingPoints,  Float preExecuteWait, Float sufExecuteWait) {
        this.deviceDriver = deviceDriver;
        this.targets = targets;
        this.passingPoints = passingPoints;
        this.preExecuteWait = preExecuteWait;
        this.sufExecuteWait = sufExecuteWait;

    }

    @Override
    public Boolean call() throws Exception {
        CommonUtils.handlePreExecuteWait(preExecuteWait);
        AppiumDriver<WebElement> driver = (AppiumDriver<WebElement>) deviceDriver;
        WebElement webElement = null;
        for (IStepTarget target : targets) {
            if (UsingType.SELECTOR.equalsIgnoreCase(target.getUsing())) {
                webElement = ElementUtil.findWebElementByXpath(driver, target, 30, true, "0", 0);
            }
        }
        if (null == webElement) {
            throw new ExecuteException("未找到解锁的滑动区域");
        }
        Point location = webElement.getLocation();
        int x = location.getX();
        int y = location.getY();
        int width = webElement.getRect().width;
        int height = webElement.getRect().height;
        List<Map<String, Integer>> pointList = new ArrayList<>();
        logger.info("手势区域，位置=>x:{},y:{},宽：{}，高：{}",x,y,width,height);
        int perWidth = width /3;
        int perHeight = height /3;
        int pointY = y - perHeight/2;
        int n = 1;
        for (int i = 0; i < 3; i++) {
            pointY = pointY + perHeight;   //每一行的y坐标
            int pointX = x - perWidth/2;
            for (int j = 0; j < 3; j++) {
                pointX = pointX + perWidth;
                logger.info("第{}个点的坐标x:{},y:{}",n,pointX,pointY);
                Map<String, Integer> point = new HashMap<>();
                point.put("x", pointX);
                point.put("y", pointY);
                pointList.add(n-1,point);
                n++;
            }
        }
        CommonUtils.handleSufExecuteWait(sufExecuteWait);
        ScreenUtil.pressAndSwipe(driver, pointList, passingPoints);
        return true;
    }
}
