package com.its.dsrc.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 = "running")
public class RunningConfig {

    private boolean commLogging = false;
    private int retrySeconds = 10;
    private int connectTimeout = 5;
    private int readerIdleTime = 0;
    private int writerIdleTime = 0;
    private int allIdleTime = 0;

    private boolean dumpRecv = true;
    private boolean dumpSend = true;

    private String serverDomainName = "GJATMSSERVER";
    private String serverAuthUser = "PTATMS";
    private String serverAuthPassword = "PTATMS";
    private String serverSenderText = "DSRC Center Unit";

    private boolean publicationMultimedia = false;
    private boolean publicationNonCryptObu = true;
    private int publicationAntenna = 1;
    private boolean subscriptionStatus = true;
    private int subscriptionStatusCycle = 30;
    private boolean subscriptionTraffic = true;

    private int filteringMinSpeed = 3;
    private int filteringMaxSpeed = 140;
    private int filteringHistoryMinute = 60;
    private int filteringAlgorithm = 1;

    //comm:
    private String commBindingAddr = "";
    private int commBindingPort = 3550;
    private int commBacklog = 0;
    private int commAcceptThreads = 0;
    private int commWorkerThreads = 0;
    private int commRcvBuf = 0;
    private int commSndBuf = 0;
    private int commReaderIdleTimeSeconds = 0;
    private int commWriterIdleTimeSeconds = 0;
    private int commAllIdleTimeSeconds = 0;
    private int commConnectTimeoutSeconds = 0;
    private int commMaxConnection = 0;
    private int commPacketWorkers = 0;
    private int commLoggingWorkers = 0;
    private int commDbmsWorkers = 1;

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

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

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

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

        if (this.commRcvBuf == 0) {
            this.commRcvBuf = 32768;
        }
        if (this.commSndBuf == 0) {
            this.commSndBuf = 32768;
        }

        if (this.commReaderIdleTimeSeconds == 0) {
            this.commReaderIdleTimeSeconds = 10;
        }

        if (this.commMaxConnection == 0) {
            this.commMaxConnection = 24;
        }
        if (this.commPacketWorkers == 0) {
            this.commPacketWorkers = DEFAULT_EVENT_THREADS / 2;
            if (this.commPacketWorkers == 0) this.commPacketWorkers = 1;
        }
        if (this.commLoggingWorkers == 0) {
            this.commLoggingWorkers = DEFAULT_EVENT_THREADS / 4;
            if (this.commLoggingWorkers == 0) this.commLoggingWorkers = 1;
        }
        if (this.commDbmsWorkers == 0) {
            this.commDbmsWorkers = DEFAULT_EVENT_THREADS / 4;
            if (this.commDbmsWorkers == 0) this.commDbmsWorkers = 1;
        }

        // IDLE TIME OUT CHECK HERE...

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