<?php

global $ETHEREUMICOIO_BLOCKCHAINS_INFO_VERSION;

add_filter('ethereumico.io/supported-blockchains', function ($blockchains) {
    return array_merge($blockchains, array(
        "avalanche-mainnet" => __('Avalanche (C-Chain) Mainnet', 'ethereum-wallet'),
    ));
}, 28, 1);

add_filter('ethereumico.io/supported-blockchains-info', function ($blockchains) {
    $info = apply_filters('ethereumico.io/blockchain-info', $blockchains && isset($blockchains['a43114']) ? $blockchains['a43114'] : null, 43114);
    return array_merge($blockchains, array(
        "avalanche-mainnet" => $info,
        "a43114" => $info,
    ));
}, $ETHEREUMICOIO_BLOCKCHAINS_INFO_VERSION, 1);

add_filter('ethereumico.io/blockchain-info/blockchain-id', function ($blockchainId, $blockchainName) {
    if (!in_array($blockchainName, ['avalanche-mainnet'])) {
        return $blockchainId;
    }
    return 43114;
}, $ETHEREUMICOIO_BLOCKCHAINS_INFO_VERSION, 2);

add_filter('ethereumico.io/blockchain-info/blockchain-name', function ($blockchainName, $chainId) {
    if (43114 !== intval($chainId)) {
        return $blockchainName;
    }
    return 'avalanche-mainnet';
}, $ETHEREUMICOIO_BLOCKCHAINS_INFO_VERSION, 2);

add_filter('ethereumico.io/blockchain-info', function ($info, $chainId) {
    if (43114 !== intval($chainId)) {
        return $info;
    }
    $etherscanApiKey = apply_filters('ethereumico.io/blockchain-explorer-api-key', '', $chainId);

    $base_url = 'https://api.routescan.io/v2/network/mainnet/evm/43114/etherscan/api?';
    $gas_price_api_url = null;
    // $gas_price_api_url = str_replace('%25', '%', $base_url . http_build_query(array(
    //     'module' => 'gastracker',
    //     'action' => 'gasoracle',
    //     'apikey' => $etherscanApiKey,
    // )));
    $ethprice_api_url = str_replace('%25', '%', $base_url . http_build_query(array(
        'module' => 'stats',
        'action' => 'ethprice',
        'apikey' => $etherscanApiKey,
    )));
    $params_base = array(
        'module' => 'account',
        'startblock' => '0',
        'endblock' => '99999999',
        'sort' => 'desc',
        'apikey' => $etherscanApiKey,
    );
    $tx_list_api_url = str_replace('%25', '%', $base_url . http_build_query(array_merge($params_base, array(
        'action' => 'txlist',
        'address' => '%s',
    ))));
    $internal_tx_list_api_url = str_replace('%25', '%', $base_url . http_build_query(array_merge($params_base, array(
        'action' => 'txlistinternal',
        'address' => '%s',
    ))));
    $token_tx_list_api_url = str_replace('%25', '%', $base_url . http_build_query(array_merge($params_base, array(
        'action' => 'tokentx',
        'contractaddress' => '%s',
    ))));
    $nft_token_tx_list_by_address_api_url = str_replace('%25', '%', $base_url . http_build_query(array_merge($params_base, array(
        'action' => 'tokennfttx',
        'address' => '%s',
    ))));
    $nft_token_tx_list_by_contractaddres_api_url = str_replace('%25', '%', $base_url . http_build_query(array_merge($params_base, array(
        'action' => 'tokennfttx',
        'contractaddress' => '%s',
    ))));
    $erc1155_token_tx_list_by_address_api_url = null;
    // $erc1155_token_tx_list_by_address_api_url = str_replace('%25', '%', $base_url . http_build_query(array_merge($params_base, array(
    //     'action' => 'token1155tx',
    //     'address' => '%s',
    // ))));
    $erc1155_token_tx_list_by_contractaddress_api_url = null;
    // $erc1155_token_tx_list_by_contractaddress_api_url = str_replace('%25', '%', $base_url . http_build_query(array_merge($params_base, array(
    //     'action' => 'token1155tx',
    //     'contractaddress' => '%s',
    // ))));

    return array_merge($info ? $info : array(), array(
        'blockchain_name' => 'avalanche-mainnet',
        'blockchain_display_name' => 'Avalanche (C-Chain)',
        'interblock_period_seconds' => 2,
        'currency_ticker' => 'AVAX',
        'currency_name' => 'AVAX',
        'fungible_token_name' => 'ERC20',
        'bulk_balance_contract_address' => null,
        'multisend_contract_address' => null,
        'uniswap_v2_router_address' => null,
        'uniswap_v3_router_address' => null,
        'txhash_path_template' => 'https://snowtrace.io/tx/%s',
        'address_path_template' => 'https://snowtrace.io/address/%s',
        'nft_default_external_url_template' => null,
        'gas_price_api_url' => $gas_price_api_url,
        'ethprice_api_url' => $ethprice_api_url,
        'tx_list_api_url_template' => $tx_list_api_url,
        'internal_tx_list_api_url_template' => $internal_tx_list_api_url,
        'token_tx_list_api_url_template' => $token_tx_list_api_url,
        'nft_token_tx_list_by_contractaddress_api_url_template' => $nft_token_tx_list_by_contractaddres_api_url,
        'nft_token_tx_list_api_url_template' => $nft_token_tx_list_by_address_api_url,
        'erc1155_token_tx_list_by_contractaddress_api_url_template' => $erc1155_token_tx_list_by_contractaddress_api_url,
        'erc1155_token_tx_list_api_url_template' => $erc1155_token_tx_list_by_address_api_url,
        'base_explorer_display_url' => 'snowtrace.io',
        'explorer_api_url' => 'https://snowtrace.io/myapikey',
        'explorer_register_url' => 'https://snowtrace.io/register',
    ));
}, $ETHEREUMICOIO_BLOCKCHAINS_INFO_VERSION, 2);
