package com.its.api;

import com.its.api.config.AppUtils;
import com.its.api.config.ProcessConfig;
import com.its.api.xnetudp.CenterCommUdpServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.Banner;
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.annotation.EnableAspectJAutoProxy;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.CrossOrigin;

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

@Slf4j
@EnableAspectJAutoProxy
@EnableAsync
@Configuration
@SpringBootApplication
@ComponentScan(basePackages = {"com.its.api.config", "com.its.api.its.repository.*", "com.its.api.*"})
@CrossOrigin("*")
public class ItsOpServerApplication implements CommandLineRunner, ApplicationListener<ContextClosedEvent>, InitializingBean, DisposableBean {

    private static final String applicationName = "its-op-server";

    public static void main(String[] args) {
        //System.setProperty("log4j.skipJansi", String.valueOf(true));

        SpringApplication application = new SpringApplicationBuilder()
                .sources(ItsOpServerApplication.class)
                .listeners(new ApplicationPidFileWriter("./conf/" + applicationName + ".pid"))
                .build();
        application.setBannerMode(Banner.Mode.OFF);
        application.run(args);
    }

    @Override
    public void run(String... args) throws Exception {

        ProcessConfig processConfig = (ProcessConfig) AppUtils.getBean(ProcessConfig.class);

        log.info("");
        log.info("");
        log.info("************************************************************************************");
        log.info("**                                                                                **");
        log.info("**                         Intelligent Traffic System                             **");
        log.info("**              Intelligent Traffic System Common Operating Server.               **");
        log.info("**                                                                                **");
        log.info("**                                                                   [ver.1.0]    **");
        log.info("** process: {}", processConfig.getId());
        log.info("** startup: {}", processConfig.getBootingDateTime());
        log.info("************************************************************************************");

        CenterCommUdpServer centerCommUdpServer = (CenterCommUdpServer)AppUtils.getBean(CenterCommUdpServer.class);
        centerCommUdpServer.run();
    }

    @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 destroy() throws Exception {
    }

    @Override
    public void afterPropertiesSet() throws Exception {
    }
}
