use clap::ArgMatches;
use fixed_bytes::FixedBytesExtended;
use lighthouse_network::{
    NETWORK_KEY_FILENAME, NetworkConfig,
    discovery::{CombinedKey, ENR_FILENAME, build_enr},
    libp2p::identity::secp256k1,
};
use network_utils::enr_ext::CombinedKeyExt;
use std::io::Write;
use std::path::PathBuf;
use std::{fs, net::Ipv4Addr};
use std::{fs::File, num::NonZeroU16};
use types::{ChainSpec, EnrForkId, Epoch, EthSpec, Hash256};

pub fn run<E: EthSpec>(matches: &ArgMatches, spec: &ChainSpec) -> Result<(), String> {
    let ip: Ipv4Addr = clap_utils::parse_required(matches, "ip")?;
    let udp_port: NonZeroU16 = clap_utils::parse_required(matches, "udp-port")?;
    let tcp_port: NonZeroU16 = clap_utils::parse_required(matches, "tcp-port")?;
    let output_dir: PathBuf = clap_utils::parse_required(matches, "output-dir")?;
    let genesis_fork_version: [u8; 4] =
        clap_utils::parse_ssz_required(matches, "genesis-fork-version")?;

    if output_dir.exists() {
        return Err(format!(
            "{:?} already exists, will not override",
            output_dir
        ));
    }

    let mut config = NetworkConfig::default();
    config.enr_address = (Some(ip), None);
    config.enr_udp4_port = Some(udp_port);
    config.enr_tcp6_port = Some(tcp_port);

    let secp256k1_keypair = secp256k1::Keypair::generate();
    let enr_key = CombinedKey::from_secp256k1(&secp256k1_keypair);
    let genesis_fork_digest = spec.compute_fork_digest(Hash256::zero(), Epoch::new(0));
    let enr_fork_id = EnrForkId {
        fork_digest: genesis_fork_digest,
        next_fork_version: genesis_fork_version,
        next_fork_epoch: Epoch::max_value(), // FAR_FUTURE_EPOCH
    };
    let enr = build_enr::<E>(
        &enr_key,
        &config,
        &enr_fork_id,
        spec.custody_requirement,
        genesis_fork_digest,
        spec,
    )
    .map_err(|e| format!("Unable to create ENR: {:?}", e))?;

    fs::create_dir_all(&output_dir).map_err(|e| format!("Unable to create output-dir: {:?}", e))?;

    let mut enr_file = File::create(output_dir.join(ENR_FILENAME))
        .map_err(|e| format!("Unable to create {}: {:?}", ENR_FILENAME, e))?;
    enr_file
        .write_all(enr.to_base64().as_bytes())
        .map_err(|e| format!("Unable to write ENR to {}: {:?}", ENR_FILENAME, e))?;

    let mut key_file = File::create(output_dir.join(NETWORK_KEY_FILENAME))
        .map_err(|e| format!("Unable to create {}: {:?}", NETWORK_KEY_FILENAME, e))?;

    let secret_bytes = secp256k1_keypair.secret().to_bytes();
    key_file
        .write_all(&secret_bytes)
        .map_err(|e| format!("Unable to write key to {}: {:?}", NETWORK_KEY_FILENAME, e))?;

    Ok(())
}
