//! Ghost chain configuration.

use serde_json::{Map, Value};

#[cfg(feature = "casper-native")]
use casper_runtime as casper;

#[cfg(feature = "casper-native")]
use sc_chain_spec::ChainType;

use sc_chain_spec::ChainSpecExtension;
use serde::{Deserialize, Serialize};

#[cfg(feature = "casper-native")]
use telemetry::TelemetryEndpoints;

#[cfg(feature = "casper-native")]
const CASPER_TELEMETRY_URL: &str = "wss://telemetry.ghostchain.io/submit/";

#[cfg(feature = "casper-native")]
const DEFAULT_PROTOCOL_ID: &str = "cspr";

/// Node `ChainSpec` extensions.
///
/// Additional parameters for some substrate core modules,
/// customizable from the chain spec.
#[derive(Default, Clone, Serialize, Deserialize, ChainSpecExtension)]
#[serde(rename_all = "camelCase")]
pub struct Extensions {
    /// Block number with known hashes.
    pub fork_blocks: sc_client_api::ForkBlocks<primitives::Block>,
    /// Known bad block hashes.
    pub bad_blocks: sc_client_api::BadBlocks<primitives::Block>,
    /// The light sync state.
    /// This value will be set by the `sync-state rpc` implementation.
    pub light_sync_state: sc_sync_state_rpc::LightSyncStateExtension,
}

// Generic chain spec, in case when we don't have the native runtime.
pub type GenericChainSpec = sc_service::GenericChainSpec<(), Extensions>;

/// The `ChainSpec` parametrized for the ghost runtime.
#[cfg(feature = "casper-native")]
pub type CasperChainSpec = sc_service::GenericChainSpec<(), Extensions>;

#[cfg(not(feature = "casper-native"))]
pub type CasperChainSpec = GenericChainSpec;

pub fn casper_config() -> Result<CasperChainSpec, String> {
    CasperChainSpec::from_json_bytes(&include_bytes!("../chain-specs/casper.json")[..])
}

pub fn casper_chain_spec_properties() -> Map<String, Value> {
    let mut properties = Map::new();

    let _ = properties.insert("ss58Format".to_string(), Value::from(1996));
    let _ = properties.insert("tokenDecimals".to_string(), Value::from(18));
    let _ = properties.insert("tokenSymbol".to_string(), Value::from("CSPR"));

    properties
}

#[cfg(feature = "casper-native")]
pub fn casper_development_config() -> Result<CasperChainSpec, String> {
    Ok(CasperChainSpec::builder(
        casper::WASM_BINARY.ok_or("Casper development wasm not available")?,
        Default::default(),
    )
    .with_name("Development")
    .with_id("casper_development")
    .with_chain_type(ChainType::Development)
    .with_genesis_config_patch(casper::genesis::casper_development_config_genesis())
    .with_protocol_id(DEFAULT_PROTOCOL_ID)
    .with_properties(casper_chain_spec_properties())
    .build())
}

#[cfg(feature = "casper-native")]
pub fn casper_local_testnet_config() -> Result<CasperChainSpec, String> {
    Ok(CasperChainSpec::builder(
        casper::WASM_BINARY.ok_or("Casper local testnet wasm not available")?,
        Default::default(),
    )
    .with_name("Casper Local Testnet")
    .with_id("casper_local_testnet")
    .with_chain_type(ChainType::Local)
    .with_genesis_config_patch(casper::genesis::casper_local_config_genesis())
    .with_protocol_id(DEFAULT_PROTOCOL_ID)
    .with_properties(casper_chain_spec_properties())
    .build())
}

#[cfg(feature = "casper-native")]
pub fn casper_staging_testnet_config() -> Result<CasperChainSpec, String> {
    Ok(CasperChainSpec::builder(
        casper::WASM_BINARY.ok_or("Casper staging testnet wasm not available")?,
        Default::default(),
    )
    .with_name("Casper Staging Testnet")
    .with_id("casper_staging_testnet")
    .with_chain_type(ChainType::Live)
    .with_genesis_config_patch(casper::genesis::casper_staging_config_genesis())
    .with_telemetry_endpoints(
        TelemetryEndpoints::new(vec![(CASPER_TELEMETRY_URL.to_string(), 0)])
            .expect("Casper Staging telemetry url is valid; qed"),
    )
    .with_protocol_id(DEFAULT_PROTOCOL_ID)
    .with_properties(casper_chain_spec_properties())
    .build())
}
