package com.its.bis.config;

import com.its.app.utils.SysUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.io.File;

@Slf4j
@Getter
@Setter
@ToString
@Configuration
@ConfigurationProperties(prefix = "application")
public class ApplicationConfig {

    private String bootingDateTime;
    private boolean startSchedule;

    private String id = "BIS01";
    private String name = "BIS 위치정보 수집 서버";
    private boolean history = true;
    private boolean statistics = true;

    private String userId = "admin";
    private String userPswd = "1234";

    // Center Communication Config
    private boolean centerCommEnable = false;
    private int listenPort = 30200;
    protected String bindingAddr = "0.0.0.0";
    protected int backlog = 0;
    protected int acceptThreads = 0;
    protected int workerThreads = 0;
    protected int rcvBuf = 0;
    protected int sndBuf = 0;
    protected int readerIdleTimeSeconds = 0;
    protected int writerIdleTimeSeconds = 0;
    protected int allIdleTimeSeconds = 0;
    protected int connectTimeoutSeconds = 0;

    private boolean loadDb = true;

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

        final int DEFAULT_EVENT_THREADS  = Runtime.getRuntime().availableProcessors() * 2;
        if (this.bindingAddr.equals("")) {
            this.bindingAddr = "0.0.0.0";
        }
        if (this.backlog == 0) {
            this.backlog = 32;
        }
        if (this.acceptThreads == 0) {
            this.acceptThreads = DEFAULT_EVENT_THREADS;
        }
        if (this.workerThreads == 0) {
            this.workerThreads = DEFAULT_EVENT_THREADS;
        }

        if (this.rcvBuf == 0) {
            this.rcvBuf = Short.MAX_VALUE / 2;
        }
        if (this.sndBuf == 0) {
            this.sndBuf = Short.MAX_VALUE / 2;
        }

        this.bootingDateTime = SysUtils.getSysTimeStr();

        log.info("{}", this);
    }

    public boolean makeDirectory(String path, String desc) {
        boolean result = false;
        File folder = new File(path);
        if (!folder.exists()) {
            try {
                result = folder.mkdir(); //폴더 생성합니다.
                log.info("{}, ({}) created.", folder.getAbsolutePath(), desc);
            }
            catch(Exception e) {
                log.error("{}, ({}) create failed. {}.", folder.getAbsolutePath(), desc, e.getMessage());
            }
        }
        return result;
    }

}
