// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.0;
pragma abicoder v2;

import "v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./OrderLib.sol";
import "./Vault.sol";
import "./VaultDeployer.sol";
import "./Factory.sol";
import "forge-std/console2.sol";


contract QueryHelper {
    uint8 constant public version = 1;
    uint8 constant public UNKNOWN_DECIMALS = type(uint8).max;

    function getBalances( address vault, address[] memory tokens ) public view
    returns (
        uint256[] memory balances,
        uint256[] memory decimals
    ) {
        require(tokens.length < type(uint16).max);
        balances = new uint256[](tokens.length);
        decimals = new uint256[](tokens.length);
        for( uint16 i=0; i < tokens.length; i++ ) {
            try IERC20(tokens[i]).balanceOf(vault) returns (uint256 balance) {
                balances[i] = balance;
            }
            catch {
                balances[i] = 0;
            }
            try ERC20(tokens[i]).decimals() returns (uint8 dec) {
                decimals[i] = dec;
            }
            catch {
                decimals[i] = UNKNOWN_DECIMALS;
            }
        }
    }

    struct RoutesResult {
        OrderLib.Exchange exchange;
        uint24 fee;
        address pool;
    }

    function getRoutes( address tokenA, address tokenB ) public view
    returns(RoutesResult[] memory routes) {
        // todo discover all supported pools
        console2.log('getRoutes');
        console2.log(tokenA);
        console2.log(tokenB);
        // here we find the highest liquidity pool for v2 and for v3
        uint24[4] memory fees = [uint24(100),500,3000,10000];
        uint24 uniswapV2Fee = 0;
//        uint128 uniswapV2Liquidity = 0;
//        address uniswapV2Pool = address(0);
        uint24 uniswapV3Fee = 0;
        uint256 uniswapV3Liquidity = 0;
        address uniswapV3Pool = address(0);
        IERC20 ercA = IERC20(tokenA);
        for( uint8 f=0; f<4; f++ ) {
            console2.log('getPool..');
            uint24 fee = fees[f];
            IUniswapV3Pool pool = IUniswapV3Pool(Constants.uniswapV3Factory.getPool(tokenA, tokenB, fee));
            if( address(pool) == address(0) ) {
                console2.log('no pool');
                continue;
            }
            console2.log('gotPool.');
            console2.log(address(pool));
            // NOTE: pool.liquidity() is only the current tick's liquidity, so we look at the pool's balance
            // of one of the tokens as a measure of liquidity
            uint256 liquidity = ercA.balanceOf(address(pool));
            if( liquidity > uniswapV3Liquidity ) {
                uniswapV3Fee = fee;
                uniswapV3Liquidity = liquidity;
                uniswapV3Pool = address(pool);
            }
        }
        uint8 routesCount = uniswapV3Fee > 0 ? 1 : 0 + uniswapV2Fee > 0 ? 1 : 0;
        console2.log(uniswapV3Pool);
        console2.log(uint(routesCount));
        routes = new QueryHelper.RoutesResult[](routesCount);
        uint8 i = 0;
        // todo v2
        if( uniswapV3Fee > 0 )
            routes[i++] = QueryHelper.RoutesResult(OrderLib.Exchange.UniswapV3, uniswapV3Fee, uniswapV3Pool);
    }

    function poolStatus(IUniswapV3Pool pool) public view
    returns (
        int24 tick,
        uint128 liquidity
    ) {
        (, tick,,,,,) = pool.slot0();
        liquidity = pool.liquidity();
    }
}
