package com.tsiwebserver;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.ApplicationPidFileWriter;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.scheduling.annotation.EnableAsync;
import lombok.extern.slf4j.Slf4j;


@Slf4j
@Configuration
@EnableAsync
//@EnableAspectJAutoProxy
@SpringBootApplication
@ComponentScan(basePackages = {"com.tsiwebserver.config", "com.tsiwebserver.security", "com.tsiwebserver.mapper", "com.tsiwebserver.service.impl", "com.tsiwebserver"})
public class TsiWebServerApplication implements CommandLineRunner, ApplicationListener<ContextClosedEvent> {

	private static final String applicationName = "tsi-web-server";

	public static void main(String[] args) {
		SpringApplication application = new SpringApplicationBuilder()
                .sources(TsiWebServerApplication.class)
                .listeners(new ApplicationPidFileWriter("./conf/" + applicationName + ".pid"))
                .build();
        //application.setBannerMode(Banner.Mode.OFF);
        application.run(args);
	}
	
    @Override
    public void run(String... args) throws Exception, RuntimeException {
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.info("");
        log.info("");
        log.info("************************************************************************************");
        log.info("**                                                                                **");
        log.info("**                               TSI CVIM System                                  **");
        log.info("**                          CVIM WEB Server Program.                              **");
        log.info("**                                                                                **");
        log.info("**                                                                   [ver.1.0]    **");
        log.info("** startup: {}", sdfDate.format(new Date()));
        log.info("************************************************************************************");

    }

    @Override
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        log.error("Application Terminated: {}, {}", sdfDate.format(new Date()), contextClosedEvent.toString());
    }
//
//    @Override
//    public void afterPropertiesSet() throws Exception {
//        //System.err.println("InitializingBean 인터페이스 구현 메서드입니다. 'Bean'이 생성될 때 마다 호출되는 메서드 입니다.");
//    }
//    @Override
//    public void destroy() throws Exception {
//        //System.err.println("DisposableBean 인터페이스 구현 메서드입니다. 'Bean'이 소멸될 때 마다 호출되는 메서드입니다");
//    }

}
