package com.gyee.alarm.service;

import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.ProBasicEquipment;
import com.gyee.alarm.model.auto.ProBasicPowerstation;
import com.gyee.alarm.model.auto.ProBasicSubStation;
import com.gyee.alarm.model.vo.AlarmTag;
import com.gyee.alarm.service.auto.ISysUserService;
import com.gyee.alarm.util.DateUtils;
import com.gyee.alarm.websocket.PushParams;
import com.gyee.alarm.websocket.WebSocket;

import com.gyee.common.model.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.EncodeException;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;


@Service
public class PushDataSevice {
    @Resource
    private ISysUserService sysUserService;
    @Value("${pushinterval}")
    private Double pushinterval;
    @Resource
    private WebSocket webSocket;


    public synchronized void pushWtData(AlarmTag po) throws EncodeException, IOException, InterruptedException {
        ConcurrentHashMap<String, WebSocket> webSocketPushMap = WebSocket.webSocketMap;
        ConcurrentHashMap<String, PushParams> webSocketPushParamsMap = WebSocket.webSocketParamsMap;

        if (!webSocketPushMap.isEmpty()) {
            for (String key : webSocketPushMap.keySet()) {
                // 根据ws连接用户ID获取推送参数


                if (checkWtId(key, po.getDeviceId())) {

                    if(webSocketPushMap.containsKey(key))
                    {
                        webSocket.sendMessageByUserId(key, po);

//                        Thread.sleep(300);
                    }
                }

            }
        }

    }

//    public synchronized void pushBtData(AlarmTag po) throws EncodeException, IOException {
//        ConcurrentHashMap<String, WebSocket> webSocketPushMap = WebSocket.webSocketMap;
//        ConcurrentHashMap<String, WebSocketBt> webSocketPushBtMap = WebSocketBt.webSocketBtMap;
//        ConcurrentHashMap<String, PushParams> webSocketPushParamsMap = WebSocket.webSocketParamsMap;
//
//        ConcurrentHashMap<String, List<String>> btParamsMap = WebSocketBt.btParamsMap;
//
//        if (!webSocketPushMap.isEmpty()) {
//            for (String key : webSocketPushMap.keySet()) {
//                // 根据ws连接用户ID获取推送参数
////                PushParams pushParams = webSocketPushParamsMap.get(key);
//                synchronized (po.getId()) {
//                    if (checkBtId(key, po.getWpId())) {
//                        webSocket.sendMessageByUserId(key, po);
//
//                    }
//                }
//            }
//        }
//
//        if (!webSocketPushBtMap.isEmpty()) {
//
//
//
//
//                if (po.getRank() > 3) {
//
//                    if (btParamsMap.containsKey(po.getWpId())) {
//
//                        List<String> btls = btParamsMap.get(po.getWpId());
//
//                        if (btls.contains(po.getAlarmId())) {
//                            btls.remove(po.getAlarmId());
//                        } else {
//                            btls.add(po.getAlarmId());
//                        }
//                        if (btls.isEmpty()) {
//                            for (String key : webSocketPushBtMap.keySet()) {
//
//                                if (checkBtId(key, po.getWpId())) {
//                                    Map<String, Boolean> map = new HashMap<>();
//                                    map.put(po.getWpId(), false);
//                                    webSocketBt.sendMessageByUserId(key, map);
//                                }
//                            }
//
//                        } else {
//
//                            for (String key : webSocketPushBtMap.keySet()) {
//
//                                if (checkBtId(key, po.getWpId())) {
//                                    Map<String, Boolean> map = new HashMap<>();
//                                    map.put(po.getWpId(), true);
//                                    webSocketBt.sendMessageByUserId(key, map);
//                                }
//                            }
//                        }
//                    } else {
//                        List<String> btls = new ArrayList<>();
//
//                        btls.add(po.getAlarmId());
//
//                        btParamsMap.put(po.getWpId(), btls);
//
//                        if (StringUtils.empty(po.getIsClose())) {
//                            for (String key : webSocketPushBtMap.keySet()) {
//
//
//                                if (checkBtId(key, po.getWpId())) {
//                                    Map<String, Boolean> map = new HashMap<>();
//                                    map.put(po.getWpId(), false);
//                                    webSocketBt.sendMessageByUserId(key, map);
//                                }
//                            }
//
//                        } else {
//                            for (String key : webSocketPushBtMap.keySet()) {
//
//
//                                if (checkBtId(key, po.getWpId())) {
//                                    Map<String, Boolean> map = new HashMap<>();
//                                    map.put(po.getWpId(), true);
//                                    webSocketBt.sendMessageByUserId(key, map);
//                                }
//                            }
//
//                        }
//
//                    }
//                }
//
//            }
//
//
//    }

