package com.fizz.business.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.fizz.business.constants.CommonConstants;
import com.fizz.business.constants.enums.WsTypeEnum;
import com.fizz.business.domain.msg.AppMeasureEntryMessage;
import com.fizz.business.domain.msg.AppMeasureMessage;
import com.fizz.business.dto.CoilPositionDTO;
import com.fizz.business.dto.L1CoilLineMeasureDTO;
import com.fizz.business.dto.MatmapDTO;
import com.fizz.business.dto.WsSignalMsgDTO;
import com.fizz.business.form.L1OperateMatForm;
import com.fizz.business.form.WebOperateMatForm;
import com.fizz.business.service.client.TrackWsHandler;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.List;
import java.util.Map;


@Slf4j
@Component
public class WebSocketUtil {

    private static TrackWsHandler trackWsHandler;

    @Autowired
    public void setTrackWsHandler(TrackWsHandler trackWsHandler) {
        WebSocketUtil.trackWsHandler = trackWsHandler;
    }

    public static void sendMessage(WsTypeEnum type, String text) {
        Map<String, WebSocketSession> clients = trackWsHandler.getClients().getOrDefault(type.name(), Maps.newConcurrentMap());
        TextMessage message = new TextMessage(text);
        clients.values().forEach(s -> {
            try {
                s.sendMessage(message);
//                log.info("[websocket]向客户端[{}]推送消息:{}", type + "-" + s.getId(), text);
            } catch (IOException e) {
                log.error("[websocket]向客户端[{}]推送消息异常:{}", type + "-" + s.getId(), text);
            }
        });
    }


    public static void sendSignalMsg(WebOperateMatForm form) {
        sendSignalMsg(form, false);
    }

    public static void sendSignalMsg(WebOperateMatForm form, boolean virtualPlanFlag) {
        WsSignalMsgDTO msg = new WsSignalMsgDTO();
        BeanUtil.copyProperties(form, msg, "operation");
        msg.setOperation(form.getOperation().name());
        msg.setAutoFlag(CommonConstants.OPERATE_FROM_WEB);
        msg.setVirtualPlanFlag(virtualPlanFlag);
        sendMessage(WsTypeEnum.track_signal, JSON.toJSONString(msg));
    }

    public static void sendSignalMsg(L1OperateMatForm form) {
        WsSignalMsgDTO msg = new WsSignalMsgDTO();
        BeanUtil.copyProperties(form, msg, "operation");
        msg.setOperation(form.getOperation().name());
        sendMessage(WsTypeEnum.track_signal, JSON.toJSONString(msg));
    }
    public static void sendPositionMsg(CoilPositionDTO positionDTO) {
        sendMessage(WsTypeEnum.track_position, JSONUtil.toJsonStr(positionDTO));
    }

    public static void sendMeasureMsg(L1CoilLineMeasureDTO measureDTO) {


        sendMessage(WsTypeEnum.track_measure, JSONUtil.toJsonStr(measureDTO));
    }

    public static void sendMeasureMsg(AppMeasureMessage measureDTO) {


        sendMessage(WsTypeEnum.track_measure, JSONUtil.toJsonStr(measureDTO));
    }


    public static void sendMatmapMsg() {
        List<MatmapDTO> list = MatmapUtil.getMatmapList();
        if (CollUtil.isEmpty(list)) return;
        sendMessage(WsTypeEnum.track_matmap, JSONUtil.toJsonStr(list));
    }
}
