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.util.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Service;


import java.util.*;

@Service

public class AlarmCtService {

    private static Logger logger = LoggerFactory.getLogger(AlarmCtService.class);



    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;
    }

    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);
                    }
                }
            }
        }
        //将pg数据库中配置的自定义报警信息转化为报警对象，方便进行自定义报警的判定功能
        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);
                    }
                }
            }
        }
        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());

        }

        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);
                                }

                            }
                        }
                    }

                }
            }
        }

        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);
                }
            }
        }
    }

    public List<AlarmCustomTag> findTags(String stbaleName) {
        List<AlarmCustomTag> ls = new ArrayList<>();
        if (StringUtils.notEmp(stbaleName)) {
            // List<AlarmVo> vos= alarmTsService.findTags(stbaleName);
            List<AlarmVo> vos = CacheContext.alarmCtList;
            if (!vos.isEmpty()) {
                for (AlarmVo vo : vos) {
                    AlarmCustomTag po = new AlarmCustomTag();
                    po.setId(vo.getTbName());
                    po.setDescription(vo.getDescription());
                    po.setTagId(vo.getTagid());
                    po.setDeviceid(vo.getDeviceid());
                    po.setDevicetype(vo.getDevicetype());
                    po.setModelId(vo.getModelId());
                    po.setAlarmtype(vo.getAlarmtype());
                    po.setStationid(vo.getStationid());

                    po.setVal(null);

                    ls.add(po);
                }
            }
        }
        return ls;
    }



}
