contract LUV_Crowdsale is Ownable {
  using SafeMath for uint256;

  // The token being sold
  LUVTOKEN public token;

  // start and end timestamps where investments are allowed (both inclusive)


  uint256 public startTime = 1523750400;
  uint256 public phase_1_Time = 1526342400 ;
  uint256 public phase_2_Time = 1529020800;
  uint256 public endTime = 1531612800;

  // Max amount of wei accepted in the crowdsale
  uint256 public cap;
  
  // Min amount of wei an investor can send
  uint256 public minInvest;
  
  
  // address where funds are collected
  address public wallet;

  // how many token units a buyer gets. 1 ETH = 10000 LUV
  uint256 public phase_1_rate = 13000;
  uint256 public phase_2_rate = 12000;
  uint256 public phase_3_rate = 11000;
  
  
  // amount of raised money in wei
  uint256 public weiRaised;

  mapping (address => uint256) rates;

  function getRate() constant returns (uint256){
    uint256 current_time = now;

    if(current_time > startTime && current_time < phase_1_Time){
      return phase_1_rate;
    }
    else if(current_time > phase_1_Time && current_time < phase_2_Time){
      return phase_2_rate;
    }
      else if(current_time > phase_2_Time && current_time < endTime){
      return phase_3_rate;
    }
      
  }

  /**
   * 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 LUV_Crowdsale() {
    wallet = msg.sender;
    token = createTokenContract();
    minInvest = 0.1 * 1 ether;
    cap = 100000 * 1 ether;
  }

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


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

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

    uint256 weiAmount = msg.value;

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

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

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

    forwardFunds();
  }

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

  // @return true if the transaction can buy tokens
  function validPurchase() internal constant returns (bool) {
    bool withinPeriod = now >= startTime && now <= endTime;
    bool nonZeroPurchase = msg.value != 0;
    return withinPeriod && nonZeroPurchase;
  }

  // @return true if crowdsale event has ended
  function hasEnded() public constant returns (bool) {
    return now > endTime;
  }
  
/**
 * @notice Terminate contract and refund to owner
 */
 function destroy() onlyOwner {
     // Transfer tokens back to owner
     uint256 balance = token.balanceOf(this);
     assert (balance > 0);
     token.transfer(owner,balance);
     
     // There should be no ether in the contract but just in case
     selfdestruct(owner);
     
 }

}
/**
 * @title Token 
 * @dev API interface for interacting with the LUVTOKEN contract
 * /
 interface Token {
 function transfer (address _to, uint256 _value) returns (bool);
 function balanceOf (address_owner) constant returns (uint256 balance);
 }
 
 /**
 * @title LUV_Crowdsale
 * @dev HDK_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 forwardFunds() internal {
  wallet.transfer(msg.value);
}

// send ether to the fund collection wallet
// override to create custom fund forwarding mechanisms