#[cfg(feature = "ssr")]
use std::fs::read_to_string;
#[cfg(feature = "ssr")]
use std::net::SocketAddr;
#[cfg(feature = "ssr")]
use std::str::FromStr;
#[cfg(feature = "ssr")]
use serde::Deserialize;

#[cfg(feature = "ssr")]
#[derive(Deserialize)]
pub struct Network {
    bind_ip: String,
    port: u16
}

#[cfg(feature = "ssr")]
#[derive(Deserialize)]
pub struct Session {
    key: String
}

#[cfg(feature = "ssr")]
impl Session {
    pub fn key(&self) -> &[u8] {
        self.key.as_bytes()
    }
}

#[cfg(feature = "ssr")]
impl Network {
    pub fn bind_address(&self) -> SocketAddr {
        SocketAddr::from_str(format!("{}:{}", &self.bind_ip, self.port).as_str()).expect("Can't parse bind IP address")
    }
}

#[cfg(feature = "ssr")]
#[derive(Deserialize)]
pub struct Database {
    user: String,
    password: String,
    db_name: String,
    host: String,
    port: Option<u16>
}

#[cfg(feature = "ssr")]
impl Database {
    pub fn con_string(&self) -> String {
        format!("postgres://{}:{}@{}:{}/{}", self.user, self.password, self.host, self.port.unwrap_or(5432), self.db_name)
    }
}

#[cfg(feature = "ssr")]
#[derive(Deserialize, Clone)]
pub enum MailTransport {
    Smtp,
    File
}

#[cfg(feature = "ssr")]
#[derive(Deserialize, Clone)]
pub struct Mailing {
    transport: MailTransport,
    from: String,
    path: Option<String>,
    server: Option<String>,
    hello_name: Option<String>,
    port: Option<u16>,
    user: Option<String>,
    password: Option<String>,
    tls: Option<bool>,
    accept_all_certs: Option<bool>
}

#[cfg(feature = "ssr")]
impl Mailing {
    pub fn transport(&self) -> &MailTransport {
        &self.transport
    }
    pub fn from(&self) -> &str {
        &self.from
    }
    pub fn path(&self) -> &Option<String> {
        &self.path
    }
    pub fn server(&self) -> &Option<String> {
        &self.server
    }
    pub fn hello_name(&self) -> &Option<String> {
        &self.hello_name
    }
    pub fn port(&self) -> Option<u16> {
        self.port
    }
    pub fn user(&self) -> &Option<String> {
        &self.user
    }
    pub fn password(&self) -> &Option<String> {
        &self.password
    }
    pub fn tls(&self) -> Option<bool> {
        self.tls
    }
    pub fn accept_all_certs(&self) -> Option<bool> {
        self.accept_all_certs
    }
}

#[cfg(feature = "ssr")]
#[derive(Deserialize)]
pub struct Logging {
    severity: String
}

#[cfg(feature = "ssr")]
impl Logging {
    pub fn severity(&self) -> &str {
        &self.severity
    }
}

#[cfg(feature = "ssr")]
#[derive(Deserialize)]
pub struct Configuration {
    session: Session,
    network: Network,
    database: Database,
    mailing: Mailing,
    logging: Logging
}

#[cfg(feature = "ssr")]
impl Configuration {
    pub fn session(&self) -> &Session {
        &self.session
    }
    pub fn network(&self) -> &Network {
        &self.network
    }
    pub fn database(&self) -> &Database {
        &self.database
    }
    pub fn mailing(&self) -> &Mailing {
        &self.mailing
    }
    pub fn logging(&self) -> &Logging {
        &self.logging
    }
}

#[cfg(feature = "ssr")]
pub fn load_config(path: &str) -> Configuration {
    toml::from_str(read_to_string(path).expect("Can not open config file").as_str()).expect("Can not parse config file")
}

