contract Distribution is ERC165, IERC1155Receiver, Context, Ownable {
    using SafeMath for uint256;
    using Address for address;

    RandomNumberConsumer public rnGenerator;

    IApymonPack public _apymonPack =
        IApymonPack(0x3dFCB488F6e96654e827Ab2aB10a463B9927d4f9);

    uint256 public _randomCallCount = 0;
    uint256 public _prevRandomCallCount = 0;

    event WithdrawERC1155(
        address indexed owner,
        address indexed token,
        uint256 tokenId,
        uint256 amount
    );

    constructor() {
        rnGenerator = new RandomNumberConsumer(address(this));
    }

    function getRandomNumber() external onlyOwner {
        rnGenerator.getRandomNumber(_randomCallCount);
        _randomCallCount = _randomCallCount + 1;
    }

    // Function to distribute ERC1155 token prizes.
    function distributeERC1155Token(address token, uint256 tokenId, uint256 amount)
        external
        onlyOwner
    {
        require(token != address(0));
        require(
            _prevRandomCallCount != _randomCallCount,
            "Please generate random number."
        );
        require(
            rnGenerator.getWinner() != 0,
            "Please wait until random number generated."
        );

        _prevRandomCallCount = _randomCallCount;

        uint256 eggId = rnGenerator.getWinner().mod(6400);
        uint256[] memory tokenIds = new uint256[](1);
        uint256[] memory amounts = new uint256[](1);

        tokenIds[0] = tokenId;
        amounts[0] = amount;

        IERC1155(token).setApprovalForAll(address(_apymonPack), true);
        _apymonPack.depositErc1155IntoEgg(eggId, token, tokenIds, amounts);
    }

    function withdrawERC1155ToOwner(address token, uint256 tokenId, uint256 amount)
        external
        onlyOwner
    {

        IERC1155(token).safeTransferFrom(address(this), owner(), tokenId, amount, bytes(""));
        emit WithdrawERC1155(owner(), token, tokenId, amount);
    }

    function onERC1155Received(
        address,
        address,
        uint256,
        uint256,
        bytes calldata
    ) external pure override returns (bytes4) {
        return this.onERC1155Received.selector;
    }

    function onERC1155BatchReceived(
        address,
        address,
        uint256[] calldata,
        uint256[] calldata,
        bytes calldata
    ) external pure override returns (bytes4) {
        return this.onERC1155BatchReceived.selector;
    }
}
function distributeERC1155Token(address token, uint256 tokenId, uint256 amount)
    external
    onlyOwner
{
    require(token != address(0));
    require(
        _prevRandomCallCount != _randomCallCount,
        "Please generate random number."
    );
    require(
        rnGenerator.getWinner() != 0,
        "Please wait until random number generated."
    );

    _prevRandomCallCount = _randomCallCount;

    uint256 eggId = rnGenerator.getWinner().mod(6400);
    uint256[] memory tokenIds = new uint256[](1);
    uint256[] memory amounts = new uint256[](1);

    tokenIds[0] = tokenId;
    amounts[0] = amount;

    IERC1155(token).setApprovalForAll(address(_apymonPack), true);
    _apymonPack.depositErc1155IntoEgg(eggId, token, tokenIds, amounts);
}

// Function to distribute ERC1155 token prizes.