package com.gyee.alarm.service;

import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.ProBasicEquipment;
import com.gyee.alarm.model.auto.ProEconAlarmConfiguration;
import com.gyee.alarm.model.auto.ProEconAlarmRule;
import com.gyee.alarm.model.vo.AlarmCustomType;
import com.gyee.alarm.model.vo.AlarmTag;
import com.gyee.alarm.model.vo.AlarmTypeValue;
import com.gyee.alarm.model.vo.AlarmVo;
import com.gyee.alarm.service.auto.IProEconAlarmConfigurationService;
import com.gyee.alarm.service.auto.IProEconAlarmRuleService;
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 org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.websocket.EncodeException;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;


@Service
public class ReloadVoSevice {

    @Resource
    private AlarmWtService alarmWtService;
    @Resource
    private AlarmInService alarmInService;
    @Resource
    private AlarmBtService alarmBtService;
    @Resource
    private IProEconAlarmRuleService proEconAlarmRuleService;
    @Resource
    private AlarmCtService alarmCtService;
    @Resource
    private IProEconAlarmConfigurationService proEconAlarmConfigurationService;
    public synchronized void reload() throws Exception {


        Map<String, List<ProEconAlarmRule>> alarmRulesMap = new HashMap<>();
        List<ProEconAlarmConfiguration> inalarmls = new ArrayList<>();
        List<ProEconAlarmConfiguration> btalarmls = new ArrayList<>();
        List<ProEconAlarmConfiguration> wtalarmls = new ArrayList<>();
         Map<String,List<ProEconAlarmConfiguration>> wtAlarmMap=new HashMap<>();
         List<AlarmVo> alarmWtList=new ArrayList<>();
         Map<String,AlarmVo> alarmWtMap=new HashMap<>();
         List<AlarmVo> alarmInList=new ArrayList<>();
         Map<String,AlarmVo> alarmInMap=new HashMap<>();
         List<AlarmVo> alarmBtList=new ArrayList<>();
         Map<String,AlarmVo> alarmBtMap=new HashMap<>();
         List<AlarmVo> alarmCtList=new ArrayList<>();
         Map<String,AlarmVo> alarmCtMap=new HashMap<>();


        List<ProEconAlarmRule> customls =proEconAlarmRuleService.list().stream().filter(i -> i.getEnable()).collect(Collectors.toList());

        List<ProEconAlarmRule> wtCustomls=new ArrayList<>();
        List<ProEconAlarmRule> inCustomls=new ArrayList<>();
        List<ProEconAlarmRule> btCustomls=new ArrayList<>();
        if(!customls.isEmpty())
        {
            for(ProEconAlarmRule vo: customls)
            {
                if(vo.getCategory().equals(AlarmCustomType.WT.getCode()))
                {
                    wtCustomls.add(vo);
                }else  if(vo.getCategory().equals(AlarmCustomType.IN.getCode()))
                {
                    inCustomls.add(vo);
                }else  if(vo.getCategory().equals(AlarmCustomType.Bt.getCode()))
                {
                    btCustomls.add(vo);
                }

            }
        }
        alarmRulesMap.put(AlarmCustomType.Bt.getCode(),btCustomls);
        alarmRulesMap.put(AlarmCustomType.WT.getCode(),wtCustomls);
        alarmRulesMap.put(AlarmCustomType.IN.getCode(),inCustomls);


        List<ProEconAlarmConfiguration> templs = proEconAlarmConfigurationService.list().stream().filter(i -> i.getEnable()).collect(Collectors.toList());
        if (!templs.isEmpty()) {
            for (ProEconAlarmConfiguration alarm : templs) {
                if (alarm.getAlarmType().equals(AlarmTypeValue.WT.getCode())) {
                    wtalarmls.add(alarm);
                    StringBuilder sb=new StringBuilder();
                    sb.append(alarm.getStationId()).append(alarm.getModelId());

                    if(wtAlarmMap.containsKey(String.valueOf(sb)))
                    {
                        List<ProEconAlarmConfiguration> ls=wtAlarmMap.get(String.valueOf(sb));
                        ls.add(alarm);
                    }else
                    {
                        List<ProEconAlarmConfiguration> ls=new ArrayList<>();
                        ls.add(alarm);
                        wtAlarmMap.put(String.valueOf(sb),ls);
                    }
                } else if (alarm.getAlarmType().equals(AlarmTypeValue.BT.getCode())) {
                    btalarmls.add(alarm);

                }
                else if (alarm.getAlarmType().equals(AlarmTypeValue.IN.getCode())) {
                    inalarmls.add(alarm);

                }
            }
        }

        //风机报警初始化标签
        alarmWtList=alarmWtService.initalAlarmVo();
        if(!alarmWtList.isEmpty())
        {
            for(AlarmVo vo: alarmWtList)
            {
                alarmWtMap.put(vo.getTbName(),vo);
            }
        }
        //逆变器报警初始化标签
        alarmInList=alarmInService.initalAlarmVo();
        if(!alarmInList.isEmpty())
        {
            for(AlarmVo vo: alarmInList)
            {
                alarmInMap.put(vo.getTbName(),vo);
            }
        }


        //升压站报警初始化标签
        alarmBtList=alarmBtService.initalAlarmVo();
        if(!alarmBtList.isEmpty())
        {
            for(AlarmVo vo: alarmBtList)
            {
                alarmBtMap.put(vo.getTbName(),vo);
            }
        }
        //自定义报警初始化标签
        alarmCtList=alarmCtService.initalAlarmVo();
        if(!alarmCtList.isEmpty())
        {
            for(AlarmVo vo: alarmCtList)
            {
                alarmCtMap.put(vo.getTbName(),vo);
            }
        }

        CacheContext.alarmRulesMap =alarmRulesMap;
        CacheContext.inalarmls = inalarmls;
        CacheContext.btalarmls =  btalarmls;
        CacheContext.wtalarmls = wtalarmls;
        CacheContext.wtAlarmMap=wtAlarmMap;
        CacheContext.alarmWtList=alarmWtList;
        CacheContext.alarmWtMap=alarmWtMap;
        CacheContext.alarmInList=alarmInList;
        CacheContext.alarmInMap=alarmInMap;
        CacheContext.alarmBtList=alarmBtList;
        CacheContext.alarmBtMap=alarmBtMap;
        CacheContext.alarmCtList=alarmCtList;
        CacheContext.alarmCtMap=alarmCtMap;
    }


}
