package com.gyee.alarm.service;

import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.*;
import com.gyee.alarm.model.vo.*;
import com.gyee.alarm.service.auto.IAlarmTsService;
import com.gyee.alarm.service.auto.IProEconAlarmInfoService;
import com.gyee.alarm.util.DateUtils;
import com.gyee.alarm.util.StringUtils;

import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

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

@Service

public class AlarmCtService {
    @Resource
    private IAlarmTsService alarmTsService;

    @Resource
    private IProEconAlarmInfoService proEconAlarmInfoService;


    @Resource
    private TokenService tokenService;

    public List<AlarmVo> initalAlarmVo() {

        List<AlarmVo> alarmVoList = new ArrayList<>();
        Map<String, List<ProEconAlarmRule>> alarmRulesMap = CacheContext.alarmRulesMap;

        //风机自定义报警
        wtCustomAlarm(alarmVoList, alarmRulesMap);
        //逆变器自定义报警
        lnCustomAlarm(alarmVoList, alarmRulesMap);
//        //场站自定义报警
//        wpCustomAlarm(alarmVoList, alarmRulesMap);
        //升压站自定义报警
        btCustomAlarm(alarmVoList, alarmRulesMap);
        return alarmVoList;
    }

    public List<AlarmVo> initalAlarmVo(ProEconAlarmRule ar) {

        List<AlarmVo> alarmVoList = new ArrayList<>();

        Map<String, List<ProEconAlarmRule>> armap = new HashMap<>();

            StringBuilder sbs = new StringBuilder();
            sbs.append(ar.getStationId() + ar.getModelId());
            if (armap.containsKey(String.valueOf(sbs))){
                List<ProEconAlarmRule> ls = armap.get(String.valueOf(sbs));
                ls.add(ar);
            } else {
                List<ProEconAlarmRule> ls = new ArrayList<>();
                ls.add(ar);
                armap.put(String.valueOf(sbs), ls);
            }


        for (ProBasicEquipment wt : CacheContext.wtls) {
            StringBuilder sb = new StringBuilder();
            sb.append(wt.getWindpowerstationId() + wt.getModelId());

            if (armap.containsKey(String.valueOf(sb))) {
                List<ProEconAlarmRule> alarmls = armap.get(String.valueOf(sb));

                if (!alarmls.isEmpty()) {
                    for (ProEconAlarmRule alarm : alarmls) {
                        AlarmVo vo = new AlarmVo();
                        StringBuilder tbName = new StringBuilder();
                        tbName.append(wt.getId()).append("_").append(alarm.getId());
                        vo.setTbName(String.valueOf(tbName));
                        vo.setAlarmId(alarm.getId());
                        vo.setTagid(alarm.getExpression());
                        vo.setAlarmType(AlarmTypeValue.CT.getCode());
                        vo.setCharacteristic(alarm.getName());
                        vo.setComponents(alarm.getRelatedParts());
                        vo.setDescription(alarm.getDescription());
                        vo.setDeviceId(wt.getId());
                        vo.setDevicename(wt.getName());
                        vo.setDeviceType(DeviceTypeValue.WT.getCode());
                        vo.setEnabled(alarm.getEnable());
                        vo.setLineid(wt.getLineId());
                        if (CacheContext.lnmap.containsKey(wt.getLineId())) {
                            ProBasicLine ln = CacheContext.lnmap.get(wt.getLineId());
                            vo.setLinename(ln.getName());
                        }
                        vo.setModelId(wt.getModelId());
                        vo.setProjectid(wt.getProjectId());
                        if (CacheContext.pjmap.containsKey(wt.getProjectId())) {
                            ProBasicProject pj = CacheContext.pjmap.get(wt.getProjectId());
                            vo.setProjectname(pj.getName());
                        }
                        vo.setRank(alarm.getRank());
                        vo.setResettable(false);
                        vo.setStationid(wt.getWindpowerstationId());
                        if (CacheContext.wpmap.containsKey(wt.getWindpowerstationId())) {
                            ProBasicPowerstation wp = CacheContext.wpmap.get(wt.getWindpowerstationId());
                            vo.setStationname(wp.getName());
                        }
                        vo.setSubcomponents("");
                        vo.setSuffix("");
                        vo.setTriggertype(1);
                        vo.setUniformcode("");
                        vo.setSuperTableName(AlarmSuperTalbeType.CT.getCode());
                        vo.setVal(0);

                        vo.setCharacteristic(alarm.getName());
                        vo.setFaultCause(alarm.getDescription());
                        vo.setResolvent(alarm.getExpression());
                        alarmVoList.add(vo);
                    }
                }
            }
        }

        return alarmVoList;
    }

