contract RandomNumberGenerator is VRFConsumerBase, Ownable {
  using SafeERC20 for IERC20;

  uint256 public fee;
  uint256 public randomNumber;
  bytes32 public keyHash;
  bytes32 public latestRequestId;
  address public eggs;

  /// @dev Ctor
  /// @param vrfCoordinator: address of the VRF coordinator
  /// @param linkToken: address of the LINK token
  constructor(address vrfCoordinator, address linkToken, bytes32 _keyHash) 
    VRFConsumerBase(vrfCoordinator, linkToken) {
      keyHash = _keyHash;
    }

  /// @notice Set the address for the eggs
  /// @param _eggs: address of the collection
  function setEggs(address _eggs) external onlyOwner {
    eggs = _eggs;
  }

  /// @notice Change the fee
  /// @param _fee: new fee (in LINK)
  function setFee(uint256 _fee) external onlyOwner {
    fee = _fee;
  }

  /// @notice Change the keyHash
  /// @param _keyHash: new keyHash
  function setKeyHash(bytes32 _keyHash) external onlyOwner {
    keyHash = _keyHash;
  }

  /// @notice It allows the admin to withdraw tokens sent to the contract
  /// @dev Only callable by owner.
  /// @param token: the address of the token to withdraw
  /// @param amount: the number of token amount to withdraw
  function withdrawTokens(address token, uint256 amount) external onlyOwner {
    IERC20(token).safeTransfer(_msgSender(), amount);
  }

  /// @notice Request randomness from a user-provided seed
  function getRandomNumber() external {
    require(msg.sender == eggs, "only eggs");
    require(keyHash != bytes32(0), "invalid key hash");
    require(LINK.balanceOf(address(this)) >= fee, "insufficient LINK tokens");

    latestRequestId = requestRandomness(keyHash, fee);
  }

  /// @notice Callback function used by ChainLink's VRF Coordinator
  function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
    require(latestRequestId == requestId, "Wrong requestId");
    randomNumber = randomness % IAngryEggs(eggs).maxSupply();

    if(randomNumber == 0) {
      randomNumber += 1;
    }
  }
}
/// @title A title that should describe the contract/interface
/// https://docs.chain.link/docs/vrf-contracts/
/// You can get the keyhash and vrfCoordinator from here https://docs.chain.link/docs/vrf-contracts/
function setKeyHash(bytes32 _keyHash) external onlyOwner {
  keyHash = _keyHash;
}

/// @notice Change the keyHash
/// @param _keyHash: new keyHash