package com.munjaon.client.config;

import com.munjaon.client.server.service.CollectClientService;
import com.munjaon.client.server.service.PropertyLoader;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class RunnerConfiguration {
    private final ServerConfig serverConfig;

    @Bean
    @Order(1)
    public CommandLineRunner getRunnerBeanForProperty() throws ConfigurationException {
        System.setProperty("PROPS", serverConfig.getServerProperyFile());
        System.setProperty("ROOTPATH", serverConfig.getServerRootPath());
        System.setProperty("DBMS", serverConfig.getString("DB.DBMS"));
        PropertyLoader.load();
//        try {
//            String[] array = serverConfig.getStringArray("test.list");
//            if (array != null && array.length > 0) {
//                for (String s : array) {
//                    System.out.println("List : " + s);
//                }
//            }
//        } catch (ConfigurationException e) {
//            throw new RuntimeException(e);
//        }
        return args -> System.out.println("Runner Bean #1 : " + serverConfig.getServerProperyFile());
    }

    @Bean
    @Order(2)
    public CommandLineRunner getRunnerBeanForSms() {
        try {
            String serviceName = "SMS";
            String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE");
            CollectClientService collectClientService = new CollectClientService(serviceName, serviceType);
            collectClientService.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return args -> System.out.println("Runner Bean #2");
    }
}
