package com.its.pis.config;

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;

@Slf4j
@Getter
@Setter
@ToString
@Configuration
@ConfigurationProperties(prefix = "application.center-comm")
public class CenterCommConfig {

    private boolean enable = true;
    private int listenPort = 9901;
    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;

    @PostConstruct
    private void init() {
        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 = 1024;
        }
        /*if (this.acceptThreads == 0) {
            if (MAX_CORE <= 8) {
                this.acceptThreads = 4;
            } else if (MAX_CORE >= 32) {
                this.acceptThreads = 12;
            } else {
                this.acceptThreads = 6;
            }
        }
        if (this.workerThreads == 0) {
            this.workerThreads = totalThreads - this.acceptThreads;
        }*/
        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;
        }

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

}
