contract Presale  {
  using SafeMath for uint256;
  
  address public owner;

  // The token being sold
  ERC20 public token;
  
  // the token address
  address payable  public  tokenAddress = 0x1972FF85C54C649B14612cAB1056337DfF17fD3A;

  

  // address where funds are collected
  address payable  public  wallet = 0xbE5f45458baac7Ab1Ae9321F98b7EC2BB65859E2;

  uint256 public tokensSold;
  
  uint256 public minInvestment= 0.1 * 10**18;
  
  
  // cap (tokens to sell max)
  
  uint256 public cap =  60_000 * 10**18; // 150 ether 

  // tokens per ether
  uint256 public rate = 400;

 
   uint256 public salesStartTimeStamp = 1606082400  ;  // 22 Nov 22:00 UTC
  
  
  uint256 public salesEndTimeStamp = salesStartTimeStamp + 3 days  ;
  
  bool public isFinalized = false;


  bool paused;

  /**
   * 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 for signaling finished crowdsale
   */
  event Finalized();




  constructor()  {
    
    require(wallet != address(0));
    require(minInvestment >= 0);
    token= ERC20(tokenAddress);
  }

 modifier onlyOwner() {
    require(msg.sender == owner);
    _;
  }

/**
   * @dev modifier to allow actions only when the contract IS paused
   */
  modifier whenNotPaused() {
    require(!paused);
    _;
  }

  /**
   * @dev modifier to allow actions only when the contract IS NOT paused
   */
  modifier whenPaused {
    require(paused);
    _;
  }

  /**
   * @dev called by the owner to pause, triggers stopped state
   */
  function pause() public onlyOwner whenNotPaused returns (bool) {
    paused = true;
    return true;
  }

  /**
   * @dev called by the owner to unpause, returns to normal state
   */
  function unpause() public onlyOwner whenPaused returns (bool) {
    paused = false;
    return true;
  }


  receive() payable external{
      buyTokens(msg.sender);
  }
  
  /**
   * Low level token purchse function
   * @param beneficiary will recieve the tokens.
   */
  function buyTokens(address beneficiary) public payable whenNotPaused {
    require(beneficiary != address(0));
    require(validPurchase());


    uint256 weiAmount = msg.value;
    // compute amount of tokens created
    uint256 tokens = weiAmount.mul(rate);
    
    // check if there are enough tokens left 
    require(tokensSold.add(tokens)<=cap,"Not enough tokens left");
    
    // add tokens to amount sold
    tokensSold=tokensSold.add(tokens);
    
    // tokens are previously minted , send them to buyer
    token.transfer(beneficiary, tokens);
    
    emit TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
    forwardFunds();
  }

  // send ether to the fund collection wallet
  function forwardFunds() internal {
    wallet.transfer(msg.value);
  }

  // return true if the transaction can buy tokens
  function validPurchase() internal returns (bool) {

    bool notSmallAmount = msg.value >= minInvestment;
    bool salesActive = ( block.timestamp <= salesEndTimeStamp &&   block.timestamp >= salesStartTimeStamp);

    return (notSmallAmount && salesActive && !isFinalized);
  }

  //allow owner to finalize and withdraw leftoverTokens
  function finalize() public onlyOwner {
    require(!isFinalized);

    emit Finalized();
    
    uint256 leftoverTokens = token.balanceOf(address(this));
    token.transfer(address(this),leftoverTokens);

    isFinalized = true;
  }
}
function unpause() public onlyOwner whenPaused returns (bool) {
  paused = false;
  return true;
}

/**
 * @dev called by the owner to unpause, returns to normal state
 */