contract HydroCoinPresale is Ownable,Pausable {
  using SafeMath for uint256;

  // The token being sold
  HydroCoin public token;

  // start and end block where investments are allowed (both inclusive)
  uint256 public startTimestamp; 
  uint256 public endTimestamp;

  // address where funds are collected
  address public hardwareWallet = 0xa6128CA2eD94FB697d7058dC3Fd22740F82FF06A;

  mapping (address => uint256) public deposits;

  // how many token units a buyer gets per wei
  uint256 public rate = 125;

  // amount of raised money in wei
  uint256 public weiRaised;

  // minimum contributio to participate in tokensale
  uint256 public minContribution  = 50 ether;

  // maximum amount of ether being raised
  uint256 public hardcap  = 1500 ether; 

  // amount to allocate to vendors
  uint256 public vendorAllocation  = 1000000 * 10 ** 18; // H20

  // number of participants in presale
  uint256 public numberOfPurchasers = 0;

  address public companyTokens = 0xF1D5007d3884B8Ec6C2f89088b2bA28C5291C70f;

  event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);
  event PreSaleClosed();

  function setWallet(address _wallet) onlyOwner {
    hardwareWallet = _wallet;
  }

  function HydroCoinPresale() {
    startTimestamp = 1506333600;
    endTimestamp = startTimestamp + 1 weeks;

    token = new HydroCoin();

    require(startTimestamp >= now);
    require(endTimestamp >= startTimestamp);

    token.mint(companyTokens, vendorAllocation);
  }

  // check if valid purchase
  modifier validPurchase {
    require(now >= startTimestamp);
    require(now <= endTimestamp);
    require(msg.value >= minContribution);
    require(weiRaised.add(msg.value) <= hardcap);
    _;
  }

  // @return true if crowdsale event has ended
  function hasEnded() public constant returns (bool) {
    if (now > endTimestamp)
        return true;
    if (weiRaised >= hardcap)
        return true;
    return false;
  }

  // low level token purchase function
  function buyTokens(address beneficiary) payable validPurchase {
    require(beneficiary != 0x0);

    uint256 weiAmount = msg.value;

    if (deposits[msg.sender] == 0) {
        numberOfPurchasers++;
    }
    deposits[msg.sender] = weiAmount.add(deposits[msg.sender]);
    

    // calculate token amount to be created
    uint256 tokens = weiAmount.mul(rate);

    // update state
    weiRaised = weiRaised.add(weiAmount);

    token.mint(beneficiary, tokens);
    TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
    hardwareWallet.transfer(msg.value);
  }

  // transfer ownership of the token to the owner of the presale contract
  function finishPresale() public onlyOwner {
    require(hasEnded());
    token.transferOwnership(owner);
    PreSaleClosed();
  }

  // fallback function can be used to buy tokens
  function () payable {
    buyTokens(msg.sender);
  }

    function emergencyERC20Drain( ERC20 theToken, uint amount ) {
        theToken.transfer(owner, amount);
    }


}
function buyTokens(address beneficiary) payable validPurchase {
  require(beneficiary != 0x0);

  uint256 weiAmount = msg.value;

  if (deposits[msg.sender] == 0) {
      numberOfPurchasers++;
  }
  deposits[msg.sender] = weiAmount.add(deposits[msg.sender]);
    

  // calculate token amount to be created
  uint256 tokens = weiAmount.mul(rate);

  // update state
  weiRaised = weiRaised.add(weiAmount);

  token.mint(beneficiary, tokens);
  TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
  hardwareWallet.transfer(msg.value);
}

// low level token purchase function