package com.spring.infra.batch;

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

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class BatchJobInfoService {
    
    private final Environment environment;
    private final Map<String, BatchJobInfoData> batchJobInfoMap = new ConcurrentHashMap<>();

    public void setBatchJobInfoData(String beanName, BatchJobInfo batchJobInfo) {
        batchJobInfoMap.put(beanName, processBatchJobInfo(batchJobInfo));
    }

    public BatchJobInfoData getBatchJobInfo(String beanName) {
        return batchJobInfoMap.get(beanName);
    }

    private BatchJobInfoData processBatchJobInfo(BatchJobInfo batchJobInfo) {
        return new BatchJobInfoData(
            resolvePlaceHolder(batchJobInfo.group()),
            resolvePlaceHolder(batchJobInfo.jobName()),
            resolvePlaceHolder(batchJobInfo.cronExpression()),
            resolvePlaceHolder(batchJobInfo.description())
        );
    }

    private String resolvePlaceHolder(String value) {
        return environment.resolvePlaceholders(value);
    }

}
