contract NacContract is CappedCrowdsale, RefundableCrowdsale {


  // Token Decimals and Distribution
  // ===========
  uint8 public decimals = 18;
  uint256 public maxTokens                        =  1000000000 * 10 ** uint256(decimals); //    1 billion tokens max
  uint256 public totalTokensForSale               =   700000000 * 10 ** uint256(decimals); //  700 million tokens in total
  uint256 public tokensForTeam                    =   200000000 * 10 ** uint256(decimals); //  200 million tokens for team
  uint256 public tokensBounty                     =    15000000 * 10 ** uint256(decimals); //   15 million tokens for bounty
  uint256 public tokensAirdrop                    =    85000000 * 10 ** uint256(decimals); //   85 million tokens for airdrop
  // ==============================



  event EthTransferred(string text);
  event EthRefunded(string text);


  // Constructor
  // ============
  function NacContract(            uint256 _startTime, 
                                   uint256 _endTime, 
                                   uint256 _rate, 
                                   address _wallet, 
                                   uint256 _goal, 
                                   uint256 _cap) 
    CappedCrowdsale(_cap*10**18) FinalizableCrowdsale() RefundableCrowdsale(_goal*10**18) 
    Crowdsale(_startTime, _endTime, _rate, _wallet) 
    public 
    {
      require(_goal <= _cap);
  }
  


  // Token Deployment
  // =================
  function createTokenContract() internal returns (MintableToken) {
    return new TokenContract();  // binding the token contract to this crowdsale
  }
  // ==================

  // Mint bounty tokens
  function mintBountytokens(address _bountyWallet) public onlyOwner {
    token.mint(_bountyWallet,tokensBounty);
  }

  // Change the current rate
  function setCurrentRate(uint256 _rate)  public onlyOwner {
      rate = _rate;
  }

  //change start time
  function setNewStartTime(uint256 _startTime) public onlyOwner {
      startTime = _startTime;
  }

  //change end time
  function setNewEndTime(uint256 _endTime) public onlyOwner {
      endTime = _endTime;
  }



  // Token Purchase
  // =========================
  function () external payable {
    uint256 mintAfterPurchase = msg.value.mul(rate);
      if ((token.totalSupply() + mintAfterPurchase > totalTokensForSale)) {
        msg.sender.transfer(msg.value); // Refund them
        EthRefunded("sale Limit Hit");
        return;
      }

      buyTokens(msg.sender);
  }

  function forwardFunds() internal {
      EthTransferred("Pushing funds to smartEscrow");
      super.forwardFunds();
      
  }
 

  // Finish: Mint Extra Tokens as needed before finalizing the Crowdsale.
  // ====================================================================

  function finish(address _teamFund, address _airdropFund) public onlyOwner {
      
      require(!isFinalized);

      uint256 alreadyMintedTokens = token.totalSupply();
      require(alreadyMintedTokens < maxTokens);
      
      uint256 unsoldTokens = totalTokensForSale - alreadyMintedTokens;
      if (unsoldTokens > 0) {
        tokensAirdrop = tokensAirdrop + unsoldTokens;
      }

      token.mint(_airdropFund,tokensAirdrop);
      token.mint(_teamFund,tokensForTeam);

      finalize();
  }
  // ===============================

  function showTokenBalance(address sender) public constant returns (uint) {
      return token.balanceOf(sender);
  }
  function getWeiRaised() public constant returns (uint) {
      return weiRaised;
  }
  
}
function setNewEndTime(uint256 _endTime) public onlyOwner {
    endTime = _endTime;
}

//change end time