package com.tsi.api.server.repository;

import com.tsi.api.server.mybatis.vo.AbstractDbmsVo;
import com.tsi.api.server.mybatis.vo.ProcessStateVo;
import com.tsi.api.server.vo.TsiAlarmConfigVo;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class TsiAlarmManager {

    private static TsiAlarmManager _instance = null;

    public static TsiAlarmManager getInstance() {
        if (_instance == null) {
            synchronized (TsiAlarmManager.class) {
                if (_instance == null)
                    _instance = new TsiAlarmManager();
            }
        }
        return _instance;
    }

    private ConcurrentHashMap<String, TsiAlarmConfigVo> tsiAlarmConfigMap;
    private ProcessStateVo processStateVo;

    private TsiAlarmManager() {
        this.tsiAlarmConfigMap = new ConcurrentHashMap<>();
        this.processStateVo = new ProcessStateVo(AbstractDbmsVo.DBMS_PROCESS_STATE);
    }

    public ProcessStateVo getProcessStateVo() {
        return this.processStateVo;
    }
    public TsiAlarmConfigVo get(String key) {
        return this.tsiAlarmConfigMap.get(key);
    }
    public void put(String key, TsiAlarmConfigVo vo) {
        this.tsiAlarmConfigMap.put(key, vo);
    }

    public int size() {
        return this.tsiAlarmConfigMap.size();
    }

    public boolean containsKey(String key) {
        return this.tsiAlarmConfigMap.containsKey(key);
    }

    public boolean checkAlarm(String code) {
        TsiAlarmConfigVo vo = get(code);
        return (vo != null && vo.isUseYn());
    }
    public boolean isAlarm(String code, int value) {
        TsiAlarmConfigVo vo = get(code);
        if (vo != null && vo.isUseYn()) {
            return (value > vo.getValue());
        }
        return false;
    }

}
