//! This module is used to collect structures that is shared into `CommandExtension` from `Command`.

use toml;

use std::{collections::BTreeMap, net::SocketAddr};

use blockchain::config::{ConsensusConfig, ValidatorKeys};
use crypto::{PublicKey, SecretKey};

/// Abstract configuration.
pub type AbstractConfig = BTreeMap<String, toml::Value>;

/// Node public configurations.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodePublicConfig {
    /// Network address.
    pub address: String,
    /// Public keys of a validator.
    pub validator_keys: ValidatorKeys,
    /// Services configurations.
    #[serde(default)]
    pub services_public_configs: AbstractConfig,
}

/// `SharedConfig` contain all public information that should be shared in the handshake process.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SharedConfig {
    /// Template for common configuration
    pub common: CommonConfigTemplate,
    /// Public node
    pub node: NodePublicConfig,
}

impl NodePublicConfig {
    /// Returns address.
    pub fn address(&self) -> &str {
        &self.address
    }

    /// Returns services configurations.
    pub fn services_public_configs(&self) -> &AbstractConfig {
        &self.services_public_configs
    }
}

/// Base config.
#[derive(PartialEq, Clone, Debug, Serialize, Deserialize, Default)]
pub struct CommonConfigTemplate {
    /// Consensus configuration.
    pub consensus_config: ConsensusConfig,
    /// Services configuration.
    #[serde(default)]
    pub services_config: AbstractConfig,
    /// General configuration.
    pub general_config: AbstractConfig,
}

/// `NodePrivateConfig` collects all public and secret keys.
#[derive(Debug, Serialize, Deserialize)]
pub struct NodePrivateConfig {
    /// Listen address.
    pub listen_address: SocketAddr,
    /// External address.
    pub external_address: String,
    /// Consensus public key.
    pub consensus_public_key: PublicKey,
    /// Consensus secret key.
    pub consensus_secret_key: SecretKey,
    /// Service public key.
    pub service_public_key: PublicKey,
    /// Service secret key.
    pub service_secret_key: SecretKey,
    /// Additional service secret config.
    #[serde(default)]
    pub services_secret_configs: AbstractConfig,
}
