contract ReferralQueue {
    
    // ID in circle
    uint64 currentReceiverId = 1;

    // Circle length
    uint64 public circleLength;
    
    // Store queue of referral circle
    struct ReferralSeat {
        uint64 petId;
        uint64 givenPetId;
    }
    
    mapping (uint64 => ReferralSeat) public referralCircle;
    
    // Store simple information about each pet: parent parrot and current referral reward
    struct PetInfo {
        uint64 parentId;
        uint256 amount;
    }
    
    mapping (uint64 => PetInfo) public petsInfo;

    
    function addPetIntoCircle(uint64 _id, uint8 _seats) internal {
        
        // Adding seats into queue
        for(uint8 i=0; i < _seats; i++)
		{
		    ReferralSeat memory _seat = ReferralSeat({
                petId: _id,
                givenPetId: 0
            });

            // Increasing circle length and save current seat in circle            
            circleLength++;
            referralCircle[circleLength] = _seat;
		}
		
		// Attach the parrot to the current receiver in the circle 
		// First 3 parrots adding without attaching
		if(_id>103) {
		    
		    referralCircle[currentReceiverId].givenPetId = _id;
		    
		    // adding new pet into information list
		    PetInfo memory petInfo = PetInfo({
		        parentId: referralCircle[currentReceiverId].petId,
		        amount: 0
		    });
		    
		    petsInfo[_id] = petInfo;
		    
		    // Increace circle receiver ID
            currentReceiverId++;
        }
    }
    
    // Current pet ID in circle for automatical attach
    function getCurrentReceiverId() view public returns(uint64 receiverId) {
        
        return referralCircle[currentReceiverId].petId;
    }
}
// Main contract, which calculating queue
function getCurrentReceiverId() view public returns(uint64 receiverId) {
        
    return referralCircle[currentReceiverId].petId;
}

// Current pet ID in circle for automatical attach