package com.its.dsrc.global;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.its.dsrc.entity.TbRseCtlr;
import com.its.dsrc.entity.TbRseObuNonCrypt;
import com.its.dsrc.entity.TbRseOffrDrct;
import com.its.dsrc.entity.TbRseSect;
import io.netty.channel.Channel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

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

    private final ConcurrentHashMap<String, TbRseCtlr> ctlrMap = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<String, TbRseCtlr> ctlrIpMap = new ConcurrentHashMap<>();
    private final ConcurrentHashMap<Channel, TbRseCtlr> ctlrChannelMap = new ConcurrentHashMap<>();

    // RSE_SECT 정보관리, RSE Sect 관리(sect_id, start_rsd_id, end_rse_id, ...)
    private final ConcurrentHashMap<String, TbRseSect> rseSectMap = new ConcurrentHashMap<>();
    // 시작 RSE 를 키로 하는 SECT_ID 목록, { STRT_ID, [SECT_ID, SECT_ID, ...] }
    private final Multimap<String, String> rseSectStartMap = Multimaps.synchronizedMultimap(HashMultimap.<String, String>create());
    // 종료 RSE 를 키로 하는 SECT_ID 목록, { END_ID, [SECT_ID, SECT_ID, ...] }
    private final Multimap<String, String> rseSectEndMap = Multimaps.synchronizedMultimap(HashMultimap.<String, String>create());

    private final List<TbRseObuNonCrypt> rseObuNonCryptList = Collections.synchronizedList(new ArrayList<>());
    private final List<TbRseOffrDrct> rseOffrDrctList = Collections.synchronizedList(new ArrayList<>());

    public List<TbRseOffrDrct> getRseOffrDrctList(String rseId) {
        List<TbRseOffrDrct> list = new ArrayList<>();
        synchronized (this.rseOffrDrctList) {
            for (TbRseOffrDrct vo : this.rseOffrDrctList) {
                if (vo.getRSE_CTLR_NMBR().equals(rseId))
                    list.add(vo);
            }
        }
        return list;
    }

}
