package com.its.vds.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 = "communication")
public class CommunicationConfig {

    private boolean logging = false;

    private String bindingAddr = "";
    private int listenPort = 30200;
    private int backLog = 0;
    private int acceptThreads = 0;
    private int workerThreads = 0;
    private int readerIdleTimeSeconds = 0;
    private int writerIdleTimeSeconds = 0;
    private int allIdleTimeSeconds = 0;

    private int rcvBuf = 0;
    private int sndBuf = 0;
    private int connectTimeoutSeconds = 0;
    private int maxConnection = 0;
    private int retrySeconds = 10;

    private boolean dumpRecv = false;
    private boolean dumpSend = false;

    private boolean subscriptionStatus = true;
    private int subscriptionStatusCycle = 30;


    @PostConstruct
    private void init() {
        int DEFAULT_EVENT_THREADS = Runtime.getRuntime().availableProcessors();
        if (this.bindingAddr.equals("")) {
            this.bindingAddr = "0.0.0.0";
        }

        if (this.backLog == 0) {
            this.backLog = 64;
        }

        if (this.acceptThreads == 0) {
            this.acceptThreads = DEFAULT_EVENT_THREADS * 2;
        }

        if (this.workerThreads == 0) {
            this.workerThreads = DEFAULT_EVENT_THREADS * 2;
        }

        if (this.rcvBuf == 0) {
            this.rcvBuf = 32768;
        }
        if (this.sndBuf == 0) {
            this.sndBuf = 32768;
        }

        if (this.readerIdleTimeSeconds == 0) {
            this.readerIdleTimeSeconds = 80;
        }
        if (this.connectTimeoutSeconds == 0) {
            this.connectTimeoutSeconds = 10;
        }

        if (this.maxConnection == 0) {
            this.maxConnection = 1024;
        }

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