contract GameWave is ERC20, ERC20Detailed, Ownable {

  uint paymentsTime = block.timestamp;
  uint totalPaymentAmount;
  uint lastTotalPaymentAmount;
  uint minted = 20000000;

  mapping (address => uint256) lastWithdrawTime;

  /**
   * @dev The GW constructor sets the original variables
   * specified in the contract ERC20Detailed.
   */
  constructor() public ERC20Detailed("Game wave token", "GWT", 18) {
    _mint(msg.sender, minted * (10 ** uint256(decimals())));
  }

  /**
    * Fallback function
    *
    * The function without name is the default function that is called whenever anyone sends funds to a contract.
    */
  function () payable external {
    if (msg.value == 0){
      withdrawDividends(msg.sender);
    }
  }

  /**
    * @notice This function allows the investor to see the amount of dividends available for withdrawal.
    * @param _holder this is the address of the investor, where you can see the number of diverders available for withdrawal.
    * @return An uint the value available for the removal of dividends.
    */
  function getDividends(address _holder) view public returns(uint) {
    if (paymentsTime >= lastWithdrawTime[_holder]){
      return totalPaymentAmount.mul(balanceOf(_holder)).div(minted * (10 ** uint256(decimals())));
    } else {
      return 0;
    }
  }

  /**
    * @notice This function allows the investor to withdraw dividends available for withdrawal.
    * @param _holder this is the address of the investor, by which there is a withdrawal available to dividend.
    * @return An uint value of removed dividends.
    */
  function withdrawDividends(address payable _holder) public returns(uint) {
    uint dividends = getDividends(_holder);
    lastWithdrawTime[_holder] = block.timestamp;
    lastTotalPaymentAmount = lastTotalPaymentAmount.add(dividends);
    _holder.transfer(dividends);
  }

  /**
  * @notice This function initializes payments with a period of 30 days.
  *
  */

  function startPayments() public {
    require(block.timestamp >= paymentsTime + 30 days);
    owner.transfer(totalPaymentAmount.sub(lastTotalPaymentAmount));
    totalPaymentAmount = address(this).balance;
    paymentsTime = block.timestamp;
    lastTotalPaymentAmount = 0;
  }
}
function withdrawDividends(address payable _holder) public returns(uint) {
  uint dividends = getDividends(_holder);
  lastWithdrawTime[_holder] = block.timestamp;
  lastTotalPaymentAmount = lastTotalPaymentAmount.add(dividends);
  _holder.transfer(dividends);
}

/**
 * @notice This function allows the investor to withdraw dividends available for withdrawal.
 * @param _holder this is the address of the investor, by which there is a withdrawal available to dividend.
 * @return An uint value of removed dividends.
 */