    public synchronized void pushBtData(AlarmTag po) throws EncodeException, IOException, InterruptedException {
        ConcurrentHashMap<String, WebSocket> webSocketPushMap = WebSocket.webSocketMap;
        ConcurrentHashMap<String, PushParams> webSocketPushParamsMap = WebSocket.webSocketParamsMap;



        if (!webSocketPushMap.isEmpty()) {
            for (String key : webSocketPushMap.keySet()) {
                // 根据ws连接用户ID获取推送参数
//                PushParams pushParams = webSocketPushParamsMap.get(key);
                if (checkBtId(key, po.getWpId())) {
                    webSocket.sendMessageByUserId(key, po);
//                        Thread.sleep(300);
                }
            }
        }

    }

    private boolean checkWtId(String userId, String wtId) {


        boolean result = false;

        if (StringUtils.notEmp(userId)) {
            String[] str = userId.split("_");
            if (str.length == 2) {
                List<String> depls = sysUserService.getUserByuserId(Long.valueOf(str[0]));
                if (!depls.isEmpty()) {
                    String depId = depls.get(0);

                    if (CacheContext.wpwtmap.containsKey(depId)) {

                        List<ProBasicEquipment> wtls = CacheContext.wpwtmap.get(depId);
                        for (ProBasicEquipment wt : wtls) {
                            if (wt.getId().equals(wtId)) {
                                result = true;
                                break;
                            }

                        }

                    } else if (CacheContext.cmwtlsmap.containsKey(depId)) {

                        List<ProBasicEquipment> wtls = CacheContext.cmwtlsmap.get(depId);
                        for (ProBasicEquipment wt : wtls) {
                            if (wt.getId().equals(wtId)) {
                                result = true;
                                break;
                            }

                        }

                    } else if (CacheContext.rgwtlsmap.containsKey(depId)) {

                        List<ProBasicEquipment> wtls = CacheContext.rgwtlsmap.get(depId);
                        for (ProBasicEquipment wt : wtls) {
                            if (wt.getId().equals(wtId)) {
                                result = true;
                                break;
                            }

                        }

                    } else if (depId.equals("0")) {
                        for (ProBasicEquipment wt : CacheContext.wtls) {
                            if (wt.getId().equals(wtId)) {
                                result = true;
                                break;
                            }

                        }

                    }
                }

            }
        }

        return result;
    }


    private boolean checkBtId(String userId, String btId) {


        boolean result = false;

        if (StringUtils.notEmp(userId)) {
            String[] str = userId.split("_");
            if (str.length == 2) {
                List<String> depls = sysUserService.getUserByuserId(Long.valueOf(str[0]));
                if (!depls.isEmpty()) {
                    String depId = depls.get(0);

                    if (CacheContext.wpmap.containsKey(depId)) {

                        ProBasicPowerstation wp = CacheContext.wpmap.get(depId);

                        if (CacheContext.subwpmap.containsKey(btId)) {
                            ProBasicSubStation sub = CacheContext.subwpmap.get(btId);
                            if (sub.getWindpowerstationId().equals(wp.getId())) {
                                result = true;
                            }
                        }

                    } else if (CacheContext.cpwpmap.containsKey(depId)) {

                        List<ProBasicPowerstation> wpls = CacheContext.cpwpmap.get(depId);
                        for (ProBasicPowerstation wp : wpls) {
                            if (CacheContext.subwpmap.containsKey(btId)) {
                                ProBasicSubStation sub = CacheContext.subwpmap.get(btId);
                                if (sub.getWindpowerstationId().equals(wp.getId())) {
                                    result = true;
                                    break;
                                }
                            }

                        }

                    } else if (CacheContext.rgwpmap.containsKey(depId)) {

                        List<ProBasicPowerstation> wpls = CacheContext.rgwpmap.get(depId);
                        for (ProBasicPowerstation wp : wpls) {
                            if (CacheContext.subwpmap.containsKey(btId)) {
                                ProBasicSubStation sub = CacheContext.subwpmap.get(btId);
                                if (sub.getWindpowerstationId().equals(wp.getId())) {
                                    result = true;
                                    break;
                                }
                            }

                        }

                    } else if (depId.equals("0")) {
                        result = true;

                    }
                }
            }
        }


        return result;
    }
}
