contract Crowdsale is TokenConfig, Pausable, ReentrancyGuard, Whitelist {
  using SafeMath for uint256;
  using SafeERC20 for IERC20;

  // The token being sold
  IERC20 private _token;

  // Address where funds are collected
  address private _wallet;

  // Address where funds are collected
  address private _tokenholder;

  // How many token units a buyer gets per wei.
  // The rate is the conversion between wei and the smallest and indivisible token unit.
  // So, if you are using a rate of 1 with a ERC20Detailed token with 3 decimals called TOK
  // 1 wei will give you 1 unit, or 0.001 TOK.
  uint256 private _rate;

  // Amount of contribution wei raised
  uint256 private _weiRaised;

  // Amount of token sold
  uint256 private _tokenSoldAmount;

  // Minimum contribution amount of fund
  uint256 private _minWeiAmount;

  // balances of user should be sent
  mapping (address => uint256) private _tokenBalances;

  // balances of user fund
  mapping (address => uint256) private _weiBalances;

  // ICO period timestamp
  uint256 private _openingTime;
  uint256 private _closingTime;

  // Amount of token hardcap
  uint256 private _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 TokensPurchased(
    address indexed purchaser,
    address indexed beneficiary,
    uint256 value,
    uint256 amount
  );
  event TokensDelivered(address indexed beneficiary, uint256 amount);
  event RateChanged(uint256 rate);
  event MinWeiChanged(uint256 minWei);
  event PeriodChanged(uint256 open, uint256 close);
  event HardcapChanged(uint256 hardcap);

  constructor(
    uint256 rate,
    uint256 minWeiAmount,
    address wallet,
    address tokenholder,
    IERC20 token,
    uint256 hardcap,
    uint256 openingTime,
    uint256 closingTime
  ) public {
    require(rate > 0, "Rate is lower than zero.");
    require(wallet != address(0), "Wallet address is zero");
    require(tokenholder != address(0), "Tokenholder address is zero");
    require(token != address(0), "Token address is zero");

    _rate = rate;
    _minWeiAmount = minWeiAmount;
    _wallet = wallet;
    _tokenholder = tokenholder;
    _token = token;
    _hardcap = hardcap;
    _openingTime = openingTime;
    _closingTime = closingTime;
  }

  // -----------------------------------------
  // Crowdsale external interface
  // -----------------------------------------

  /**
   * @dev fallback function ***DO NOT OVERRIDE***
   * Note that other contracts will transfer fund with a base gas stipend
   * of 2300, which is not enough to call buyTokens. Consider calling
   * buyTokens directly when purchasing tokens from a contract.
   */
  function () external payable {
    buyTokens(msg.sender);
  }

  /**
   * @return the token being sold.
   */
  function token() public view returns(IERC20) {
    return _token;
  }

  /**
   * @return token hardcap.
   */
  function hardcap() public view returns(uint256) {
    return _hardcap;
  }

  /**
   * @return the address where funds are collected.
   */
  function wallet() public view returns(address) {
    return _wallet;
  }

  /**
   * @return the number of token units a buyer gets per wei.
   */
  function rate() public view returns(uint256) {
    return _rate;
  }

  /**
   * @return the amount of wei raised.
   */
  function weiRaised() public view returns (uint256) {
    return _weiRaised;
  }

  /**
   * @return ICO opening time.
   */
  function openingTime() public view returns (uint256) {
    return _openingTime;
  }

  /**
   * @return ICO closing time.
   */
  function closingTime() public view returns (uint256) {
    return _closingTime;
  }

  /**
   * @return the amount of token raised.
   */
  function tokenSoldAmount() public view returns (uint256) {
    return _tokenSoldAmount;
  }

  /**
   * @return the number of minimum amount buyer can fund.
   */
  function minWeiAmount() public view returns(uint256) {
    return _minWeiAmount;
  }

  /**
   * @return is ICO period
   */
  function isOpen() public view returns (bool) {
     // solium-disable-next-line security/no-block-members
    return now >= _openingTime && now <= _closingTime;
  }

  /**
  * @dev Gets the token balance of the specified address for deliver
  * @param owner The address to query the balance of.
  * @return An uint256 representing the amount owned by the passed address.
  */
  function tokenBalanceOf(address owner) public view returns (uint256) {
    return _tokenBalances[owner];
  }

  /**
  * @dev Gets the ETH balance of the specified address.
  * @param owner The address to query the balance of.
  * @return An uint256 representing the amount owned by the passed address.
  */
  function weiBalanceOf(address owner) public view returns (uint256) {
    return _weiBalances[owner];
  }

  function setRate(uint256 value) public onlyOwner {
    _rate = value;
    emit RateChanged(value);
  }

  function setMinWeiAmount(uint256 value) public onlyOwner {
    _minWeiAmount = value;
    emit MinWeiChanged(value);
  }

  function setPeriodTimestamp(uint256 open, uint256 close)
    public
    onlyOwner
  {
    _openingTime = open;
    _closingTime = close;
    emit PeriodChanged(open, close);
  }

  function setHardcap(uint256 value) public onlyOwner {
    _hardcap = value;
    emit HardcapChanged(value);
  }

  /**
   * @dev low level token purchase ***DO NOT OVERRIDE***
   * This function has a non-reentrancy guard, so it shouldn't be called by
   * another `nonReentrant` function.
   * @param beneficiary Recipient of the token purchase
   */
  function buyTokens(address beneficiary)
    public
    nonReentrant
    whenNotPaused
    payable
  {
    uint256 weiAmount = msg.value;
    _preValidatePurchase(beneficiary, weiAmount);

    // Calculate token amount to be created
    uint256 tokens = _getTokenAmount(weiAmount);

    require(_hardcap > _tokenSoldAmount.add(tokens), "Over hardcap");

    // Update state
    _weiRaised = _weiRaised.add(weiAmount);
    _tokenSoldAmount = _tokenSoldAmount.add(tokens);

    _weiBalances[beneficiary] = _weiBalances[beneficiary].add(weiAmount);
    _tokenBalances[beneficiary] = _tokenBalances[beneficiary].add(tokens);

    emit TokensPurchased(
      msg.sender,
      beneficiary,
      weiAmount,
      tokens
    );

    _forwardFunds();
  }

  /**
   * @dev method that deliver token to user
   */
  function deliverTokens(address[] users)
    public
    whenNotPaused
    onlyOwner
  {
    uint256 len = users.length;
    for (uint256 i = 0; i < len; i++) {
      address user = users[i];
      uint256 tokenAmount = _tokenBalances[user];
      _deliverTokens(user, tokenAmount);
      _tokenBalances[user] = 0;

      emit TokensDelivered(user, tokenAmount);
    }
  }

  // -----------------------------------------
  // Internal interface (extensible)
  // -----------------------------------------

  /**
   * @dev Validation of an incoming purchase. Use require statements to revert state when conditions are not met. Use `super` in contracts that inherit from Crowdsale to extend their validations.
   * @param beneficiary Address performing the token purchase
   * @param weiAmount Value in wei involved in the purchase
   */
  function _preValidatePurchase(
    address beneficiary,
    uint256 weiAmount
  )
    internal
    view
    onlyAuthorised(beneficiary)
  {
    require(weiAmount != 0, "Zero ETH");
    require(weiAmount >= _minWeiAmount, "Must be equal or higher than minimum");
    require(beneficiary != address(0), "Beneficiary address is zero");
    require(isOpen(), "Sales is close");
  }

  /**
   * @dev Source of tokens. Override this method to modify the way in which the crowdsale ultimately gets and sends its tokens.
   * @param beneficiary Address performing the token purchase
   * @param tokenAmount Number of tokens to be emitted
   */
  function _deliverTokens(
    address beneficiary,
    uint256 tokenAmount
  )
    internal
  {
    _token.safeTransferFrom(_tokenholder, beneficiary, tokenAmount);
  }

  /**
   * @dev Override to extend the way in which ether is converted to tokens.
   * @param weiAmount Value in wei to be converted into tokens
   * @return Number of tokens that can be purchased with the specified _weiAmount
   */
  function _getTokenAmount(uint256 weiAmount) internal view returns (uint256)
  {
    uint ethDecimals = 18;
    require(DECIMALS <= ethDecimals, "");

    uint256 covertedTokens = weiAmount;
    if (DECIMALS != ethDecimals) {
      covertedTokens = weiAmount.div((10 ** uint256(ethDecimals - DECIMALS)));
    }
    return covertedTokens.mul(_rate);
  }

  /**
    * @dev Determines how ETH is stored/forwarded on purchases.
    */
  function _forwardFunds() internal {
    _wallet.transfer(msg.value);
  }
}
/**
 * @title Crowdsale
 * @dev Crowdsale is a base contract for managing a token crowdsale,
 * allowing investors to purchase tokens with ether. This contract implements
 * such functionality in its most fundamental form and can be extended to provide additional
 * functionality and/or custom behavior.
 * The external interface represents the basic interface for purchasing tokens, and conform
 * the base architecture for crowdsales. They are *not* intended to be modified / overridden.
 * The internal interface conforms the extensible and modifiable surface of crowdsales. Override
 * the methods to add functionality. Consider using 'super' where appropriate to concatenate
 * behavior.
 */
function tokenSoldAmount() public view returns (uint256) {
    return _tokenSoldAmount;
  }
/**
 * @return the amount of token raised.
 */