    private void wtCustomAlarm(List<AlarmVo> alarmVoList, Map<String, List<ProEconAlarmRule>> alarmRulesMap) {

        Map<String, List<ProEconAlarmRule>> armap = new HashMap<>();

        if (alarmRulesMap.containsKey(DeviceTypeValue.WT.getCode())) {
            List<ProEconAlarmRule> arls = alarmRulesMap.get(DeviceTypeValue.WT.getCode());
            if (!arls.isEmpty()) {
                for (ProEconAlarmRule ar : arls) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(ar.getStationId() + ar.getModelId());
                    if (armap.containsKey(String.valueOf(sb))) {
                        List<ProEconAlarmRule> ls = armap.get(String.valueOf(sb));
                        ls.add(ar);
                    } else {
                        List<ProEconAlarmRule> ls = new ArrayList<>();
                        ls.add(ar);
                        armap.put(String.valueOf(sb), ls);
                    }
                }
            }
        }

        for (ProBasicEquipment wt : CacheContext.wtls) {
            StringBuilder sb = new StringBuilder();
            sb.append(wt.getWindpowerstationId() + wt.getModelId());

            if (armap.containsKey(String.valueOf(sb))) {
                List<ProEconAlarmRule> alarmls = armap.get(String.valueOf(sb));

                if (!alarmls.isEmpty()) {
                    for (ProEconAlarmRule alarm : alarmls) {
                        AlarmVo vo = new AlarmVo();
                        StringBuilder tbName = new StringBuilder();
                        tbName.append(wt.getId()).append("_").append(alarm.getId());
                        vo.setTbName(String.valueOf(tbName));
                        vo.setAlarmId(alarm.getId());
                        vo.setTagid(alarm.getExpression());
                        vo.setAlarmType(AlarmTypeValue.CT.getCode());
                        vo.setCharacteristic(alarm.getName());
                        vo.setComponents(alarm.getRelatedParts());
                        vo.setDescription(alarm.getDescription());
                        vo.setDeviceId(wt.getId());
                        vo.setDevicename(wt.getName());
                        vo.setDeviceType(DeviceTypeValue.WT.getCode());
                        vo.setEnabled(alarm.getEnable());
                        vo.setLineid(wt.getLineId());
                        if (CacheContext.lnmap.containsKey(wt.getLineId())) {
                            ProBasicLine ln = CacheContext.lnmap.get(wt.getLineId());
                            vo.setLinename(ln.getName());
                        }
                        vo.setModelId(wt.getModelId());
                        vo.setProjectid(wt.getProjectId());
                        if (CacheContext.pjmap.containsKey(wt.getProjectId())) {
                            ProBasicProject pj = CacheContext.pjmap.get(wt.getProjectId());
                            vo.setProjectname(pj.getName());
                        }
                        vo.setRank(alarm.getRank());
                        vo.setResettable(false);
                        vo.setStationid(wt.getWindpowerstationId());
                        if (CacheContext.wpmap.containsKey(wt.getWindpowerstationId())) {
                            ProBasicPowerstation wp = CacheContext.wpmap.get(wt.getWindpowerstationId());
                            vo.setStationname(wp.getName());
                        }
                        vo.setSubcomponents("");
                        vo.setSuffix("");
                        vo.setTriggertype(1);
                        vo.setUniformcode("");
                        vo.setSuperTableName(AlarmSuperTalbeType.CT.getCode());
                        vo.setVal(0);

                        vo.setCharacteristic(alarm.getName());
                        vo.setFaultCause(alarm.getDescription());
                        vo.setResolvent(alarm.getExpression());
                        alarmVoList.add(vo);
                    }
                }
            }
        }
    }

    private void lnCustomAlarm(List<AlarmVo> alarmVoList, Map<String, List<ProEconAlarmRule>> alarmRulesMap) {

        Map<String, List<ProEconAlarmRule>> armap = new HashMap<>();

        if (alarmRulesMap.containsKey(DeviceTypeValue.IN.getCode())) {
            List<ProEconAlarmRule> arls = alarmRulesMap.get(DeviceTypeValue.IN.getCode());
            if (!arls.isEmpty()) {
                for (ProEconAlarmRule ar : arls) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(ar.getStationId() + ar.getModelId());
                    if (armap.containsKey(String.valueOf(sb))) {
                        List<ProEconAlarmRule> ls = armap.get(String.valueOf(sb));
                        ls.add(ar);
                    } else {
                        List<ProEconAlarmRule> ls = new ArrayList<>();
                        ls.add(ar);
                        armap.put(String.valueOf(sb), ls);
                    }
                }
            }
        }
