contract PrivateroundCrowdsale is Ownable{
 // using SafeMath for uint256;

  // The token being sold
  ERC20 public token;

  // Address where funds are collected
  address payable wallet;

  // How many token units a buyer gets per wei
  uint256 public rate;

  // Amount of wei raised
  uint256 public weiRaised;
  
  //uint256 public unlockDate = 1630324800; //August 1 , 2021 . 12:00 PM
  
    
    mapping (address => uint256) public vestPercent;
    mapping (address => uint256) public lastWithdraw;
    mapping(address => uint256) public tokenHolders;
    mapping(address => bool) public vestUnlocked;
  
  address public PrivateRoundEquityHolder = 0x7Bdb94900F020850808221a068A946a2e262dFa0;

  /**
   * Event for token purchase logging
   * @param purchaser who paid for the tokens
   * @param beneficiary who got the tokens
   * @param value weis paid for purchase
   * @param amount amount of tokens purchased
   */
  event TokenPurchase(
    address indexed purchaser,
    address indexed beneficiary,
    uint256 value,
    uint256 amount
  );

  event tokenWithdrawn(address indexed purchaser, uint256 amount);
  constructor() {
  
    rate = 51361;
    wallet = payable(0x0ce2B35f93c5D247a74E9E8bf37BFCCF78EaeEd9);
    token = ERC20(0x313c7d0D6cAfD295A1f0433265F3d5ee74BFBa45);

    
  }

  receive() external payable {
    buyTokens(msg.sender);
  }

  
  function buyTokens(address _beneficiary) public payable {

    uint256 weiAmount = msg.value;
    _preValidatePurchase(_beneficiary, weiAmount);

    // calculate token amount to be created
    uint256 tokens = _getTokenAmount(weiAmount);
    tokenHolders[_beneficiary] = tokens;
    lastWithdraw[_beneficiary] = block.timestamp;
    getPercent(tokens, _beneficiary);

    // update state
    weiRaised = weiRaised + weiAmount;

  
    _forwardFunds();
   
  }

  // -----------------------------------------
  // Internal interface (extensible)
  // -----------------------------------------

  /**
   * @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met. Use super to concatenate validations.
   * @param _beneficiary Address performing the token purchase
   * @param _weiAmount Value in wei involved in the purchase
   */
  function _preValidatePurchase(
    address _beneficiary,
    uint256 _weiAmount
  )
    internal pure
  {
    require(_beneficiary != address(0));
    require(_weiAmount != 0);
  }

  /**
   * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
   * @param _beneficiary Address performing the token purchase
   * @param _tokenAmount Number of tokens to be emitted
   */
  function _deliverTokens(
    address _beneficiary,
    uint256 _tokenAmount
  )
    internal
  {
    token.transfer(_beneficiary, _tokenAmount);
  }


  /**
   * @dev Override to extend the way in which ether is converted to tokens.
   * @param _weiAmount Value in wei to be converted into tokens
   * @return Number of tokens that can be purchased with the specified _weiAmount
   */
  function _getTokenAmount(uint256 _weiAmount)
    internal view returns (uint256)
  {
    return (_weiAmount *rate)/(10**8) ;
  }

  /**
   * @dev Determines how ETH is stored/forwarded on purchases.
   */
  function _forwardFunds() internal {
    wallet.transfer(msg.value);
  }
  function getPercent(uint256 amount,address beneficiary) internal returns(uint256)
  {
      uint256 _getPercent = (amount * 417/100)/100;
      vestPercent[beneficiary] = _getPercent;
      return _getPercent;
  }
  function unlocktoVest(address beneficiary) public {
      require(block.timestamp >= lastWithdraw[beneficiary] + 120 days, "Lock Period");
      require(vestUnlocked[beneficiary] == false, "You have already unlocked the vest");
        vestUnlocked[beneficiary] = true;
        lastWithdraw[beneficiary] = block.timestamp;
       
    }
    
    function VestEveryMonth(address beneficiary) public
    {
        require(vestUnlocked[beneficiary] == true, "Get your vest unlocked first");
        require( block.timestamp >= lastWithdraw[beneficiary] + 30 days, " you can not withdraw before 1 month");
        _deliverTokens(beneficiary, vestPercent[beneficiary]);
        tokenHolders[beneficiary] = tokenHolders[beneficiary] - vestPercent[beneficiary];
        emit tokenWithdrawn(beneficiary, vestPercent[beneficiary]);
      
    }
    //change rate --the number of tokens for 1 ether
    function changePrice(uint256 amount) public onlyOwner
    {
        rate = amount;
    }
  
   function queryERC20Balance(address _addressToQuery) view public onlyOwner returns (uint) {
        uint256 contractBalance = token.balanceOf(_addressToQuery);
        return contractBalance;
    }

  
  function sendTokensBack(address addressToQuery) public onlyOwner
  {
    uint256 contractBalance = token.balanceOf(addressToQuery);
    token.transfer(PrivateRoundEquityHolder, contractBalance);
  }
}
function _getTokenAmount(uint256 _weiAmount)
  internal view returns (uint256)
{
  return (_weiAmount *rate)/(10**8) ;
}

/**
 * @dev Override to extend the way in which ether is converted to tokens.
 * @param _weiAmount Value in wei to be converted into tokens
 * @return Number of tokens that can be purchased with the specified _weiAmount
 */