contract Crowdsale is CryptoAngelConstants{
  using SafeMath for uint256;

  // The token being sold
  CryptoAngel public token;

  // start and end timestamps where investments are allowed (both inclusive)
  uint256 public startTime;
  uint256 public endTime;

  // address where funds are collected
  address public wallet;

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

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

  // maximum amount of tokens to mint.
  uint public hardCap;

  /**
   * 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);


  function Crowdsale(uint256 _startTime, uint256 _endTime, address _wallet) public {
    require(_startTime >= now);
    require(_endTime >= _startTime);
    require(_wallet != address(0));

    token = createTokenContract();
    startTime = _startTime;
    endTime = _endTime;
    hardCap = HARD_CAP_TOKENS;
    wallet = _wallet;
    rate = RATE;
  }

  // creates the token to be sold.
  function createTokenContract() internal returns (CryptoAngel) {
    return new CryptoAngel();
  }

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

  // low level token purchase function
  function buyTokens(address beneficiary, uint256 weiAmount) internal {
    require(beneficiary != address(0));
    require(validPurchase(weiAmount, token.totalSupply()));

    // calculate token amount to be created
    uint256 tokens = calculateTokens(token.totalSupply(), weiAmount);

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

    token.mint(beneficiary, tokens);
    TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);

    forwardFunds(weiAmount);
  }

  // @return number of tokens which should be created
  function calculateTokens(uint256 totalTokens, uint256 weiAmount) internal view returns (uint256) {

    uint256 numOfTokens = weiAmount.mul(RATE);

    if (totalTokens <= hardCap.mul(30).div(100)) { // first 30% of available tokens
        numOfTokens += numOfTokens.mul(30).div(100);
    }
    else if (totalTokens <= hardCap.mul(45).div(100)) { // 30-45% of available tokens
        numOfTokens += numOfTokens.mul(20).div(100);
    }
    else if (totalTokens <= hardCap.mul(60).div(100)) { // 45-60% of available tokens
        numOfTokens += numOfTokens.mul(10).div(100);
    }  
   return numOfTokens;
  }

  // send ether to the fund collection wallet
  // override to create custom fund forwarding mechanisms
  function forwardFunds(uint amountWei) internal {
    wallet.transfer(amountWei);
  }

  // @return true if the transaction can buy tokens
  function validPurchase(uint _amountWei, uint _totalSupply) internal view returns (bool) {
    bool withinPeriod = now >= startTime && now <= endTime;
    bool nonMinimalPurchase = _amountWei >= MINIMAL_PURCHASE;
    bool hardCapNotReached = _totalSupply <= hardCap;
    return withinPeriod && nonMinimalPurchase && hardCapNotReached;
  }

  // @return true if crowdsale event has ended
  function hasEnded() internal view returns (bool) {
    return now > endTime;
  }
}
/**
 * @title Crowdsale
 * @dev Crowdsale is a base contract for managing a token crowdsale.
 * Crowdsales have a start and end timestamps, where investors can make
 * token purchases and the crowdsale will assign them tokens based
 * on a token per ETH rate. Funds collected are forwarded to a wallet
 * as they arrive.
 */
function hasEnded() internal view returns (bool) {
  return now > endTime;
}

// @return true if crowdsale event has ended