package net.northking.cctp.upperComputer.deviceManager.screen;

import net.northking.cctp.upperComputer.constants.ResponseCmd;
import net.northking.cctp.upperComputer.deviceManager.HarmonyDeviceManager;
import net.northking.cctp.upperComputer.deviceManager.thread.HarmonyProvider;
import net.northking.cctp.upperComputer.driver.harmony.HarmonyDevice;
import net.northking.cctp.upperComputer.driver.harmony.hyppium.HyppiumAgent;
import net.northking.cctp.upperComputer.utils.SessionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import javax.websocket.Session;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author : yineng.huang
 * @date : 2024/10/22 17:03
 */
public class HarmonyScreenResponseThread extends ImageScreenResponse implements HyppiumAgent.OnCaptureData{

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

    private HarmonyDevice harmonyDevice;

    public HarmonyScreenResponseThread(Session session, HarmonyDevice harmonyDevice){
        this.webSessions.add(session);
        this.harmonyDevice = harmonyDevice;
        this.deviceId = harmonyDevice.getHdcDevice().getConnectKey();
    }
    public HarmonyScreenResponseThread(HarmonyDevice harmonyDevice){
        this.harmonyDevice = harmonyDevice;
        this.deviceId = harmonyDevice.getHdcDevice().getConnectKey();
    }


    @Override
    public void changeScreenSize() {

    }

    @Override
    public void stopFetchPic() {
        HarmonyProvider provider = HarmonyDeviceManager.getInstance().getCurrentDeviceProvider(this.deviceId);
        if (null != provider) {
            boolean success = provider.stopCaptureScreenImageStream();
            logger.info("停止获取手机【{}】的屏幕的结果：{}", deviceId, success);
        }else {
            logger.info("未获取到手机【{}】的provider", deviceId);
        }
        HarmonyDeviceManager.getInstance().removeScreenThread(deviceId);
        this.harmonyDevice = null;
    }

    @Override
    public void onCaptureData(byte[] data) {
        this.lastData = data;
        //有后端会话则给后端推送屏幕图片
        if (!CollectionUtils.isEmpty(webSessions)) {
            Iterator<Session> iterator = webSessions.iterator();
            while (iterator.hasNext()) {
                Session webSession = iterator.next();
                if (webSession.isOpen()) {
                    if (sendDeviceStatus) {
                        Map<String, Object> result = new HashMap<>();
                        result.put(ResponseCmd.DeviceStatus.STATUS, ResponseCmd.DeviceStatus.CONNECTED);
                        SessionUtils.sendMessageInitiative(webSession, ResponseCmd.DEVICE_STATUS, deviceId, result, "设备连接失败");
                        sendDeviceStatus = false;
                    }
                    SessionUtils.sendBinary(webSession, this.lastData);
                } else {
                    logger.warn("推送到web端的session已经断开，sessionId:{}",webSession.getId());
                    iterator.remove();
                }
            }
        }
        if (null != screenRecorder) {
            screenRecorder.addImgToVideo(this.lastData);
        }
    }
}
