package com.munjaon.client.config;

import com.munjaon.client.server.service.*;
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;

/**
 * RunnerConfiguration
 * Agent Service 실행
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class RunnerConfiguration {
    private final ServerConfig serverConfig;

    /**
     * 설정파일 로드
     * @return
     * @throws ConfigurationException
     */
    @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();

        return args -> System.out.println("MunjaonAgent Config Property : " + serverConfig.getServerProperyFile());
    }

    /**
     * SMS 서비스 실행
     * @return
     */
    @Bean
    @Order(2)
    public CommandLineRunner getRunnerBeanForSms() {
        try {
            String serviceName = "SMS";
            String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE");
            CollectClientService collectClientService = new CollectClientService(serviceName, serviceType);

            ShutdownService shutdownService = new ShutdownService(collectClientService);
            Runtime.getRuntime().addShutdownHook(shutdownService);

            collectClientService.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return args -> System.out.println("SMS Service Started");
    }

    /**
     * LMS 서비스 실행
     * @return
     */
    @Bean
    @Order(2)
    public CommandLineRunner getRunnerBeanForLms() {
        try {
            String serviceName = "LMS";
            String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE");
            CollectClientService collectClientService = new CollectClientService(serviceName, serviceType);

            ShutdownService shutdownService = new ShutdownService(collectClientService);
            Runtime.getRuntime().addShutdownHook(shutdownService);

            collectClientService.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return args -> System.out.println("LMS Service Started");
    }

    /**
     * MMS 서비스 실행
     * @return
     */
    @Bean
    @Order(2)
    public CommandLineRunner getRunnerBeanForMms() {
        try {
            String serviceName = "MMS";
            String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE");
            CollectClientService collectClientService = new CollectClientService(serviceName, serviceType);

            ShutdownService shutdownService = new ShutdownService(collectClientService);
            Runtime.getRuntime().addShutdownHook(shutdownService);

            collectClientService.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return args -> System.out.println("MMS Service Started");
    }

    /**
     * 카카오 알림톡 서비스 실행
     * @return
     */
    @Bean
    @Order(2)
    public CommandLineRunner getRunnerBeanForKat() {
        try {
            String serviceName = "KAT";
            String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE");
            CollectClientService collectClientService = new CollectClientService(serviceName, serviceType);

            ShutdownService shutdownService = new ShutdownService(collectClientService);
            Runtime.getRuntime().addShutdownHook(shutdownService);

            collectClientService.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return args -> System.out.println("KAT Service Started");
    }

    /**
     * 카카오 친구톡 서비스 실행
     * @return
     */
    @Bean
    @Order(2)
    public CommandLineRunner getRunnerBeanForKft() {
        try {
            String serviceName = "KFT";
            String serviceType = serverConfig.getString(serviceName + ".SERVICE_TYPE");
            CollectClientService collectClientService = new CollectClientService(serviceName, serviceType);

            ShutdownService shutdownService = new ShutdownService(collectClientService);
            Runtime.getRuntime().addShutdownHook(shutdownService);

            collectClientService.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return args -> System.out.println("KFT Service Started");
    }

    /**
     * 리포트 서비스 실행
     * @return
     */
    @Bean
    @Order(2)
    public CommandLineRunner getRunnerBeanForReport() {
        try {
            String serviceName = "REPORT";
            ReportClientService reportClientService = new ReportClientService(serviceName);

            ShutdownService shutdownService = new ShutdownService(reportClientService);
            Runtime.getRuntime().addShutdownHook(shutdownService);

            reportClientService.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return args -> System.out.println("REPORT Service Started");
    }

    /**
     * 로그이동 서비스 실행
     * @return
     */
    @Bean
    @Order(3)
    public CommandLineRunner getRunnerBeanForMove() {
        try {
            String serviceName = "LOG_MOVE";
            DataMoveService dataMoveService = new DataMoveService(serviceName);

            ShutdownService shutdownService = new ShutdownService(dataMoveService);
            Runtime.getRuntime().addShutdownHook(shutdownService);

            dataMoveService.start();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return args -> System.out.println("LOG MOVE Service Started");
    }
}
