package org.ts.ddcs.tool;

import org.ts.ddcs.entity.ProjectWarningSettingInfo;
import org.ts.ddcs.entity.RtuWarningSettingInfo;
import org.ts.ddcs.entity.WarningInfo;
import org.ts.ddcs.utils.GBConstants;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author dylan
 */
public class WarningHelper {
    public static ProjectWarningSettingInfo getProjectWarningSetting(String projectId, RedisTemplate redisTemplate) {
        HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
        String key = "project_" + projectId;
        if (redisTemplate.hasKey(key)) {
            ProjectWarningSettingInfo settingInfo = new ProjectWarningSettingInfo();
            if (opsHash.hasKey(key, GBConstants.WARNING_PROJECT_SETTING_GB_ACTIVE_KEY)) {
                String switchValue = opsHash.get(key, GBConstants.WARNING_PROJECT_SETTING_GB_ACTIVE_KEY);
                settingInfo.setGlobalSwitch(switchValue);
            }
            if (opsHash.hasKey(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_OFFLINE)) {
                String switchValue = opsHash.get(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_OFFLINE);
                settingInfo.setOfflineSwitch(switchValue);
            }
            if (opsHash.hasKey(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_DATAGRAM_DELAY)) {
                String switchValue = opsHash.get(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_DATAGRAM_DELAY);
                settingInfo.setDatagramDelaySwitch(switchValue);
            }
            if (opsHash.hasKey(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_RTU_FAULT_SE)) {
                String switchValue = opsHash.get(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_RTU_FAULT_SE);
                settingInfo.setFaultSeSwitch(switchValue);
            }
            if (opsHash.hasKey(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_RTU_FAULT_AC)) {
                String switchValue = opsHash.get(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_RTU_FAULT_AC);
                settingInfo.setFaultAcSwitch(switchValue);
            }
            if (opsHash.hasKey(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_RTU_FAULT_BV)) {
                String switchValue = opsHash.get(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_RTU_FAULT_BV);
                settingInfo.setFaultBvSwitch(switchValue);
            }
            if (opsHash.hasKey(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_RTU_COLLECT_TIME_ERROR)) {
                String switchValue = opsHash.get(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_RTU_COLLECT_TIME_ERROR);
                settingInfo.setCollectTimeErrorSwitch(switchValue);
            }
            if (opsHash.hasKey(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_CRC_ERROR)) {
                String switchValue = opsHash.get(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_CRC_ERROR);
                settingInfo.setCrcErrorSwitch(switchValue);
            }
            if (opsHash.hasKey(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_OUTLIER_DATA)) {
                String switchValue = opsHash.get(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_OUTLIER_DATA);
                settingInfo.setOutlierDataSwitch(switchValue);
            }
            if (opsHash.hasKey(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_SD_DAMAGE)) {
                String switchValue = opsHash.get(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_SD_DAMAGE);
                settingInfo.setSdDamageSwitch(switchValue);
            }
            if (opsHash.hasKey(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_RTU_NET_BLOCKING)) {
                String switchValue = opsHash.get(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_RTU_NET_BLOCKING);
                settingInfo.setRtuNetBlockingSwitch(switchValue);
            }
            if (opsHash.hasKey(key, GBConstants.WARNING_PROJECT_SETTING_DATAGRAM_DELAY_TIME_KEY)) {
                String switchValue = opsHash.get(key, GBConstants.WARNING_PROJECT_SETTING_DATAGRAM_DELAY_TIME_KEY);
                settingInfo.setOfflineDuration(switchValue);
            }
            if (opsHash.hasKey(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_SUN_VOLTAGE)) {
                String switchValue = opsHash.get(key, GBConstants.REDIS_PROJECT_WARNING_SETTING_KIND_KEY_PRE + GBConstants.WARNING_TYPE_SUN_VOLTAGE);
                settingInfo.setSunVoltageSwitch(switchValue);
            }
            return settingInfo;
        }
        return null;
    }

    public static RtuWarningSettingInfo getRtuWarningSetting(String rtuCode, RedisTemplate redisTemplate) {
        HashOperations<String, String, String> opsHash = redisTemplate.opsForHash();
        RtuWarningSettingInfo settingInfo = new RtuWarningSettingInfo();
        if (opsHash.hasKey(rtuCode, GBConstants.REDIS_KEY_RTU_WARNING_ACTIVE)) {
            settingInfo.setGlobalSwitch(opsHash.get(rtuCode, GBConstants.REDIS_KEY_RTU_WARNING_ACTIVE));
        }
        if (opsHash.hasKey(rtuCode, GBConstants.REDIS_KEY_RTU_OFFLINE_ACTIVE)) {
            settingInfo.setOfflineWarningActive(opsHash.get(rtuCode, GBConstants.REDIS_KEY_RTU_OFFLINE_ACTIVE));
            settingInfo.setOfflineDuration(opsHash.get(rtuCode, GBConstants.REDIS_KEY_RTU_OFFLINE_DURATION));
        }
        return settingInfo;
    }

    public static WarningInfo toWarningInfo(String rtuCode, String warningType, String warningStatus, Date checkTime, String warningInfo, String warningContent) {
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        WarningInfo info = new WarningInfo();
        info.setRtuCode(rtuCode);
        info.setWarningType(warningType);
        info.setWarningCheckTime(timeFormat.format(checkTime));
        info.setWarningStatus(warningStatus);
        info.setLastTime(timeFormat.format(checkTime));
        info.setWarningContent(warningContent);
        info.setWarningInfo(warningInfo);
        return info;
    }
}
