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

import net.northking.cctp.upperComputer.deviceManager.IOSDeviceManager;
import net.northking.cctp.upperComputer.deviceManager.thread.IosDeviceInitThread;
import net.northking.cctp.upperComputer.driver.ios.command.data.UiNodeData;
import net.northking.cctp.upperComputer.utils.SessionUtils;
import net.northking.cctp.upperComputer.webSocket.entity.CmdRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.Session;

/**
 * @author : yineng.huang
 * @date : 2024/4/25 16:22
 */
public class GetNodeTreeThread extends Thread {

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

    private Session session;

    private String deviceId;

    private CmdRequest request;

    private boolean keepPushNode = false;

    private final Object lock = new Object();


    public GetNodeTreeThread(Session session, String deviceId, CmdRequest request) {
        this.session = session;
        this.deviceId = deviceId;
        this.request = request;
        this.keepPushNode = true;
        setName(deviceId + "】node节点");
    }

    @Override
    public void run() {
        logger.info("开始获取设备【{}】的节点............................", deviceId);
        while (!isInterrupted()) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                interrupt();
                break;
            }
            try {
                IosDeviceInitThread iosInitThread = IOSDeviceManager.getInstance().getIosInitThread(deviceId);
                if (null == iosInitThread || iosInitThread.isInterrupted() || !iosInitThread.isAlive()) {
                    logger.debug("设备【{}】掉线了,等一下，不要急........................", deviceId);
                    continue;
                }
                logger.debug("开始获取......");
                UiNodeData uiNodeData = iosInitThread.getNkAgent().uiTreeNode();
                logger.debug("获取结束，nodeData空：{}", uiNodeData == null);
                SessionUtils.sendContinuousData(session, request, uiNodeData, "获取节点树成功");
            } catch (IllegalStateException e) {
                logger.warn("webSession已经关闭",e);
                break;
            } catch (Exception e) {
                logger.warn("设备【{}】获取节点出了点问题，但是不影响，继续", deviceId, e);
            }
            if (!keepPushNode) {
                synchronized (lock) {
                    logger.warn("设备【{}】等待获取node节点.....................", deviceId);
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        interrupt();
                        break;
                    }
                }
            }
        }
        logger.info("设备【{}】获取node节点线程退出...............", deviceId);
    }

    public CmdRequest getRequest() {
        return request;
    }

    public void setRequest(CmdRequest request) {
        this.request = request;
    }


    public void stopGetNodeTree(CmdRequest request) {
        SessionUtils.sendSuccessData(session, this.request, null, "即将停止获取节点树........");
        SessionUtils.sendSuccessData(session, request, null, "停止获取节点树成功........");
        this.keepPushNode = false;
    }

    public void startGetNodeTree(CmdRequest request) {
        this.request = request;
        this.keepPushNode = true;
        synchronized (lock) {
            logger.debug("设备【{}】开始获取节点树...................", deviceId);
            lock.notify();
        }
    }


    public void exitGetNodeTree() {
        interrupt();
    }
}
