contract ZapIn_711_V2 is ReentrancyGuard, Ownable, ADDRESSBOOK {
    using SafeMath for uint256;
    using Address for address;
    using SafeERC20 for IERC20;

    event AmountToAgent(address indexed from, address indexed agent, uint256 ethAmount);
    event AmountToVault(address indexed from, uint256 ethAmount);
    event AmountToInvest(address indexed from, uint256 ethAmount, uint256 lpBought);

    mapping (address => uint256) public referralRewards;

    bool public stopped = false;
    uint16 public goodwill;
    address payable public vault;
    address public stakingContract;
    IAgentRegistry public agentRegistry;

    IUniswapV2Router02 private constant uniswapRouter = IUniswapV2Router02(
        0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
    );
    IUniswapV2Factory
        private constant UniSwapV2FactoryAddress = IUniswapV2Factory(
        0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f
    );
    uint256
        private constant deadline = 0xf000000000000000000000000000000000000000000000000000000000000000;
    address
        private constant wethTokenAddress = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    address
        private constant ethTokenAddress = address(0); // only accepts ETH

    constructor() public {
        goodwill = 1000;
        vault = VAULT;
        stakingContract = STAKING_CONTRACT;
        agentRegistry = IAgentRegistry(AGENT_REGISTRY);
    }

    // circuit breaker modifiers
    modifier stopInEmergency {
        if (stopped) {
            revert("Temporarily Paused");
        } else {
            _;
        }
    }

    // Zap in with referral id
    function ZapInWithId(
        address _staker,
        uint256 _minPoolTokens,
        uint256 _agentId
    ) public payable stopInEmergency returns (uint256) {
        //query agent
        address payable _agent = agentRegistry.getAgentAddressById(_agentId);
        ZapIn(_staker, _minPoolTokens, _agent);
    }

    // Zap in with referral name
    function ZapInWithName(
        address _staker,
        uint256 _minPoolTokens,
        bytes32 _agentName
    ) public payable stopInEmergency returns (uint256) {
        //query agent
        address payable _agent = agentRegistry.getAgentAddressByName(_agentName);
        ZapIn(_staker, _minPoolTokens, _agent);
    }

    /**
    @notice This function is used to single sided add liquidity to Uniswap V2 ETH/7-11 pair and stake
    @param _minPoolTokens Reverts if less tokens received than this
    @return Amount of stake amount
     */
    function ZapIn(
        address _staker,
        uint256 _minPoolTokens,
        address payable _agent
    ) public payable nonReentrant stopInEmergency returns (uint256) {
        require(msg.value > 0, "Error: ETH not sent");
        if(msg.sender == _agent || !(agentRegistry.isAgent(_agent))){
            _agent = address(0);
        }
        
        // Default
        uint256 toVault = msg.value.mul(goodwill).div(10000); // 10% to vault
        uint256 toInvest = msg.value.sub(toVault); // 90% to swap
        
        // And if it is referred by an agent
        if(_agent != address(0)){
            toVault = msg.value.mul(500).div(10000); // 5% to vault
            toInvest = msg.value.mul(9250).div(10000); // 92.5% to swap
            uint256 toAgent = msg.value.sub(toVault).sub(toInvest); // 2.5% to agent
            // Transfer to agnet
            _agent.transfer(toAgent);
            referralRewards[_agent] = referralRewards[_agent].add(toAgent);
            emit AmountToAgent(msg.sender, _agent, toAgent);
        }

        // Transfer to vault
        vault.transfer(toVault);
        emit AmountToVault(msg.sender, toVault);
        // Single sided add liquidity
        uint256 LPBought = _performZapIn(
            _staker,
            ethTokenAddress,
            TOKEN_ADDRESS_711,
            wethTokenAddress,
            toInvest
        );
        emit AmountToInvest(msg.sender, toInvest, LPBought);
        require(LPBought >= _minPoolTokens, "ERR: High Slippage");

        // Stake LP token
        IERC20(UNISWAP_V2_PAIR_711_WETH).safeApprove(stakingContract, LPBought);
        IStakingRewards(stakingContract).stakeFor(_staker, LPBought);

        return LPBought;
    }

    function _performZapIn(
        address _toWhomToIssue,
        address _FromTokenContractAddress,
        address _ToUnipoolToken0,
        address _ToUnipoolToken1,
        uint256 _amount
    ) internal returns (uint256) {
        address intermediate = _getIntermediate(
            _FromTokenContractAddress,
            _amount,
            _ToUnipoolToken0,
            _ToUnipoolToken1
        );

        // swap to intermediate
        uint256 interAmt = _token2Token(
            _FromTokenContractAddress,
            intermediate,
            _amount
        );

        // divide to swap in amounts
        uint256 token0Bought;
        uint256 token1Bought;

        IUniswapV2Pair pair = IUniswapV2Pair(
            UniSwapV2FactoryAddress.getPair(_ToUnipoolToken0, _ToUnipoolToken1)
        );
        (uint256 res0, uint256 res1, ) = pair.getReserves();

        if (intermediate == _ToUnipoolToken0) {
            uint256 amountToSwap = calculateSwapInAmount(res0, interAmt);
            //if no reserve or a new pair is created
            if (amountToSwap <= 0) amountToSwap = interAmt.div(2);
            token1Bought = _token2Token(
                intermediate,
                _ToUnipoolToken1,
                amountToSwap
            );
            token0Bought = interAmt.sub(amountToSwap);
        } else {
            uint256 amountToSwap = calculateSwapInAmount(res1, interAmt);
            //if no reserve or a new pair is created
            if (amountToSwap <= 0) amountToSwap = interAmt.div(2);
            token0Bought = _token2Token(
                intermediate,
                _ToUnipoolToken0,
                amountToSwap
            );
            token1Bought = interAmt.sub(amountToSwap);
        }

        return
            _uniDeposit(
                _toWhomToIssue,
                _ToUnipoolToken0,
                _ToUnipoolToken1,
                token0Bought,
                token1Bought
            );
    }

    function _uniDeposit(
        address _toWhomToIssue,
        address _ToUnipoolToken0,
        address _ToUnipoolToken1,
        uint256 token0Bought,
        uint256 token1Bought
    ) internal returns (uint256) {
        IERC20(_ToUnipoolToken0).safeIncreaseAllowance(
            address(uniswapRouter),
            token0Bought
        );
        IERC20(_ToUnipoolToken1).safeIncreaseAllowance(
            address(uniswapRouter),
            token1Bought
        );

        (uint256 amountA, uint256 amountB, uint256 LP) = uniswapRouter
            .addLiquidity(
            _ToUnipoolToken0,
            _ToUnipoolToken1,
            token0Bought,
            token1Bought,
            1,
            1,
            address(this),
            deadline
        );

        //Returning Residue in token0, if any.
        if (token0Bought.sub(amountA) > 0) {
            IERC20(_ToUnipoolToken0).safeTransfer(
                _toWhomToIssue,
                token0Bought.sub(amountA)
            );
        }

        //Returning Residue in token1, if any
        if (token1Bought.sub(amountB) > 0) {
            IERC20(_ToUnipoolToken1).safeTransfer(
                _toWhomToIssue,
                token1Bought.sub(amountB)
            );
        }

        return LP;
    }

    function _getIntermediate(
        address _FromTokenContractAddress,
        uint256 _amount,
        address _ToUnipoolToken0,
        address _ToUnipoolToken1
    ) internal view returns (address) {
        // set from to weth for eth input
        if (_FromTokenContractAddress == address(0)) {
            _FromTokenContractAddress = wethTokenAddress;
        }

        if (_FromTokenContractAddress == _ToUnipoolToken0) {
            return _ToUnipoolToken0;
        } else if (_FromTokenContractAddress == _ToUnipoolToken1) {
            return _ToUnipoolToken1;
        } else {
            IUniswapV2Pair pair = IUniswapV2Pair(
                UniSwapV2FactoryAddress.getPair(
                    _ToUnipoolToken0,
                    _ToUnipoolToken1
                )
            );
            (uint256 res0, uint256 res1, ) = pair.getReserves();

            uint256 ratio;
            bool isToken0Numerator;
            if (res0 >= res1) {
                ratio = res0 / res1;
                isToken0Numerator = true;
            } else {
                ratio = res1 / res0;
            }

            //find outputs on swap
            uint256 output0 = _calculateSwapOutput(
                _FromTokenContractAddress,
                _amount,
                _ToUnipoolToken0
            );
            uint256 output1 = _calculateSwapOutput(
                _FromTokenContractAddress,
                _amount,
                _ToUnipoolToken1
            );

            if (isToken0Numerator) {
                if (output1 * ratio >= output0) return _ToUnipoolToken1;
                else return _ToUnipoolToken0;
            } else {
                if (output0 * ratio >= output1) return _ToUnipoolToken0;
                else return _ToUnipoolToken1;
            }
        }
    }

    function _calculateSwapOutput(
        address _from,
        uint256 _amt,
        address _to
    ) internal view returns (uint256) {
        // check output via tokenA -> tokenB
        address pairA = UniSwapV2FactoryAddress.getPair(_from, _to);

        uint256 amtA;
        if (pairA != address(0)) {
            address[] memory pathA = new address[](2);
            pathA[0] = _from;
            pathA[1] = _to;

            amtA = uniswapRouter.getAmountsOut(_amt, pathA)[1];
        }

        uint256 amtB;
        // check output via tokenA -> weth -> tokenB
        if ((_from != wethTokenAddress) && _to != wethTokenAddress) {
            address[] memory pathB = new address[](3);
            pathB[0] = _from;
            pathB[1] = wethTokenAddress;
            pathB[2] = _to;

            amtB = uniswapRouter.getAmountsOut(_amt, pathB)[2];
        }

        if (amtA >= amtB) {
            return amtA;
        } else {
            return amtB;
        }
    }

    function calculateSwapInAmount(uint256 reserveIn, uint256 userIn)
        public
        pure
        returns (uint256)
    {
        return
            Babylonian
                .sqrt(
                reserveIn.mul(userIn.mul(3988000) + reserveIn.mul(3988009))
            )
                .sub(reserveIn.mul(1997)) / 1994;
    }

    /**
    @notice This function is used to swap ETH/ERC20 <> ETH/ERC20
    @param _FromTokenContractAddress The token address to swap from. (0x00 for ETH)
    @param _ToTokenContractAddress The token address to swap to. (0x00 for ETH)
    @param tokens2Trade The amount of tokens to swap
    @return tokenBought The quantity of tokens bought
    */
    function _token2Token(
        address _FromTokenContractAddress,
        address _ToTokenContractAddress,
        uint256 tokens2Trade
    ) internal returns (uint256 tokenBought) {
        if (_FromTokenContractAddress == _ToTokenContractAddress) {
            return tokens2Trade;
        }

        if (_FromTokenContractAddress == address(0)) {
            if (_ToTokenContractAddress == wethTokenAddress) {
                IWETH(wethTokenAddress).deposit.value(tokens2Trade)();
                return tokens2Trade;
            }

            address[] memory path = new address[](2);
            path[0] = wethTokenAddress;
            path[1] = _ToTokenContractAddress;
            tokenBought = uniswapRouter.swapExactETHForTokens.value(
                tokens2Trade
            )(1, path, address(this), deadline)[path.length - 1];
        } else if (_ToTokenContractAddress == address(0)) {
            if (_FromTokenContractAddress == wethTokenAddress) {
                IWETH(wethTokenAddress).withdraw(tokens2Trade);
                return tokens2Trade;
            }

            IERC20(_FromTokenContractAddress).safeIncreaseAllowance(
                address(uniswapRouter),
                tokens2Trade
            );

            address[] memory path = new address[](2);
            path[0] = _FromTokenContractAddress;
            path[1] = wethTokenAddress;
            tokenBought = uniswapRouter.swapExactTokensForETH(
                tokens2Trade,
                1,
                path,
                address(this),
                deadline
            )[path.length - 1];
        } else {
            IERC20(_FromTokenContractAddress).safeIncreaseAllowance(
                address(uniswapRouter),
                tokens2Trade
            );

            if (_FromTokenContractAddress != wethTokenAddress) {
                if (_ToTokenContractAddress != wethTokenAddress) {
                    // check output via tokenA -> tokenB
                    address pairA = UniSwapV2FactoryAddress.getPair(
                        _FromTokenContractAddress,
                        _ToTokenContractAddress
                    );
                    address[] memory pathA = new address[](2);
                    pathA[0] = _FromTokenContractAddress;
                    pathA[1] = _ToTokenContractAddress;
                    uint256 amtA;
                    if (pairA != address(0)) {
                        amtA = uniswapRouter.getAmountsOut(
                            tokens2Trade,
                            pathA
                        )[1];
                    }

                    // check output via tokenA -> weth -> tokenB
                    address[] memory pathB = new address[](3);
                    pathB[0] = _FromTokenContractAddress;
                    pathB[1] = wethTokenAddress;
                    pathB[2] = _ToTokenContractAddress;

                    uint256 amtB = uniswapRouter.getAmountsOut(
                        tokens2Trade,
                        pathB
                    )[2];

                    if (amtA >= amtB) {
                        tokenBought = uniswapRouter.swapExactTokensForTokens(
                            tokens2Trade,
                            1,
                            pathA,
                            address(this),
                            deadline
                        )[pathA.length - 1];
                    } else {
                        tokenBought = uniswapRouter.swapExactTokensForTokens(
                            tokens2Trade,
                            1,
                            pathB,
                            address(this),
                            deadline
                        )[pathB.length - 1];
                    }
                } else {
                    address[] memory path = new address[](2);
                    path[0] = _FromTokenContractAddress;
                    path[1] = wethTokenAddress;

                    tokenBought = uniswapRouter.swapExactTokensForTokens(
                        tokens2Trade,
                        1,
                        path,
                        address(this),
                        deadline
                    )[path.length - 1];
                }
            } else {
                address[] memory path = new address[](2);
                path[0] = wethTokenAddress;
                path[1] = _ToTokenContractAddress;
                tokenBought = uniswapRouter.swapExactTokensForTokens(
                    tokens2Trade,
                    1,
                    path,
                    address(this),
                    deadline
                )[path.length - 1];
            }
        }
        require(tokenBought > 0, "Error Swapping Tokens");
    }

    /**
    @notice This function is used to calculate and transfer goodwill
    @param _tokenContractAddress Token in which goodwill is deducted
    @param tokens2Trade The total amount of tokens to be zapped in
    @return The quantity of goodwill deducted
     */
    function _transferGoodwill(
        address _tokenContractAddress,
        uint256 tokens2Trade
    ) internal returns (uint256 goodwillPortion) {
        goodwillPortion = SafeMath.div(
            SafeMath.mul(tokens2Trade, goodwill),
            10000
        );

        if (goodwillPortion == 0) {
            return 0;
        }

        IERC20(_tokenContractAddress).safeTransfer(
            vault,
            goodwillPortion
        );
    }

    function set_new_goodwill(uint16 _new_goodwill) public onlyOwner {
        require(
            _new_goodwill >= 0 && _new_goodwill < 10000,
            "GoodWill Value not allowed"
        );
        goodwill = _new_goodwill;
    }

    function inCaseTokengetsStuck(IERC20 _TokenAddress) public onlyOwner {
        uint256 qty = _TokenAddress.balanceOf(address(this));
        _TokenAddress.safeTransfer(owner(), qty);
    }

    // - to Pause the contract
    function toggleContractActive() public onlyOwner {
        stopped = !stopped;
    }

    // - to withdraw any ETH balance sitting in the contract
    function withdraw() public onlyOwner {
        uint256 contractBalance = address(this).balance;
        address payable _to = owner().toPayable();
        _to.transfer(contractBalance);
    }

    function() external payable {
        require(msg.sender != tx.origin, "Do not send ETH directly");
    }
}
function ZapInWithName(
    address _staker,
    uint256 _minPoolTokens,
    bytes32 _agentName
) public payable stopInEmergency returns (uint256) {
    //query agent
    address payable _agent = agentRegistry.getAgentAddressByName(_agentName);
    ZapIn(_staker, _minPoolTokens, _agent);
}

// Zap in with referral name