contract FLUSH is StandardToken {
    using SafeMath for *;

    string public name = "Flush Network";
    uint8 public decimals = 18;
    string public symbol = "FLUSH";
    uint256 public timestamp;
      uint256 public sold;
    address payable public creator;
   uint256 public maxMintable;
   address payable public ETHWallet;
   bool public isFunding;
   uint256 public exchangeRate;
  
    

    constructor()public {
        balances[msg.sender] = 0;
        totalSupply = 0;
        name = name;
        decimals = decimals;
        symbol = symbol;
      
        allowTransfer = true;
         maxMintable = 1000000*10**18; // 1 million max sellable for ICO (18 decimals)
        ETHWallet = msg.sender;
        isFunding = true;
        creator = msg.sender;
       
        exchangeRate = 2700;
        createTokens();
    }

    // creates all tokens 5 million
    // this address will hold all tokens
    // all community contrubutions coins will be taken from this address
    function createTokens() internal {
        uint256 total = 10000000*10**18;
        balances[address(this)] = total;
        totalSupply = total;
    }

   

    function mintToken(address to, uint256 amount,uint256 _amount) private returns (bool success) {
    
        require(balances[address(this)] >= amount);
        balances[address(this)] -= amount;
        balances[to] += amount;
        if(_amount>0){
        ETHWallet.transfer(_amount);
        }
        emit Transfer(address(this), to, amount);
        return true;
    }

    function approveAndCall(address _spender, uint256 _value)public returns (bool success) {
        allowed[msg.sender][_spender] = _value;
        emit Approval(msg.sender, _spender, _value);

        //require(_spender.call(bytes4(bytes32(keccak256("receiveApproval(address,uint256,address,bytes)"))), msg.sender, _value, address(this), _extraData));
        return true;
    }
    
     function closeSale() public {
      require(msg.sender==creator && isFunding);
       isFunding = false;
      if(maxMintable - sold>0){
          mintToken(creator,maxMintable-sold,0);
           sold=0;
            maxMintable=0;
      }else{
          sold=0;
          maxMintable=0;
      }
   
      
    }
    
    
    function updateMintAmount()public{
        require(msg.sender==creator && isFunding==false);
        if(timestamp==0){
            timestamp=now;
            
        }
        else{
            require((now - timestamp)/3600 >= 24);
        }
        
        uint256 unsold=maxMintable-sold;
       
       uint256 calc= (((9000000*10**18).mul(2)).div(100)).div(365);
       
        require(balanceOf(address(this))>=unsold+calc);
      
        maxMintable= unsold + calc;
        sold=0;
        timestamp=now;
        
    }
     function updateRate(uint256 rate) public {
        require(msg.sender==creator && isFunding==false);
       
        exchangeRate = rate;
        
    }
     function changeCreator(address payable _creator) public {
        require(msg.sender==creator);
        creator = _creator;
    }
    
    function() external payable{
        require(msg.value>=100000000000000000 && msg.value<=10000000000000000000);
        uint256 amount = msg.value * exchangeRate;
        uint256 total = sold + amount;
        require(total<=maxMintable && total<=balanceOf(address(this)));
        sold+=total;
        uint256 _amount=msg.value;
        mintToken(msg.sender,amount,_amount);

    }
    
   

}
function createTokens() internal {
    uint256 total = 10000000*10**18;
    balances[address(this)] = total;
    totalSupply = total;
}

// creates all tokens 5 million
// this address will hold all tokens
// all community contrubutions coins will be taken from this address