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.beans.factory.annotation.Value;
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 AlarmInService {
    @Resource
    private IAlarmTsService alarmTsService;
    @Resource
    private IProEconAlarmInfoService proEconAlarmInfoService;
    @Value("${initialcode}")
    private String initialcode;
    @Resource
    private TokenService tokenService;


    public List<AlarmVo> initalAlarmVo() {

        List<AlarmVo> alarmVoList=new ArrayList<>();
        Map<String, List<ProEconAlarmConfiguration>> wtAlarmMap = CacheContext.wtAlarmMap;
        Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;

//        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
//        List<ProBasicEquipment> wtls = tokenService.getWtls(request);
        for (ProBasicEquipment wt : CacheContext.wtls) {

            if (wtpAimap.containsKey(wt.getId())) {
                Map<String, ProBasicEquipmentPoint> pointMap = wtpAimap.get(wt.getId());

                StringBuilder sb = new StringBuilder();
                sb.append(wt.getWindpowerstationId() + wt.getModelId());
                if (StringUtils.notEmp(pointMap) && wtAlarmMap.containsKey(String.valueOf(sb))) {
                    List<ProEconAlarmConfiguration> alarmls = wtAlarmMap.get(String.valueOf(sb));

                    if (!alarmls.isEmpty()) {
                        for (ProEconAlarmConfiguration alarm : alarmls) {
                            if (pointMap.containsKey(alarm.getUniformCode())) {
                                ProBasicEquipmentPoint equipmentPoint = pointMap.get(alarm.getUniformCode());

                                if(StringUtils.notEmp(initialcode) &&
                                        StringUtils.notEmp(equipmentPoint.getNemCode()) &&
                                        !equipmentPoint.getNemCode().equals(initialcode) &&
                                        alarm.getAlarmType().equals(AlarmTypeValue.IN.getCode())
                                )
                                {
                                    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(equipmentPoint.getNemCode());
                                    vo.setAlarmType(alarm.getAlarmType());
                                    vo.setCharacteristic(alarm.getCharacteristic());
                                    vo.setComponents(alarm.getComponents());
                                    vo.setDescription(alarm.getDescription());
                                    vo.setDeviceId(wt.getId());
                                    vo.setDevicename(wt.getName());
                                    vo.setDeviceType(alarm.getDeviceType());
                                    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(alarm.getResetTable());
                                    vo.setStationid(wt.getWindpowerstationId());
                                    if(CacheContext.wpmap.containsKey(wt.getWindpowerstationId()))
                                    {
                                        ProBasicPowerstation wp=CacheContext.wpmap.get(wt.getWindpowerstationId());
                                        vo.setStationname(wp.getName());
                                    }
                                    vo.setSubcomponents(alarm.getSubcomponents());
                                    vo.setSuffix(alarm.getSuffix());
                                    vo.setTriggertype(alarm.getTriggerType());
                                    vo.setUniformcode(alarm.getUniformCode());
                                    vo.setSuperTableName(AlarmSuperTalbeType.WT.getCode());
                                    vo.setVal(0);

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

                                    alarmVoList.add(vo);
                                }

                            }
//                            else {
//
//                                //临时创建表
//                                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("GF-TZ-SYZ.038200G9911SYB99GI017DB0111PA99RAW01");
//                                vo.setAlarmtype(alarm.getAlarmType());
//                                vo.setCharacteristic(alarm.getCharacteristic());
//                                vo.setComponents(alarm.getComponents());
//                                vo.setDescription(alarm.getDescription());
//                                vo.setDeviceid(wt.getId());
//                                vo.setDevicename(wt.getName());
//                                vo.setDevicetype(alarm.getDeviceType());
//                                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(alarm.getResetTable());
//                                vo.setStationid(wt.getWindpowerstationId());
//                                if(CacheContext.wpmap.containsKey(wt.getWindpowerstationId()))
//                                {
//                                    ProBasicPowerstation wp=CacheContext.wpmap.get(wt.getWindpowerstationId());
//                                    vo.setStationname(wp.getName());
//                                }
//                                vo.setSubcomponents(alarm.getSubcomponents());
//                                vo.setSuffix(alarm.getSuffix());
//                                vo.setTriggertype(alarm.getTriggerType());
//                                vo.setUniformcode(alarm.getUniformCode());
//                                vo.setSuperTableName(AlarmSuperTalbeType.WT.getCode());
//                                vo.setVal(0);
//                                vo.setOval(0);
//                                alarmVoList.add(vo);
//
//                            }
                        }
                    }
                }
            }

        }
        return  alarmVoList;
    }

    public List<AlarmVo> initalAlarmVo(ProEconAlarmConfiguration alarm) {

        List<AlarmVo> alarmVoList=new ArrayList<>();
        Map<String, List<ProEconAlarmConfiguration>> wtAlarmMap = CacheContext.wtAlarmMap;
        Map<String, Map<String, ProBasicEquipmentPoint>> wtpAimap = CacheContext.wtpAimap;

//        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
//        List<ProBasicEquipment> wtls = tokenService.getWtls(request);
        for (ProBasicEquipment wt : CacheContext.wtls) {

            if (wtpAimap.containsKey(wt.getId())) {
                Map<String, ProBasicEquipmentPoint> pointMap = wtpAimap.get(wt.getId());


                if (StringUtils.notEmp(pointMap) ) {
                    if (pointMap.containsKey(alarm.getUniformCode())) {
                        ProBasicEquipmentPoint equipmentPoint = pointMap.get(alarm.getUniformCode());

                        if(StringUtils.notEmp(initialcode) &&
                                StringUtils.notEmp(equipmentPoint.getNemCode()) &&
                                !equipmentPoint.getNemCode().equals(initialcode) &&
                                alarm.getAlarmType().equals(AlarmTypeValue.IN.getCode())
                        )
                        {
                            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(equipmentPoint.getNemCode());
                            vo.setAlarmType(alarm.getAlarmType());
                            vo.setCharacteristic(alarm.getCharacteristic());
                            vo.setComponents(alarm.getComponents());
                            vo.setDescription(alarm.getDescription());
                            vo.setDeviceId(wt.getId());
                            vo.setDevicename(wt.getName());
                            vo.setDeviceType(alarm.getDeviceType());
                            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(alarm.getResetTable());
                            vo.setStationid(wt.getWindpowerstationId());
                            if(CacheContext.wpmap.containsKey(wt.getWindpowerstationId()))
                            {
                                ProBasicPowerstation wp=CacheContext.wpmap.get(wt.getWindpowerstationId());
                                vo.setStationname(wp.getName());
                            }
                            vo.setSubcomponents(alarm.getSubcomponents());
                            vo.setSuffix(alarm.getSuffix());
                            vo.setTriggertype(alarm.getTriggerType());
                            vo.setUniformcode(alarm.getUniformCode());
                            vo.setSuperTableName(AlarmSuperTalbeType.WT.getCode());
                            vo.setVal(0);

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

                            alarmVoList.add(vo);
                        }

                    }
                }
            }

        }
        return  alarmVoList;
    }

}
