package com.its.bis.service;

import com.its.bis.dto.TbVmsCtlrDto;
import io.netty.channel.Channel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

@Slf4j
@Service
@RequiredArgsConstructor
public class AppRepositoryService {

    private final ConcurrentHashMap<Long, TbVmsCtlrDto> ctlrMap = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, TbVmsCtlrDto> ipAddrMap = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<Channel, TbVmsCtlrDto> channelMap = new ConcurrentHashMap<>();

    private boolean isStaticCycle = false;
    private String dsplDt;  // 폼 정보 다운로드 시각

    public void setStaticCycle(boolean isStaticCycle) {
        this.isStaticCycle = isStaticCycle;
    }
    public boolean isStaticCycle() {
        return this.isStaticCycle;
    }

    public void initCtlrInfo() {
        try {
            for (Map.Entry<Long, TbVmsCtlrDto> e : this.ctlrMap.entrySet()) {
                TbVmsCtlrDto obj = e.getValue();
                obj.setDelYn("Y");
            }
        }
        catch (Exception e) {
            log.error("AppRepositoryService.initCtlrInfo: Exception: {}.", e.toString());
        }
    }
    public Set<Map.Entry<Long, TbVmsCtlrDto>> getEntrySetCtrl() {
        return this.ctlrMap.entrySet();
    }
    public ConcurrentMap<Long, TbVmsCtlrDto> getCtlrMap() {
        return this.ctlrMap;
    }
    public ConcurrentMap<String, TbVmsCtlrDto> getIpAddrMap() {
        return this.ipAddrMap;
    }
    public ConcurrentMap<Channel, TbVmsCtlrDto> getChannelMap() {
        return this.channelMap;
    }

    public TbVmsCtlrDto putCtlrMap(TbVmsCtlrDto obj) {
        return this.ctlrMap.put(obj.getVmsCtlrNmbr(), obj);
    }
    public TbVmsCtlrDto getCtlrMap(Long vmsCtlrNmbr) {
        return this.ctlrMap.get(vmsCtlrNmbr);
    }

    public TbVmsCtlrDto putIpAddrMap(TbVmsCtlrDto obj) {
        return this.ipAddrMap.put(obj.getCtlrIp(), obj);
    }
    public TbVmsCtlrDto getIpAddrMap(String ipAddr) {
        return this.ipAddrMap.get(ipAddr);
    }

    public TbVmsCtlrDto putChannelMap(TbVmsCtlrDto obj) {
        return this.channelMap.put(obj.getChannel(), obj);
    }
    public TbVmsCtlrDto getChannelMap(Channel channel) {
        return this.channelMap.get(channel);
    }

    public void setDsplDt(String dsplDt) {
        this.dsplDt = dsplDt;
    }
    public String getDsplDt() {
        return this.dsplDt;
    }
}
