package com.its.op;

import com.its.op.config.*;
import com.its.op.entity.its.unit.TbUnitSyst;
import com.its.op.global.UnitSystManager;
import com.its.op.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.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationListener;
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
//@EnableJpaAuditing    // JPA Auditing 활성화
@EnableAspectJAutoProxy
@EnableAsync
@Configuration
@SpringBootApplication
/*@ComponentScan(basePackages = {
        "com.its.op.config",
        "com.its.op.dao.repository.its",
        "com.its.op.dao.repository.bis",
        "com.its.op.dao.repository.utis",
        "com.its.op.controller.*"
})*/
//@ComponentScan("com.its")
//@MapperScan({"com.its.op.dao.mapper.its", "com.its.op.dao.mapper.bis", "com.its.op.dao.mapper.utis"})
@CrossOrigin("*")
public class ItsOpServerApplication extends SpringBootServletInitializer implements CommandLineRunner, ApplicationListener<ContextClosedEvent>, InitializingBean, DisposableBean {

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

    public ItsOpServerApplication() {
        super();
        setRegisterErrorPageFilter(false);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(ItsOpServerApplication.class);
    }

    public static void main(String[] args) {
        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) {

        ApplicationConfig applicationConfig = (ApplicationConfig) AppUtils.getBean(ApplicationConfig.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: {}", applicationConfig.getId());
        log.info("** startup: {}", applicationConfig.getBootingDateTime());
        log.info("************************************************************************************");

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

//        VdsCommClientService vdsCommClientService = (VdsCommClientService)AppUtils.getBean(VdsCommClientService.class);
//        vdsCommClientService.run();

        VmsServerConfig vmsServerConfig = (VmsServerConfig)AppUtils.getBean(VmsServerConfig.class);
        TbUnitSyst vmsServer = UnitSystManager.get(vmsServerConfig.getProcessId());
        if (vmsServer != null) {
            vmsServerConfig.setIpAddress(vmsServer.getSystIp1().trim());
            vmsServerConfig.setPort(Integer.parseInt(vmsServer.getPrgmPort().trim()));
        }
        VdsServerConfig vdsServerConfig = (VdsServerConfig)AppUtils.getBean(VdsServerConfig.class);
        TbUnitSyst vdsServer = UnitSystManager.get(vdsServerConfig.getProcessId());
        if (vdsServer != null) {
            vdsServerConfig.setIpAddress(vdsServer.getSystIp1().trim());
            vdsServerConfig.setPort(Integer.parseInt(vdsServer.getPrgmPort().trim()));
        }
        RseServerConfig rseServerConfig = (RseServerConfig)AppUtils.getBean(RseServerConfig.class);
        TbUnitSyst rseServer = UnitSystManager.get(rseServerConfig.getProcessId());
        if (rseServer != null) {
            rseServerConfig.setIpAddress(rseServer.getSystIp1().trim());
            rseServerConfig.setPort(Integer.parseInt(rseServer.getPrgmPort().trim()));
        }
        CctvServerConfig cctvServerConfig = (CctvServerConfig)AppUtils.getBean(CctvServerConfig.class);
        TbUnitSyst cctvServer = UnitSystManager.get(cctvServerConfig.getProcessId());
        if (cctvServer != null) {
            cctvServerConfig.setIpAddress(cctvServer.getSystIp1().trim());
            cctvServerConfig.setPort(Integer.parseInt(cctvServer.getPrgmPort().trim()));
        }

        applicationConfig.setStartSchedule(true);
    }

    @Override
    public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
        // shutdown.....
        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        log.error("Application Terminated: {}, {}", sdfDate.format(new Date()), contextClosedEvent.toString());
    }

    @Override
    public void destroy() {
    }

    @Override
    public void afterPropertiesSet() {
    }

}
