package net.northking.cctp.upperComputer.webSocket.function;

import net.northking.cctp.upperComputer.constants.HandCommand;
import net.northking.cctp.upperComputer.deviceManager.IOSDeviceManager;
import net.northking.cctp.upperComputer.deviceManager.thread.IosDeviceInitThread;
import net.northking.cctp.upperComputer.driver.ios.NKAgent;
import net.northking.cctp.upperComputer.driver.ios.command.data.DragXYData;
import net.northking.cctp.upperComputer.driver.ios.command.data.ForceTapXYData;
import net.northking.cctp.upperComputer.driver.ios.command.data.TypeKeysUiNodeData;
import net.northking.cctp.upperComputer.driver.ios.packet.EmptyCommandData;
import net.northking.cctp.upperComputer.exception.ExecuteException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author : yineng.huang
 * @date : 2024/7/3 16:43
 */
public class IosFunctionByAgent {

    private String deviceId;

    private final Logger logger = LoggerFactory.getLogger(IosFunctionByAgent.class);

    public IosFunctionByAgent(String deviceId){
        this.deviceId = deviceId;
    }

    /**
     * 点击屏幕
     *
     * @param x
     * @param y
     * @return
     */
    public boolean click(Float x, Float y) {
        NKAgent nkAgent = getDeviceNkAgent();
        logger.debug("请求点击屏幕的位置x：{}   y：{}", x,y);
        ForceTapXYData forceTapXYData = new ForceTapXYData();
        forceTapXYData.setX(x.intValue());
        forceTapXYData.setY(y.intValue());
        boolean success = nkAgent.handleTextUiNode(forceTapXYData, HandCommand.CLICK.getCode());
        logger.debug("设备【{}】点击成功?------->{}",deviceId,success);
        return success;
    }

    /**
     * 点击屏幕
     *
     * @param x
     * @param y
     * @return
     */
    public boolean longPress(Float x, Float y) {
        NKAgent nkAgent = getDeviceNkAgent();
        ForceTapXYData forceTapXYData = new ForceTapXYData();
        forceTapXYData.setX(x.intValue());
        forceTapXYData.setY(y.intValue());
        forceTapXYData.setDuration(1500);
        forceTapXYData.setPressure(20);
        logger.debug("请求长按屏幕的位置x：{}   y：{}", x,y);
        boolean success = nkAgent.handleTextUiNode(forceTapXYData, HandCommand.CLICK.getCode());
        logger.debug("设备【{}】长按成功?------->{}",deviceId,success);
        return success;
    }

    /**
     * 滑动
     * @param startX
     * @param startY
     * @param endX
     * @param endY
     * @return
     */
    public boolean swipe(Float startX, Float startY,Float endX,Float endY) {
        NKAgent nkAgent = getDeviceNkAgent();
        DragXYData dragXYData = new DragXYData();
        dragXYData.setX(startX.intValue());
        dragXYData.setToX(endX.intValue());
        dragXYData.setY(startY.intValue());
        dragXYData.setToY(endY.intValue());
        dragXYData.setDuration(0);
        boolean success = nkAgent.handleTextUiNode(dragXYData, HandCommand.SWIPE.getCode());
        logger.debug("设备【{}】滑动成功?------->{}",deviceId,success);
        return success;
    }

    /**
     * 点击home键
     * @return
     */
    public boolean pressHome() {
        NKAgent nkAgent = getDeviceNkAgent();
        EmptyCommandData emptyCommandData = new EmptyCommandData();
        boolean success = nkAgent.handleTextUiNode(emptyCommandData, HandCommand.PRESS_HOME.getCode());
        logger.debug("设备【{}】点击home成功?------->{}",deviceId,success);
        return success;
    }

    /**
     * 输入文本
     * @param text
     * @return
     */
    public boolean inputText(String text,boolean clear) {
        NKAgent nkAgent = getDeviceNkAgent();
        logger.debug("输入文本：{}，是否清空：{}", text, clear);
        if (clear) {
            logger.debug("清空输入......");
        }
        TypeKeysUiNodeData typeKeysUiNodeData = new TypeKeysUiNodeData();
        typeKeysUiNodeData.setText(text);
        typeKeysUiNodeData.setFrequency(70);
        boolean success = nkAgent.handleTextUiNode(typeKeysUiNodeData, HandCommand.INPUT.getCode());
        return success;
    }

    /**
     * 删除一个文字
     * @return
     */
    public boolean deleteOneKey() {
        NKAgent nkAgent = getDeviceNkAgent();
        boolean success = nkAgent.handleDeleteTextUiNode(HandCommand.DELETE_ONE_KEY.getCode());
        return success;
    }

    private NKAgent getDeviceNkAgent(){
        IosDeviceInitThread iosInitThread = IOSDeviceManager.getInstance().getIosInitThread(deviceId);
        if (null == iosInitThread || !iosInitThread.isAlive() || iosInitThread.isInterrupted()) {
            throw new ExecuteException(String.format("手机【%s】还没准备好，待会再试",deviceId));
        }
        NKAgent nkAgent = iosInitThread.getNkAgent();
        if (null == nkAgent) {
            throw new ExecuteException(String.format("手机【%s】还没准备好，待会再试",deviceId));
        }
        return nkAgent;
    }
}
