use substreams_ethereum::pb::eth::v2::{Call, Log, TransactionTrace};
use substreams_ethereum::Event;
use tycho_substreams::models::{
    ChangeType, FinancialType, ImplementationType, ProtocolComponent, ProtocolType,
};
use crate::abi;
use crate::params::Params;

/// Potentially constructs a new ProtocolComponent given a call
///
/// This method is given each individual call within a transaction, the corresponding
/// logs emitted during that call as well as the full transaction trace.
///
/// If this call creates a component in your protocol please construct and return it
/// here. Otherwise, simply return None.
pub fn maybe_create_component(
    params: &Params,
    call: &Call,
    _log: &Log,
    _tx: &TransactionTrace,
) -> Option<ProtocolComponent> {
    if call.address.as_slice() == params.planner {
        if let Some(event) = abi::party_planner::events::PartyStarted::match_and_decode(_log) {
            return Some(ProtocolComponent {
                id: hex::encode(&event.pool),
                tokens: event.tokens,
                contracts: vec![event.pool.clone(), params.mint_impl.clone(), params.swap_impl.clone()],
                static_att: vec![],
                change: ChangeType::Creation.into(),
                protocol_type: Some(ProtocolType {
                    name: "liquidity_party".to_string(),
                    financial_type: FinancialType::Swap.into(),
                    attribute_schema: vec![],
                    implementation_type: ImplementationType::Vm.into(),
                }),
            });
        }
    }
    None
}
