package it.integry.integrywmsnative.core.data_cache;

import com.annimon.stream.Stream;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class DataCache {

    private static final List<DataCacheDTO> dataCacheList = new ArrayList<>();


    public static String addItem(Object item) {
        String uniqueID = UUID.randomUUID().toString();

        DataCacheDTO dataCacheDTO = new DataCacheDTO()
                .setItem(item)
                .setUUID(uniqueID);

        dataCacheList.add(dataCacheDTO);

        return uniqueID;
    }

    public static <T>T retrieveItem(String uuid) {

        List<DataCacheDTO> dataCacheTemp = Stream.of(dataCacheList)
            .filter(x -> x.getUUID().equals(uuid)).toList();

        Object foundDataCache = null;

        if(dataCacheTemp != null && dataCacheTemp.size() > 0) {
            foundDataCache = dataCacheTemp.get(0).getItem();
            dataCacheList.remove(dataCacheTemp.get(0));
        }

        return (T)foundDataCache;

    }


}