//
//        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
//        List<ProBasicEquipment> wtls = tokenService.getWtls(request);
        for (ProBasicEquipment wt : CacheContext.wtls) {
            StringBuilder sb = new StringBuilder();
            sb.append(wt.getWindpowerstationId() + wt.getModelId());

            if (armap.containsKey(String.valueOf(sb))) {
                List<ProEconAlarmRule> alarmls = armap.get(String.valueOf(sb));

                if (!alarmls.isEmpty()) {
                    for (ProEconAlarmRule alarm : alarmls) {
                        AlarmVo vo = new AlarmVo();
                        StringBuilder tbName = new StringBuilder();
                        tbName.append(wt.getId()).append("_").append(alarm.getId());
                        vo.setTbName(String.valueOf(tbName));
                        vo.setAlarmId(alarm.getId());
                        vo.setTagid(alarm.getExpression());
                        vo.setAlarmType(AlarmTypeValue.CT.getCode());
                        vo.setCharacteristic(alarm.getName());
                        vo.setComponents(alarm.getRelatedParts());
                        vo.setDescription(alarm.getDescription());
                        vo.setDeviceId(wt.getId());
                        vo.setDevicename(wt.getName());
                        vo.setDeviceType(DeviceTypeValue.IN.getCode());
                        vo.setEnabled(alarm.getEnable());
                        vo.setLineid(wt.getLineId());
                        if (CacheContext.lnmap.containsKey(wt.getLineId())) {
                            ProBasicLine ln = CacheContext.lnmap.get(wt.getLineId());
                            vo.setLinename(ln.getName());
                        }
                        vo.setModelId(wt.getModelId());
                        vo.setProjectid(wt.getProjectId());
                        if (CacheContext.pjmap.containsKey(wt.getProjectId())) {
                            ProBasicProject pj = CacheContext.pjmap.get(wt.getProjectId());
                            vo.setProjectname(pj.getName());
                        }
                        vo.setRank(alarm.getRank());
                        vo.setResettable(false);
                        vo.setStationid(wt.getWindpowerstationId());
                        if (CacheContext.wpmap.containsKey(wt.getWindpowerstationId())) {
                            ProBasicPowerstation wp = CacheContext.wpmap.get(wt.getWindpowerstationId());
                            vo.setStationname(wp.getName());
                        }
                        vo.setSubcomponents("");
                        vo.setSuffix("");
                        vo.setTriggertype(1);
                        vo.setUniformcode("");
                        vo.setSuperTableName(AlarmSuperTalbeType.CT.getCode());
                        vo.setVal(0);

                        vo.setCharacteristic(alarm.getName());
                        vo.setFaultCause(alarm.getDescription());
                        vo.setResolvent(alarm.getExpression());
                        alarmVoList.add(vo);
                    }
                }
            }
        }
    }

    private void wpCustomAlarm(List<AlarmVo> alarmVoList, Map<String, List<ProEconAlarmRule>> alarmRulesMap) {
        List<ProEconAlarmRule> alarmls = new ArrayList<>();
        if (alarmRulesMap.containsKey(DeviceTypeValue.WP.getCode())) {
            alarmls = alarmRulesMap.get(DeviceTypeValue.WP.getCode());

        }
//        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
//        List<ProBasicPowerstation> wpls = tokenService.getWpls(request);
        for (ProBasicPowerstation wp : CacheContext.wpls) {
            if (!alarmls.isEmpty()) {
                for (ProEconAlarmRule alarm : alarmls) {
                    AlarmVo vo = new AlarmVo();
                    StringBuilder tbName = new StringBuilder();
                    tbName.append(wp.getId()).append("_").append(alarm.getId());
                    vo.setTbName(String.valueOf(tbName));
                    vo.setAlarmId(alarm.getId());
                    vo.setTagid(alarm.getExpression());
                    vo.setAlarmType(AlarmTypeValue.CT.getCode());
                    vo.setCharacteristic(alarm.getName());
                    vo.setComponents(alarm.getRelatedParts());
                    vo.setDescription(alarm.getDescription());
                    vo.setDeviceId("");
                    vo.setDevicename("");
                    vo.setDeviceType(DeviceTypeValue.WP.getCode());
                    vo.setEnabled(alarm.getEnable());
                    vo.setLineid("");
                    vo.setLinename("");
                    vo.setModelId("");
                    vo.setProjectid("");
                    vo.setProjectname("");
                    vo.setRank(alarm.getRank());
                    vo.setResettable(false);
                    vo.setStationid(wp.getId());
                    vo.setStationname(wp.getName());
                    vo.setSubcomponents("");
                    vo.setSuffix("");
                    vo.setTriggertype(1);
                    vo.setUniformcode("");
                    vo.setSuperTableName(AlarmSuperTalbeType.CT.getCode());
                    vo.setVal(0);
                    alarmVoList.add(vo);
                }
            }
        }
    }

    private void btCustomAlarm(List<AlarmVo> alarmVoList, Map<String, List<ProEconAlarmRule>> alarmRulesMap) {


        Map<String,List<ProEconAlarmRule>> alarmMap=new HashMap<>();

        if (alarmRulesMap.containsKey(DeviceTypeValue.BT.getCode())) {

            List<ProEconAlarmRule>  alarmls = alarmRulesMap.get(DeviceTypeValue.BT.getCode());

            if(!alarmls.isEmpty())
            {
                for(ProEconAlarmRule alarm:alarmls)
                {
                    if (StringUtils.notEmp(alarm) && StringUtils.notEmp(alarm.getElectricalId()))
                    {
                        String [] ids=alarm.getElectricalId().split(",");
                        if(ids.length>0)
                        {
                            for(int i=0;i<ids.length;i++)
                            {
                                if(alarmMap.containsKey(ids[i]))
                                {
                                    List<ProEconAlarmRule> arls=alarmMap.get(ids[i]);
                                    arls.add(alarm);

                                }else
                                {
                                    List<ProEconAlarmRule> arls=new ArrayList<>();
                                    arls.add(alarm);
                                    alarmMap.put(ids[i],arls);
                                }

                            }
                        }
                    }

                }
            }
        }
//        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
//        List<ProBasicSubStation> subwpls = tokenService.getSubwpls(request);
        for (ProBasicSubStation wp : CacheContext.subwpls) {

            if (!alarmMap.isEmpty() && alarmMap.containsKey(wp.getId())) {

                List<ProEconAlarmRule> alarmls = alarmMap.get(wp.getId());
                for (ProEconAlarmRule alarm : alarmls) {
                    AlarmVo vo = new AlarmVo();
                    StringBuilder tbName = new StringBuilder();

                    tbName.append(wp.getId()).append("_").append(alarm.getId());
                    vo.setTbName(String.valueOf(tbName));
                    vo.setAlarmId(alarm.getId());
                    vo.setTagid(alarm.getExpression());
                    vo.setAlarmType(AlarmTypeValue.CT.getCode());
                    vo.setCharacteristic(alarm.getName());
                    vo.setComponents(alarm.getRelatedParts());
                    vo.setDescription(alarm.getDescription());
                    vo.setDeviceId("");
                    vo.setDevicename("");
                    vo.setDeviceType(DeviceTypeValue.BT.getCode());
                    vo.setEnabled(alarm.getEnable());
                    vo.setLineid("");
                    vo.setLinename("");
                    vo.setModelId("");
                    vo.setProjectid("");
                    vo.setProjectname("");
                    vo.setRank(alarm.getRank());
                    vo.setResettable(false);
                    vo.setStationid(wp.getId());
                    vo.setStationname(wp.getName());
                    vo.setSubcomponents("");
                    vo.setSuffix("");
                    vo.setTriggertype(1);
                    vo.setUniformcode("");
                    vo.setSuperTableName(AlarmSuperTalbeType.CT.getCode());
                    vo.setVal(0);
                    vo.setOval(0);

                    vo.setCharacteristic(alarm.getName());
                    vo.setFaultCause(alarm.getDescription());
                    vo.setResolvent(alarm.getExpression());
                    alarmVoList.add(vo);
                }
            }
        }
    }


}
