package com.gyee.alarm.service;

import com.gyee.alarm.init.CacheContext;
import com.gyee.alarm.model.auto.ProBasicSubStation;
import com.gyee.alarm.model.auto.ProEconAlarmConfiguration;
import com.gyee.alarm.model.auto.ProEconAlarmInfo;
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.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

@Service

public class AlarmBtService {
    @Resource
    private IAlarmTsService alarmTsService;
    @Resource
    private IProEconAlarmInfoService proEconAlarmInfoService;





    public List<AlarmVo> initalAlarmVo() {

        List<ProEconAlarmConfiguration> btalarmls = CacheContext.btalarmls;


        List<AlarmVo> alarmVoList = new ArrayList<>();
        if (!btalarmls.isEmpty()) {
            for (ProEconAlarmConfiguration alarm : btalarmls) {
                if (StringUtils.notEmp(alarm.getTagId()) &&
                        (alarm.getAlarmType().equals(AlarmTypeValue.BT.getCode()) || alarm.getAlarmType().equals(AlarmTypeValue.BTE.getCode()))) {
                    AlarmVo vo = new AlarmVo();
                    StringBuilder tbName = new StringBuilder();
                    tbName.append(alarm.getStationId()).append("_").append(alarm.getId());
                    vo.setTbName(String.valueOf(tbName));
                    vo.setAlarmId(alarm.getId());
                    vo.setTagid(alarm.getTagId());
                    vo.setAlarmType(alarm.getAlarmType());
                    vo.setCharacteristic(alarm.getCharacteristic());
                    vo.setComponents(alarm.getComponents());
                    vo.setDescription(alarm.getDescription());
                    vo.setDeviceId("");
                    vo.setDevicename("");
                    vo.setDeviceType(DeviceTypeValue.BT.getCode());
                    vo.setEnabled(alarm.getEnable());
                    vo.setLineid("");
                    vo.setLinename("");
                    vo.setModelId(alarm.getModelId());
                    vo.setProjectid("");
                    vo.setProjectname("");
                    vo.setRank(alarm.getRank());
                    vo.setResettable(alarm.getResetTable());
                    vo.setStationid(alarm.getStationId());
                    if (CacheContext.subwpmap.containsKey(alarm.getStationId())) {
                        ProBasicSubStation wp = CacheContext.subwpmap.get(alarm.getStationId());
                        vo.setStationname(wp.getName());
                    }
                    vo.setSubcomponents(alarm.getSubcomponents());
                    vo.setSuffix(alarm.getSuffix());
                    vo.setTriggertype(alarm.getTriggerType());
                    vo.setUniformcode(alarm.getUniformCode());
                    vo.setSuperTableName(AlarmSuperTalbeType.BT.getCode());
                    vo.setVal(0);
                    vo.setOval(0);

                    vo.setName(alarm.getName());
                    vo.setNemCode(alarm.getNemCode());
                    vo.setFaultCause(alarm.getFaultCause());
                    vo.setResolvent(alarm.getResolvent());
                    alarmVoList.add(vo);
                }
            }
        }

        return alarmVoList;
    }

    public List<AlarmVo> initalAlarmVo(ProEconAlarmConfiguration alarm) {

        List<ProEconAlarmConfiguration> btalarmls = CacheContext.btalarmls;


        List<AlarmVo> alarmVoList = new ArrayList<>();
        if (StringUtils.notEmp(alarm.getTagId()) &&
                (alarm.getAlarmType().equals(AlarmTypeValue.BT.getCode()) || alarm.getAlarmType().equals(AlarmTypeValue.BTE.getCode()))) {
            AlarmVo vo = new AlarmVo();
            StringBuilder tbName = new StringBuilder();
            tbName.append(alarm.getStationId()).append("_").append(alarm.getId());
            vo.setTbName(String.valueOf(tbName));
            vo.setAlarmId(alarm.getId());
            vo.setTagid(alarm.getTagId());
            vo.setAlarmType(alarm.getAlarmType());
            vo.setCharacteristic(alarm.getCharacteristic());
            vo.setComponents(alarm.getComponents());
            vo.setDescription(alarm.getDescription());
            vo.setDeviceId("");
            vo.setDevicename("");
            vo.setDeviceType(DeviceTypeValue.BT.getCode());
            vo.setEnabled(alarm.getEnable());
            vo.setLineid("");
            vo.setLinename("");
            vo.setModelId(alarm.getModelId());
            vo.setProjectid("");
            vo.setProjectname("");
            vo.setRank(alarm.getRank());
            vo.setResettable(alarm.getResetTable());
            vo.setStationid(alarm.getStationId());
            if (CacheContext.subwpmap.containsKey(alarm.getStationId())) {
                ProBasicSubStation wp = CacheContext.subwpmap.get(alarm.getStationId());
                vo.setStationname(wp.getName());
            }
            vo.setSubcomponents(alarm.getSubcomponents());
            vo.setSuffix(alarm.getSuffix());
            vo.setTriggertype(alarm.getTriggerType());
            vo.setUniformcode(alarm.getUniformCode());
            vo.setSuperTableName(AlarmSuperTalbeType.BT.getCode());
            vo.setVal(0);
            vo.setOval(0);

            vo.setName(alarm.getName());
            vo.setNemCode(alarm.getNemCode());
            vo.setFaultCause(alarm.getFaultCause());
            vo.setResolvent(alarm.getResolvent());
            alarmVoList.add(vo);
        }

        return alarmVoList;
    }

}
