package com.its.api.its.global;

import com.its.api.its.model.dto.node.TbNodeDto;
import com.its.api.its.model.entity.node.TbNode;
import com.its.api.its.repository.node.TbNodeRepository;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@RequiredArgsConstructor
@Getter
@Component
public class TbNodeManager {

    private final TbNodeRepository repo;

    private static final ConcurrentHashMap<Long, TbNodeDto> nodeMap = new ConcurrentHashMap<>();

    @PostConstruct
    void init() {
        load();
    }

    public void load() {

        List<TbNode> lists = repo.findAll();
        for (TbNode obj : lists) {
            nodeMap.put(obj.getNodeId(), obj.toDto());
        }
        //log.info("=== {}", nodeMap);
    }

    public static String getNodeName(Long NODE_ID) {
        TbNodeDto obj = nodeMap.get(NODE_ID);
        if (obj != null) {
            return "[" + NODE_ID + "] " + obj.getNodeName();
        }
        return "[" + NODE_ID + "] ?";
    }
    public static String getNodeNameInteger(Long NODE_ID) {
        TbNodeDto obj = nodeMap.get(NODE_ID);
        if (obj != null) {
            return obj.getNodeName();
        }
        return "?";
    }
}
