package com.utic.center.utic.traf.server.config;

import com.utic.center.common.utils.TimeUtils;
import com.utic.center.utic.traf.server.repository.ApplicationRepository;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Slf4j
@Getter
@Setter
@Component
@RequiredArgsConstructor
@ConfigurationProperties(prefix = "application")
public class ApplicationConfig {

    private String processName = "utic-traf-server";
    private String processId = "70030";
    private int errTimeGap = 120;
    private int prcsThreadCount = 10;
    private int dbmsBatchSize = 2000;
    private boolean loadingLink = false;

    private String bootingTime;
    private boolean startSchedule;

    @PostConstruct
    private void init() {
        this.startSchedule = false;
        this.bootingTime = TimeUtils.now();

        if (this.prcsThreadCount < 1) {
            this.prcsThreadCount = 1;
        }
        this.prcsThreadCount = Math.min(this.prcsThreadCount, 20);

        if (this.dbmsBatchSize <= 1000) {
            this.dbmsBatchSize = 1000;
        }
        this.dbmsBatchSize = Math.min(this.dbmsBatchSize, 2000);

        ApplicationRepository.PRCS_THREAD_COUNT = this.prcsThreadCount;
        ApplicationRepository.DBMS_BATCH_SIZE = this.dbmsBatchSize;
        ApplicationRepository.processState.setProcessId(this.processId);
        ApplicationRepository.processState.setErrTimeGap(this.getErrTimeGap());

        log.info("[{}] -------------------------", this.getClass().getSimpleName());
        log.info("{}", super.toString());
    }

}
