package com.ics.server;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.boyue.changyangoffice.domain.DeviceInfo;
//import com.boyue.changyangoffice.domain.R;
//import com.boyue.changyangoffice.entity.Equipment;
//import com.boyue.changyangoffice.entity.Panel;
//import com.boyue.changyangoffice.entity.PanelEquipment;
//import com.boyue.changyangoffice.service.IEquipmentService;
//import com.boyue.changyangoffice.service.IPanelEquipmentService;
//import com.boyue.changyangoffice.service.IPanelService;
//import com.boyue.changyangoffice.utils.DeviceUtils;
import com.ics.common.core.domain.R;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassDescription: websocket服务端
 * @JdkVersion: 1.8
 * @Author: 李白
 * @Created: 2023/8/31 14:59
 */
@Slf4j
@Component
@RestController
@ServerEndpoint(value = "/websocket-server")
//@ServerEndpoint("/")
public class WsServer {


    private Session session;

//    @Autowired
//    private IPanelEquipmentService panelEquipmentService;
//
//    @Autowired
//    private IEquipmentService equipmentService;
//
//    @Autowired
//    private IPanelService panelService;


    /**
     * 记录在线连接客户端数量
     */
    private static AtomicInteger onlineCount = new AtomicInteger(0);
    /**
     * 存放每个连接进来的客户端对应的websocketServer对象，用于后面群发消息
     */
    private static CopyOnWriteArrayList<WsServer> wsServers = new CopyOnWriteArrayList<>();


    /**
     * 服务端与客户端连接成功时执行
     *
     * @param session 会话
     */
    @OnOpen
    public void onOpen(Session session) {
        //将这次会话信息记录

        this.session = session;

        //接入的客户端+1
        int count = onlineCount.incrementAndGet();
        //集合中存入客户端对象+1
        wsServers.add(this);
        log.info("与客户端连接成功，当前连接的客户端数量为：{}", count);
    }

    /**
     * 收到客户端的消息时执行
     *
     * @param message 消息
     * @param session 会话
     */
    @OnMessage
    public void onMessage(String message, Session session) {
        log.info("收到来自客户端的消息，客户端地址：{}，消息内容：{}", session.getMessageHandlers(), message);
        //业务逻辑，对消息的处理
//        sendMessageToAll("群发消息的内容");
    }

    /**
     * 连接发生报错时执行
     *
     * @param session   会话
     * @param throwable 报错
     */
    @OnError
    public void onError(Session session, @NonNull Throwable throwable) {
        log.error("连接发生报错");
        throwable.printStackTrace();
    }

    /**
     * 连接断开时执行
     */
    @OnClose
    public void onClose() {
        //接入客户端连接数-1
        int count = onlineCount.decrementAndGet();
        //集合中的客户端对象-1
        wsServers.remove(this);
        log.info("服务端断开连接，当前连接的客户端数量为：{}", count);
    }


    /**
     * 向客户端推送消息
     *
     * @param message 消息
     */
    public void sendMessage(String message) {

        this.session.getAsyncRemote().sendText(message);
        log.info("推送消息给客户端:{}，消息内容为：{}", this.session.getMessageHandlers(), message);
    }

//    @PostMapping("/device/pushDeviceInfo")
//    public R pushDeviceInfo(@RequestBody DeviceInfo deviceInfo) throws IOException {
//
//        if (null != deviceInfo) {
//            log.info("接收到的设备信息：{}", deviceInfo);
//            String beanToString = JSON.toJSONString(deviceInfo);
//            System.out.println("接收到的设备信息：" + beanToString);
//
//
//            sendMessageToAll(beanToString);
//
//            //如果类型是 情景面板的
//            JSONObject jsonObject = JSONObject.parseObject(beanToString);
//            String pushMsgType = (String) jsonObject.get("pushMsgType");
//
//            if (pushMsgType.equals("ShortcutPanelActivedEvent")) {
//                //如果类型是 情景面板的 去查询数据库的情景面板id 如果匹配，查询对应的窗帘 进行开关。
//                //获取 按钮id 和面板id
//                JSONObject pushMsg = (JSONObject) jsonObject.get("pushMsg");
//                Integer panelId = (Integer) pushMsg.get("id");
//                String buttonId = (String) pushMsg.get("buttonId");
//
//                QueryWrapper<Panel> wrapper1 = new QueryWrapper<Panel>();
//                wrapper1.eq("panel_id", panelId);
//                wrapper1.eq("button_id", buttonId);
//
//                Panel panel = panelService.getOne(wrapper1);
//                if (null != panel) {
//                    //根据面板id获取对应的设备id
//                    QueryWrapper<PanelEquipment> wrapper = new QueryWrapper<>();
//                    wrapper.eq("panel_id", panel.getId());
//                    List<PanelEquipment> list = panelEquipmentService.list(wrapper);
//                    if (CollUtil.isNotEmpty(list)) {
//                        //如果设备id不等于空
//                        for (PanelEquipment panelEquipment : list) {
//                            Equipment equipment = equipmentService.getById(panelEquipment.getEquipmentId());
//                            if (null != equipment) {
//                                Boolean aBoolean = DeviceUtils.openCover(equipment.getEquipmentNum(), panelEquipment.getType());
//                            }
//                        }
//                    }
//                }
//
//            }
//
//
//        }
//
//
//        //控克 灯
//
//
//        return R.ok();
//
//    }


    /**
     * 群发消息
     *
     * @param message 消息
     */
    public void sendMessageToAll(String message) {
        CopyOnWriteArrayList<WsServer> ws = wsServers;
        for (WsServer wsServer : ws) {
            wsServer.sendMessage(message);
        }
    }

//    @PostMapping("/send2AllC")
//    public void sendMessageToAll1(@RequestBody String message){
//        CopyOnWriteArrayList<WsServer> ws = wsServers;
//        for (WsServer wsServer : ws){
//            wsServer.sendMessage(message);
//        }
//    }


}

