package com.munjaon.client.config;

import com.munjaon.client.server.service.CollectClientService;
import com.munjaon.client.server.service.DataMoveService;
import com.munjaon.client.server.service.PropertyLoader;
import com.munjaon.client.server.service.ReportClientService;
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");
    }

    @Bean
    @Order(2)
    public CommandLineRunner getRunnerBeanForLms() {
        try {
            String serviceName = "LMS";
            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");
    }

    @Bean
    @Order(2)
    public CommandLineRunner getRunnerBeanForMms() {
        try {
            String serviceName = "MMS";
            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");
    }

    @Bean
    @Order(2)
    public CommandLineRunner getRunnerBeanForKat() {
        try {
            String serviceName = "KAT";
            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");
    }

    @Bean
    @Order(2)
    public CommandLineRunner getRunnerBeanForKft() {
        try {
            String serviceName = "KFT";
            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");
    }

    @Bean
    @Order(2)
    public CommandLineRunner getRunnerBeanForReport() {
        try {
            String serviceName = "REPORT";
            ReportClientService reportClientService = new ReportClientService(serviceName);
            reportClientService.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return args -> System.out.println("Runner Bean #2");
    }

    @Bean
    @Order(3)
    public CommandLineRunner getRunnerBeanForMove() {
        try {
            String serviceName = "LOG_MOVE";
            DataMoveService dataMoveService = new DataMoveService(serviceName);
            dataMoveService.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return args -> System.out.println("Runner Bean #2");
    }
}
