use std::collections::HashMap;
use fluence_spell_dtos::trigger_config::TriggerConfig;
use maplit::hashmap;
use serde_json::{json, Value as JValue};
pub use build_info::PKG_VERSION as VERSION;
const CONNECTOR: &'static [u8] = include_bytes!("../decider-spell/chain_connector.wasm");
const CURL_ADAPTER: &'static [u8] = include_bytes!("../decider-spell/curl_adapter.wasm");
const CONFIG: &'static [u8] = include_bytes!("../decider-spell/Config.toml");
const DECIDER_SPELL: &'static str = include_str!("../decider-spell/poll/poll.main.air");
const WORKER_SPELL: &'static str = include_str!("../decider-spell/deal_spell/deal_spell.main.air");
pub mod build_info {
    include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
pub struct DistrService {
    pub name: &'static str,
    pub config: &'static [u8],
    pub modules: HashMap<&'static str, &'static [u8]>,
}
pub fn connector_service_modules() -> DistrService {
    DistrService {
        name: "chain_connector",
        config: CONFIG,
        modules: hashmap! {
            "chain_connector" => CONNECTOR,
            "curl_adapter" => CURL_ADAPTER,
        },
    }
}
pub struct DistrSpell {
    pub air: &'static str,
    pub kv: HashMap<&'static str, JValue>,
}
#[derive(Debug)]
pub struct DeciderConfig {
    pub worker_ipfs_multiaddr: String,
    pub worker_period_sec: u32,
    pub chain_api_endpoint: String,
    pub chain_network_id: u64,
    pub chain_contract_block_hex: String,
    pub chain_matcher_addr: String,
    pub chain_workers_gas: u64,
    pub chain_wallet_key: String,
}
pub fn decider_spell(config: DeciderConfig) -> DistrSpell {
    let mut worker_config = TriggerConfig::default();
    worker_config.clock.start_sec = 1;
    worker_config.clock.period_sec = config.worker_period_sec;
    DistrSpell {
        air: DECIDER_SPELL,
        kv: hashmap! {
            "worker_settings" => json!({
                "script": WORKER_SPELL,
                "config": worker_config,
                "ipfs": config.worker_ipfs_multiaddr
            }),
            "from_block" => json!(config.chain_contract_block_hex),
            "chain" => json!( {
                "api_endpoint": config.chain_api_endpoint,
                "matcher": config.chain_matcher_addr,
                "workers_gas": config.chain_workers_gas,
                "wallet_key": config.chain_wallet_key,
                "network_id": config.chain_network_id
            }),
        },
    }
}