package com.prolog.eis.init;

import com.prolog.eis.constants.cache.EisCache;
import com.prolog.eis.model.config.PalletPosStrategyConfig;
import com.prolog.eis.service.config.PalletPosStrategyConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor
public class PalletPositioning implements CommandLineRunner {

    private final PalletPosStrategyConfigService palletPosStrategyConfigService;

    /*@Override
    public void run(String... args) throws Exception {
        List<PalletPosStrategyConfig> palletPosStrategyConfigList = palletPosStrategyConfigService.findAllConfig();
        Map<String, List<PalletPosStrategyConfig>> map = palletPosStrategyConfigList.stream().collect(Collectors.groupingBy(PalletPosStrategyConfig::getStrategyGroup));
        for (Map.Entry<String, List<PalletPosStrategyConfig>> entry : map.entrySet()) {
            ConcurrentHashMap<Integer, String> concurrentHashMap = new ConcurrentHashMap<>();
            List<PalletPosStrategyConfig> value = entry.getValue();
            for (PalletPosStrategyConfig palletPosStrategyConfig : value) {
                concurrentHashMap.put(palletPosStrategyConfig.getPriority(), palletPosStrategyConfig.getStrategyName());

            }
            EisCache.PALLET_POSITIONING_STRATEGY_MAP.put(entry.getKey(),concurrentHashMap);
        }
    }*/

    @Override
    public void run(String... args) throws Exception {
        List<PalletPosStrategyConfig> palletPosStrategyConfigList = palletPosStrategyConfigService.findAllConfig();
        for (PalletPosStrategyConfig palletPosStrategyConfig : palletPosStrategyConfigList) {
            EisCache.PALLET_POSITIONING_STRATEGY_MAP.put(palletPosStrategyConfig.getPriority(), palletPosStrategyConfig.getStrategyName());
        }
    }

}
