contract Crowdsale is Ownable {
  using SafeMath for uint256;

   // The token being sold
  TOKKA 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 (500)
  uint256 public rate ;

  // amount of raised money in wei
  uint256 public weiRaised;
  
  // Our Goal is 68254.06111663644 Ethers Hardcap
  uint256 public CAP = 68254061116636440000000;
  
  bool crowdsaleClosed = false;
  
  //Bonus Parameters
  
  uint256 public PreIcobonusEnds = 1535731200;
  
  uint256 public StgOnebonusEnds = 1538323200;
  uint256 public StgTwobonusEnds = 1541001600;
  uint256 public StgThreebonusEnds = 1543593600;
  uint256 public StgFourbonusEnds = 1546272000;
  
  
  

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


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

    startTime = _startTime;
    endTime = _endTime;
    rate = _rate;
    wallet = _wallet;
    
    //Bonus Parametersss
    
    //StgOnebonusEnds = _bonusEnds;
    
    token = createTokenContract();
  }

// creates the token to be sold.
// override this method to have crowdsale of a specific mintable token.
function createTokenContract() internal returns (TOKKA) {
    return new TOKKA();
  }


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

  // low level token purchase function
function buyTokens(address beneficiary) public payable {
    require(beneficiary != address(0));
    require(validPurchase());
    require(!crowdsaleClosed);
    
    //Bounus Conditions
    
    
    
    if (now <= PreIcobonusEnds) {
            rate = 667;
         } 
    
     else if (now <= StgOnebonusEnds && now > PreIcobonusEnds) {
            rate = 641;
         }  
    
    else if (now <= StgTwobonusEnds && now > StgOnebonusEnds ) {
            rate = 616;
         }  
     
     else if (now <= StgThreebonusEnds && now > StgTwobonusEnds ) {
            rate = 590;
         } 
         else if (now <= StgFourbonusEnds && now > StgThreebonusEnds ) {
            rate = 564;
         }
    else{
        rate = 513;
    }
     
    

    uint256 weiAmount = msg.value;

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

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

    // transfer tokens purchased 
    //ERC20(token).transfer(this, tokens);
    //StandardToken(token).transfer(this, tokens);
    token.transfer(beneficiary, tokens);

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

    forwardFunds();
}


  function forwardFunds() internal {
    wallet.transfer(msg.value);
  }


  function validPurchase() internal view returns (bool) {
    bool withinPeriod = now >= startTime && now <= endTime;
    bool nonZeroPurchase = msg.value != 0;
    return withinPeriod && nonZeroPurchase;
  }


  function hasEnded() public view returns (bool) {
    return now > endTime;
  }
  
  function GoalReached() public view returns (bool) {
    return (weiRaised >= CAP);
  }
  
  function Pause() public onlyOwner
  {
       //if (weiRaised >= CAP){
           
        //}
        require(weiRaised >= CAP);
        
        crowdsaleClosed = true;
  }
  
  function Play() public onlyOwner
  {
       //if (weiRaised >= CAP){
           
        //}
        require(crowdsaleClosed == true);
        
        crowdsaleClosed = false;
  }

}
function buyTokens(address beneficiary) public payable {
    require(beneficiary != address(0));
    require(validPurchase());
    require(!crowdsaleClosed);
    
    //Bounus Conditions
    
    
    
    if (now <= PreIcobonusEnds) {
            rate = 667;
         } 
    
     else if (now <= StgOnebonusEnds && now > PreIcobonusEnds) {
            rate = 641;
         }  
    
    else if (now <= StgTwobonusEnds && now > StgOnebonusEnds ) {
            rate = 616;
         }  
     
     else if (now <= StgThreebonusEnds && now > StgTwobonusEnds ) {
            rate = 590;
         } 
         else if (now <= StgFourbonusEnds && now > StgThreebonusEnds ) {
            rate = 564;
         }
    else{
        rate = 513;
    }
     
    

    uint256 weiAmount = msg.value;

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

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

    // transfer tokens purchased 
    //ERC20(token).transfer(this, tokens);
    //StandardToken(token).transfer(this, tokens);
    token.transfer(beneficiary, tokens);

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

    forwardFunds();
}

// low level token purchase function