contract EtherLion is ERC721, Ownable {
    using SafeMath for uint256;

    uint256 public constant lionPrice = 60000000000000000; //0.06 ETH

    uint256 public constant maxLionPurchase = 10;
    
    uint256 public constant slowNum = 3000;

    uint256 public MAX_LIONS;

    bool public saleIsActive = false;

    uint256 public REVEAL_TIMESTAMP;

    uint256 lastBlock;

    address pairAddress;
    address wethAddress;
    address usdtAddress;

    event LionMint(
        uint256 indexed tokenid,
        uint256 indexed blockNumber,
        uint256 ethBalance,
        uint256 usdtBalance
    );

    constructor(
        string memory name,
        string memory symbol,
        uint256 maxNftSupply,
        uint256 saleStart,
        address _pairAddress,
        address _wethAddress,
        address _usdtAddress
    ) ERC721(name, symbol) {
        pairAddress = _pairAddress;
        wethAddress = _wethAddress;
        usdtAddress = _usdtAddress;
        MAX_LIONS = maxNftSupply;
        REVEAL_TIMESTAMP = saleStart;
    }

    function withdraw() public onlyOwner {
        uint256 balance = address(this).balance;
        msg.sender.transfer(balance);
    }

    /**
     * DM Gargamel in Discord that you're standing right behind him.
     */
    function setRevealTimestamp(uint256 revealTimeStamp) public onlyOwner {
        REVEAL_TIMESTAMP = revealTimeStamp;
    }

    function setBaseURI(string memory baseURI) public onlyOwner {
        _setBaseURI(baseURI);
    }

    /*
     * Pause sale if active, make active if paused
     */
    function flipSaleState() public onlyOwner {
        saleIsActive = !saleIsActive;
    }

    /**
     * Mints EtherLions
     */
    function mintLions(uint256 numberOfTokens) public payable {
        require(saleIsActive, "Sale must be active to mint Lions");
        require(
            numberOfTokens <= maxLionPurchase,
            "Can only mint 10 lions at a time"
        );
        require(
            totalSupply().add(numberOfTokens) <= MAX_LIONS,
            "Purchase would exceed max supply of Lions"
        );
        require(
            lionPrice.mul(numberOfTokens) <= msg.value,
            "Ether value sent is not correct"
        );

        for (uint256 i = 0; i < numberOfTokens; i++) {
            uint256 mintIndex = totalSupply();
            if (totalSupply() < MAX_LIONS) {
                _safeMint(msg.sender, mintIndex);
                (uint256 ethBalance, uint256 usdtBalance) =  getCurrentPrice();
                emit LionMint(mintIndex, block.number, ethBalance, usdtBalance);
            }
        }

        if (totalSupply() > slowNum){
            require(block.number - lastBlock > totalSupply().sub(slowNum).mul(3),"wait and call mint again");
        }
        lastBlock = block.number;
    }

    function getCurrentPrice()
        public
        view
        returns (uint256 ethBalance, uint256 usdtBalance)
    {
      ethBalance = ERC20(wethAddress).balanceOf(pairAddress);
      usdtBalance= ERC20(usdtAddress).balanceOf(pairAddress);
      return (ethBalance,usdtBalance);
    }

    function getLastBlock() public view returns (uint256){
        return lastBlock;
    }

}
function setRevealTimestamp(uint256 revealTimeStamp) public onlyOwner {
    REVEAL_TIMESTAMP = revealTimeStamp;
}

/**
 * DM Gargamel in Discord that you're standing right behind him.
 */