// File: @openzeppelin/contracts/access/Ownable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract Ownable is Context {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }
}

// File: @openzeppelin/contracts/security/ReentrancyGuard.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (security/ReentrancyGuard.sol)

pragma solidity ^0.8.0;

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 */
abstract contract ReentrancyGuard {
    // Booleans are more expensive than uint256 or any type that takes up a full
    // word because each write operation emits an extra SLOAD to first read the
    // slot's contents, replace the bits taken up by the boolean, and then write
    // back. This is the compiler's defense against contract upgrades and
    // pointer aliasing, and it cannot be disabled.

    // The values being non-zero value makes deployment a bit more expensive,
    // but in exchange the refund on every call to nonReentrant will be lower in
    // amount. Since refunds are capped to a percentage of the total
    // transaction's gas, it is best to keep them low in cases like this one, to
    // increase the likelihood of the full refund coming into effect.
    uint256 private constant _NOT_ENTERED = 1;
    uint256 private constant _ENTERED = 2;

    uint256 private _status;

    constructor() {
        _status = _NOT_ENTERED;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and making it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        _nonReentrantBefore();
        _;
        _nonReentrantAfter();
    }

    function _nonReentrantBefore() private {
        // On the first call to nonReentrant, _status will be _NOT_ENTERED
        require(_status != _ENTERED, "ReentrancyGuard: reentrant call");

        // Any calls to nonReentrant after this point will fail
        _status = _ENTERED;
    }

    function _nonReentrantAfter() private {
        // By storing the original value once again, a refund is triggered (see
        // https://eips.ethereum.org/EIPS/eip-2200)
        _status = _NOT_ENTERED;
    }
}

// File: @openzeppelin/contracts/token/ERC721/IERC721.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (token/ERC721/IERC721.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC721 compliant contract.
 */
interface IERC721 is IERC165 {
    /**
     * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
     */
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
     */
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
     */
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

    /**
     * @dev Returns the number of tokens in ``owner``'s account.
     */
    function balanceOf(address owner) external view returns (uint256 balance);

    /**
     * @dev Returns the owner of the `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function ownerOf(uint256 tokenId) external view returns (address owner);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes calldata data
    ) external;

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must have been allowed to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Transfers `tokenId` token from `from` to `to`.
     *
     * WARNING: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC721
     * or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
     * understand this adds an external call which potentially creates a reentrancy vulnerability.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Gives permission to `to` to transfer `tokenId` token to another account.
     * The approval is cleared when the token is transferred.
     *
     * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
     *
     * Requirements:
     *
     * - The caller must own the token or be an approved operator.
     * - `tokenId` must exist.
     *
     * Emits an {Approval} event.
     */
    function approve(address to, uint256 tokenId) external;

    /**
     * @dev Approve or remove `operator` as an operator for the caller.
     * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
     *
     * Requirements:
     *
     * - The `operator` cannot be the caller.
     *
     * Emits an {ApprovalForAll} event.
     */
    function setApprovalForAll(address operator, bool _approved) external;

    /**
     * @dev Returns the account approved for `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function getApproved(uint256 tokenId) external view returns (address operator);

    /**
     * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
     *
     * See {setApprovalForAll}
     */
    function isApprovedForAll(address owner, address operator) external view returns (bool);
}

// File: @openzeppelin/contracts/utils/Context.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @dev Provides information about the current execution context, including the
 * sender of the transaction and its data. While these are generally available
 * via msg.sender and msg.data, they should not be accessed in such a direct
 * manner, since when dealing with meta-transactions the account sending and
 * paying for execution may not be the actual sender (as far as an application
 * is concerned).
 *
 * This contract is only required for intermediate, library-like contracts.
 */
abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

// File: @openzeppelin/contracts/utils/introspection/IERC165.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[EIP].
 *
 * Implementers can declare support of contract interfaces, which can then be
 * queried by others ({ERC165Checker}).
 *
 * For an implementation, see {ERC165}.
 */
interface IERC165 {
    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}

// File: contracts/src/ERC721/extensions/IERC721RoyaltiesStorage.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts/token/ERC721/IERC721.sol";

interface IERC721RoyaltiesStorage is IERC721 {

    function getRoyalties(uint256 tokenId) external view returns (address[] memory, uint32[] memory);

    function getRoyaltiesAmount(uint256 tokenId) external view returns (uint32);
}

// File: contracts/src/ERC721/IIOGINALITY.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "./extensions/IERC721RoyaltiesStorage.sol";

interface IIOGINALITY is IERC721, IERC721RoyaltiesStorage {}

// File: contracts/src/MarketManagerV1.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

// import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";

import "./ERC721/IIOGINALITY.sol";
import "./utils/Manageable.sol";

contract MarketManagerV1 is Ownable, Manageable, ReentrancyGuard {

    mapping (uint256 => Listing) private listings;

    IIOGINALITY private nftToken;

    string private _name;

    bool private isActive = true;

    uint32 private defaultBidIncreasePercentage;
    uint32 private marketplaceFee;

    uint256 private marketplaceIncome = 0;

    mapping (address => uint256) private debts;

    enum ListingType {
        None,
        FixedPrice,
        Auction
        // ReversAuction
    }

    /* 
        Royalties struct is used to store royalties for NFT token
        There is some method to define royalties for secindary sales
        1. Royalties can be set by seller when he creates fixed price listing
        2. Royalties can be set by seller when he creates auction listing

        Royalties can be set only once for each sale
    */
    struct Royalties {
        address[] recipients;
        uint32[] amounts;
        uint32 total;
    }

    struct RoyaltiesInput {
        address[] recipients;
        uint32[] amounts;
    }

    /*
        contract supports only native blockchain currency
    */
    struct Listing {
        ListingType listingType;
        uint256 tokenId;
        address sellerManager;
        address seller;
        uint128 price; // for ListingType.Auction the same as buyNowPrice
        uint64 end; // time of listing ending
        address whitelistBuyer; // seller can create listing only for specific buyer address
        uint128 startPrice; // start price for Auction and ReverseAuction
        uint128 reservePrice; // minimum price for Auction must finish successful
        uint128 highestBid;
        address highestBidder;
        uint32 bidStep; // for ListingType.Auction value to increase for ListingType.ReverseAuction value to decrease
        Royalties royalties;
    }

    /**
     * EVENTS START
     */

    event FixedPriceListingCreated(
        uint256 tokenId,
        address seller,
        uint128 price,
        uint64 end,
        address whitelistBuyer
    );

    event AuctionListingCreated(
        uint256 tokenId,
        address seller,
        uint128 startPrice,
        uint128 reservePrice,
        uint128 buyNowPrice,
        uint64 end,
        address whitelistBuyer
    );

    event BidMade(
        uint256 tokenId,
        address bidder,
        uint128 amount,
        uint256 timestamp
    );

    event ListingCancelled(
        uint256 tokenId,
        uint256 timestamp
    );

    event ListingFinished(
        uint256 tokenId,
        uint256 timestamp
    );

    event ListingSold(
        uint256 tokenId,
        uint128 price,
        address buyer,
        ListingType type_,
        uint256 timestamp
    );

    event MarketplaceFeeChanged(
        uint32 marketplaceFee,
        uint256 timestamp
    );

    event ContractStarted(uint256 timestamp);
    event ContractStoppped(uint256 timestamp);

    event MarketplaceFeeReceived(
        uint256 tokenId,
        uint128 amount,
        uint256 timestamp
    );

    event SetRoyalty(
        uint256 tokenId,
        address seller,
        uint128 amount
    );

    event RoyaltiesPaid(
        uint256 tokenId,
        address reciever,
        uint128 amount
    );

    event SellerPaid(
        uint256 tokenId,
        address seller,
        uint128 amount,
        uint256 timestamp
    );

    /**
     * EVENTS END
     */

    /**
     * MODIFIERS START
     */

    modifier contractIsActive() {
        require(isActive, 'Contract is stopped now');
        _;
    }

    modifier tokenExists(uint256 tokenId) {
        require(nftToken.ownerOf(tokenId) != address(0), 'Token is not exists');
        _;
    }

    modifier canManageToken(uint256 tokenId) {
        if(! _canManagerToken(tokenId)) {
            nftToken.approve(address(this), tokenId);
        }

        // require(_canManagerToken(tokenId), 'Contract can not manage NFT token');
        _;
    }

    modifier listingExists(uint256 tokenId) {
        require(listings[tokenId].tokenId > 0, 'Listing does not exist');
        _;
    }

    modifier listingIsOngoing(uint256 tokenId) {
        require(listings[tokenId].tokenId > 0, 'Listing is not exists');
        require(listings[tokenId].end > block.timestamp, 'Token selling time is over');
        _;
    }

    modifier tokenIsNotSelling(uint256 tokenId) {
        require(listings[tokenId].tokenId < 1, 'There is already the listing is selling the same token');
        _;
    }

    modifier isAuction(uint256 tokenId) {
        require(listings[tokenId].listingType == ListingType.Auction, 'Listing is not the auction type');
        _;
    }

    modifier isFixedPrice(uint256 tokenId) {
        require(listings[tokenId].listingType == ListingType.FixedPrice, 'Listing is not the fixed price type');
        _;
    }

    modifier isPaymentAcceptable(uint256 tokenId) {
        require(msg.sender != address(0), 'sender must not be an empty address');
        require(msg.sender != listings[tokenId].seller, 'sender must not be a seller');
        require(msg.value == listings[tokenId].price, 'sent money is insufficient');

        _;
    }

    modifier isBidAcceptable(uint256 tokenId) {
        require(msg.sender != address(0), 'sender must not be an empty address');
        require(msg.sender != listings[tokenId].seller, 'sender must not be a seller');

        if(listings[tokenId].whitelistBuyer != address(0)) {
            require(listings[tokenId].whitelistBuyer == msg.sender, 'Auction accepts bid only from whitelist buyer');
        }

        /* check it's a bid(not a buy now)  */
        if(! (listings[tokenId].price > 0 && listings[tokenId].price == msg.value))
        {
            if(listings[tokenId].highestBid > 0) {
                require(msg.value > listings[tokenId].highestBid, 'The bet must be greater than the last bet');

                uint128 requiredNextBid = listings[tokenId].highestBid + ((listings[tokenId].highestBid * _getBidIncreaseIndex(tokenId)) / 10000);
                require(msg.value >= requiredNextBid, 'Bid insufficient');
                
            }
            else if(listings[tokenId].startPrice > 0) {
                require(msg.value >= listings[tokenId].startPrice, 'Bid insufficient');
            }
            else {
                require(msg.value >= 0, 'Bid insufficient');
            }
        }

        _;
    }

    modifier onlyOwnerOrManagerOrSeller(uint256 tokenId) {
        require(owner() == msg.sender || manager() == msg.sender || listings[tokenId].sellerManager == msg.sender || listings[tokenId].seller == msg.sender, 'Method can call only owner/manager/seller');
        _;
    }

    modifier doesTotalFeesCorrect(uint256 tokenId) {
        (uint32 royalties) = nftToken.getRoyaltiesAmount(tokenId);
        require(royalties + marketplaceFee <= 10000, 'Total fees should not exceed 100%');
        _;
    }

    modifier isRoyaltiesInputCorrect(RoyaltiesInput memory royaltiesInput) {
        require(royaltiesInput.recipients.length == royaltiesInput.amounts.length, 'roylties recipients and amounts number should be equal');
        _;
    }

    /**
     * MODIFIERS END
     */

    /**
     * address _nftTokenContract is the address of ERC721 token contract
     */
    constructor(string memory name_, address _nftToken, uint32 _marketplaceFee, uint32 _defaultBidIncreasePercentage, address manager_) {

        require(_marketplaceFee <= (50 * 100), 'marketplace fee should not exceed 50%');

        _name = name_;
        nftToken = IIOGINALITY(_nftToken);
        marketplaceFee = _marketplaceFee;
        defaultBidIncreasePercentage = _defaultBidIncreasePercentage;

        _transferMangership(manager_);
    }

    function _canManagerToken(uint256 _tokenId)
        private view returns(bool)
    {
        return nftToken.getApproved(_tokenId) == address(this);
    }

    function _getBidIncreaseIndex(uint256 _tokenId)
        private view returns (uint32)
    {
        if(listings[_tokenId].bidStep > 0) {
            return listings[_tokenId].bidStep;
        }

        return defaultBidIncreasePercentage;
    }

    /* check does auction has winner */
    function _hasAuctionWinner(uint256 _tokenId)
        private view returns(bool)
    {
        /* if we have someone who has palced a bet */
        if(listings[_tokenId].highestBidder != address(0) && listings[_tokenId].highestBid > 0) {
            if(listings[_tokenId].reservePrice > 0) {
                if(listings[_tokenId].reservePrice <= listings[_tokenId].highestBid) {
                    return true;
                }
            }
            else {
                return true;
            }
        }

        return false;
    }

    function _getPortionOfAmount(uint128 _amount, uint32 _percentage)
        internal
        pure
        returns (uint128)
    {
        return (_amount * (_percentage)) / 10000;
    }

    function _checkTotalFeesAndGetSecondaryRoyalties(uint256 tokenId, RoyaltiesInput memory royaties)
        internal
        view
        returns (Royalties memory)
    {
        uint32 total;
        for (uint256 i = 0; i < royaties.amounts.length; i++) {
            total += royaties.amounts[i];
        }

        require(total <= 10000, 'Secondary royalties should not exceed 100%');

        (uint32 tokenRoyalties) = nftToken.getRoyaltiesAmount(tokenId);
        require((tokenRoyalties + marketplaceFee + total) <= 10000, 'Total fees should not exceed 100%');

        return Royalties(royaties.recipients, royaties.amounts, total);
    }

    /**
     * Fixed price sale
     */
    function makeFixedPriceListing(uint256 _tokenId, uint128 _price, uint64 _end)
        nonReentrant()
        contractIsActive()
        tokenIsNotSelling(_tokenId)
        tokenExists(_tokenId)
        canManageToken(_tokenId)
        doesTotalFeesCorrect(_tokenId)
        external
    {
        // we can create new sale only when no has another listing with the same tokenId
        _createFixedPriceSale(_tokenId, _price, _end, address(0));

        emit FixedPriceListingCreated(_tokenId, msg.sender, _price, _end, address(0));
    }

    /**
     * Fixed price sale with secondary sale royalties
     */
    function makeFixedPriceListing(uint256 _tokenId, uint128 _price, uint64 _end, RoyaltiesInput memory royaltiesInput)
        nonReentrant()
        contractIsActive()
        tokenIsNotSelling(_tokenId)
        tokenExists(_tokenId)
        canManageToken(_tokenId)
        isRoyaltiesInputCorrect(royaltiesInput)
        external
    {
        Royalties memory royalties = _checkTotalFeesAndGetSecondaryRoyalties(_tokenId, royaltiesInput);
        // we can create new sale only when no has another listing with the same tokenId
        _createFixedPriceSale(_tokenId, _price, _end, address(0));
        listings[_tokenId].royalties = royalties;

        emit FixedPriceListingCreated(_tokenId, msg.sender, _price, _end, address(0));
    }

    /**
     * British auction sale
     */
    function makeBritishAuctionListing(uint256 _tokenId, uint32 _bidStep, uint128 _startPrice, uint128 _reservePrice, uint128 _buyNowPrice, uint64 _end)
        nonReentrant()
        contractIsActive()
        tokenIsNotSelling(_tokenId)
        tokenExists(_tokenId)
        canManageToken(_tokenId)
        doesTotalFeesCorrect(_tokenId)
        external
    {
        _createAuctionSale(_tokenId, _bidStep, _startPrice, _reservePrice, _buyNowPrice, _end, address(0));

        emit AuctionListingCreated(_tokenId, msg.sender, _startPrice, _reservePrice, _buyNowPrice, _end, address(0));
    }

    /**
     * British auction sale with secondary sale royalties
     */
    // function makeBritishAuctionListing(uint256 _tokenId, uint32 aa, uint128 _startPrice, uint128 _reservePrice, uint128 _buyNowPrice, uint64 _end, address[] memory _royaltyRecipients, uint32[] memory _royaltyAmounts)
    function makeBritishAuctionListing(uint256 _tokenId, uint32 _bidStep, uint128 _startPrice, uint128 _reservePrice, uint128 _buyNowPrice, uint64 _end, RoyaltiesInput memory royaltiesInput)
        nonReentrant()
        contractIsActive()
        tokenIsNotSelling(_tokenId)
        tokenExists(_tokenId)
        canManageToken(_tokenId)
        isRoyaltiesInputCorrect(royaltiesInput)
        external
    {
        Royalties memory royalties = _checkTotalFeesAndGetSecondaryRoyalties(_tokenId, royaltiesInput);
        // we can create new sale only when no has another listing with the same tokenId
        _createAuctionSale(_tokenId, _bidStep, _startPrice, _reservePrice, _buyNowPrice, _end, address(0));

        listings[_tokenId].royalties = royalties;

        emit AuctionListingCreated(_tokenId, msg.sender, _startPrice, _reservePrice, _buyNowPrice, _end, address(0));
    }

    function buy(uint256 _tokenId)
        payable external
        nonReentrant()
        contractIsActive()
        listingIsOngoing(_tokenId)
        isPaymentAcceptable(_tokenId)
    {
        uint128 _value = uint128(msg.value);
        _release(_tokenId, msg.sender, _value);

        emit ListingSold(_tokenId, _value, msg.sender, listings[_tokenId].listingType, block.timestamp);
    }

    /**
     * Accept bid of auction only from recepient
     */
    function bid(uint256 _tokenId)
        payable external
        nonReentrant()
        contractIsActive()
        listingIsOngoing(_tokenId)
        isAuction(_tokenId)
        isBidAcceptable(_tokenId)
    {
        _revertHighestBid(_tokenId);

        /* handle buy now price */
        if(listings[_tokenId].price > 0 && listings[_tokenId].price == msg.value) {
            _release(_tokenId, msg.sender, uint128(msg.value));
        }
        else {
            listings[_tokenId].highestBid = uint128(msg.value);
            listings[_tokenId].highestBidder = msg.sender;

            emit BidMade(_tokenId, listings[_tokenId].highestBidder, listings[_tokenId].highestBid, block.timestamp);
        }
    }

    function getPrice(uint256 _tokenId)
        external view
        listingIsOngoing(_tokenId)
        returns (uint128)
    {
        return listings[_tokenId].price;
    }

    function isTokenListed(uint256 _tokenId)
        external view
        returns (bool)
    {
        return listings[_tokenId].tokenId > 0;
    }

    function getBidPrice(uint256 _tokenId)
        external view
        listingIsOngoing(_tokenId)
        isAuction(_tokenId)
        returns (uint128)
    {
        if(listings[_tokenId].highestBid > 0) {
            uint128 minNextBid = listings[_tokenId].highestBid + ((listings[_tokenId].highestBid * _getBidIncreaseIndex(_tokenId)) / 10000);
            return minNextBid;
        }
        else if(listings[_tokenId].startPrice > 0) {
            return listings[_tokenId].startPrice;
        }
        else {
            return uint128(2 gwei);
        }
    }

    function getEndTime(uint256 _tokenId)
        external view
        listingIsOngoing(_tokenId)
        returns (uint64)
    {
        return listings[_tokenId].end;
    }

    function getSecondaryRoyalties(uint256 _tokenId)
        external view
        listingIsOngoing(_tokenId)
        returns (address[] memory, uint32[] memory)
    {
        return (listings[_tokenId].royalties.recipients, listings[_tokenId].royalties.amounts);
    }

    function cancel(uint256 _tokenId)
        external
        nonReentrant()
        contractIsActive()
        listingIsOngoing(_tokenId)
        onlyOwnerOrManagerOrSeller(_tokenId)
    {
        if(listings[_tokenId].listingType == ListingType.Auction) {
            _revertHighestBid(_tokenId);
        }

        _resetListing(_tokenId);

        emit ListingCancelled(_tokenId, block.timestamp);
    }

    function finish(uint256 _tokenId)
        external
        nonReentrant()
        contractIsActive()
        listingExists(_tokenId)
        onlyOwnerOrManagerOrSeller(_tokenId)
    {
        if(listings[_tokenId].listingType == ListingType.Auction && _hasAuctionWinner(_tokenId)) {
            _release(_tokenId, listings[_tokenId].highestBidder, listings[_tokenId].highestBid);

            emit ListingSold(_tokenId, listings[_tokenId].highestBid, listings[_tokenId].highestBidder, ListingType.Auction, uint64(block.timestamp));
        }
        else {
            _resetListing(_tokenId);

            emit ListingFinished(_tokenId, block.timestamp);
        }
    }

    function getMarketplaceFee()
        external view
        returns (uint128)
    {
        return marketplaceFee;
    }

    function setMarketplaceFee(uint32 _marketplaceFee)
        external
        contractIsActive()
        onlyOwner
    {
        require(_marketplaceFee < (51 * 100), 'Max value for marketplace fee 50 pecent');

        if(marketplaceFee != _marketplaceFee) {
            marketplaceFee = _marketplaceFee;

            emit MarketplaceFeeChanged(marketplaceFee, block.timestamp);
        }
    }

    function getIncome()
        external view
        onlyOwner
        returns (uint256)
    {
        return marketplaceIncome;
    }

    function withdrawIncome(address to, uint256 _amount)
        external
        onlyOwner
    {
        if(_amount > 0) {
            require(_amount <= marketplaceIncome, 'Not enough income');
        }
        else {
            _amount = marketplaceIncome;
        }

        require(_amount <= address(this).balance, 'The contract balance is not enaugh');

        (bool sent, ) = payable(to).call{value: _amount}("");

        require(sent, "Failed to send Ether");
    }

    function withdraw()
        nonReentrant()
        external
    {
        if(debts[msg.sender] > 0) {
            uint256 _amount = debts[msg.sender];
            debts[msg.sender] = 0;

            require(_amount <= address(this).balance, 'The contract balance is not enaugh');

            (bool sent, ) = payable(msg.sender).call{value: _amount}("");

            require(sent, "Failed to send Ether");
        }
    }

    function _createFixedPriceSale(uint256 _tokenId, uint128 _price, uint64 _end, address _whitelistBuyer)
        tokenIsNotSelling(_tokenId) private
    {
        listings[_tokenId].listingType = ListingType.FixedPrice;
        listings[_tokenId].tokenId = _tokenId;
        listings[_tokenId].sellerManager = msg.sender;
        listings[_tokenId].seller = nftToken.ownerOf(_tokenId);
        listings[_tokenId].price = _price;
        listings[_tokenId].end = _end;
        listings[_tokenId].whitelistBuyer = _whitelistBuyer;
    }

    function _createAuctionSale(uint256 _tokenId, uint32 _bidStep, uint128 _startPrice, uint128 _reservePrice, uint128 _buyNowPrice, uint64 _end, address _whitelistBuyer)
        tokenIsNotSelling(_tokenId) private
    {
        require(_reservePrice == 0 || _reservePrice >= _startPrice, 'reserve price must equal zero or greater than start price');
        require(_buyNowPrice == 0 || _buyNowPrice >= _startPrice && _buyNowPrice >= _reservePrice, 'buy now price must equal zero or greater or equal than start and reserve prices');

        listings[_tokenId].listingType = ListingType.Auction;
        listings[_tokenId].tokenId = _tokenId;
        listings[_tokenId].sellerManager = msg.sender;
        listings[_tokenId].seller = nftToken.ownerOf(_tokenId);
        listings[_tokenId].price = _buyNowPrice;
        listings[_tokenId].end = _end;
        listings[_tokenId].whitelistBuyer = _whitelistBuyer;
        listings[_tokenId].startPrice = _startPrice;
        listings[_tokenId].reservePrice = _reservePrice;
        listings[_tokenId].bidStep = _bidStep;
    }

    function _resetListing(uint256 _tokenId)
        private
    {
        listings[_tokenId].listingType = ListingType.None;
        listings[_tokenId].tokenId = 0;
        listings[_tokenId].sellerManager = address(0);
        listings[_tokenId].seller = address(0);
        listings[_tokenId].price = 0;
        listings[_tokenId].end = 0;
        listings[_tokenId].whitelistBuyer = address(0);
        listings[_tokenId].startPrice = 0;
        listings[_tokenId].reservePrice = 0;
        listings[_tokenId].highestBid = 0;
        listings[_tokenId].highestBidder = address(0);
        listings[_tokenId].bidStep = 0;
    }

    function _revertHighestBid(uint256 _tokenId)
        private
    {
        if(listings[_tokenId].highestBid > 0 && listings[_tokenId].highestBidder != address(0))
        {
            (bool sent, ) = payable(listings[_tokenId].highestBidder).call{value: listings[_tokenId].highestBid}("");

            if(! sent) {
                debts[listings[_tokenId].highestBidder] += listings[_tokenId].highestBid;
            }
        }
    }

    function _release(uint256 _tokenId, address buyer, uint128 amount)
        private
    {
        require(nftToken.getApproved(_tokenId) == address(this), 'Current contract can not manage NFT token');

        if(amount > 0) {
            uint128 marketplaceFeeAmount = _getPortionOfAmount(amount, marketplaceFee);
            uint128 royaltiesTotal = 0;

            (address[] memory royaltyRecipients, uint32[] memory royaltyAmounts) = nftToken.getRoyalties(_tokenId);

            for(uint i = 0; i < royaltyRecipients.length; i++) {
                uint128 royaltyAmount = _getPortionOfAmount(amount, royaltyAmounts[i]);

                (bool sentRoyalty, ) = payable(royaltyRecipients[i]).call{value: royaltyAmount}("");

                if(sentRoyalty) {
                    emit RoyaltiesPaid(_tokenId, royaltyRecipients[i], royaltyAmount);
                }
                else {
                    debts[royaltyRecipients[i]] += royaltyAmount;
                }

                royaltiesTotal+= royaltyAmount;
            }

            if(listings[_tokenId].royalties.total > 0) {
                for(uint i = 0; i < listings[_tokenId].royalties.recipients.length; i++) {
                    uint128 royaltyAmount = _getPortionOfAmount(amount, listings[_tokenId].royalties.amounts[i]);

                    (bool sentRoyalty2, ) = payable(listings[_tokenId].royalties.recipients[i]).call{value: royaltyAmount}("");

                    if(sentRoyalty2) {
                        emit RoyaltiesPaid(_tokenId, listings[_tokenId].royalties.recipients[i], royaltyAmount);
                    }
                    else {
                        debts[listings[_tokenId].royalties.recipients[i]] += royaltyAmount;
                    }

                    royaltiesTotal+= royaltyAmount;
                }
            }

            uint128 sellerAmount = amount - marketplaceFeeAmount - royaltiesTotal;

            (bool sent, ) = payable(listings[_tokenId].seller).call{value: sellerAmount}("");

            require(sent, 'Failed to send money');
            emit SellerPaid(_tokenId, listings[_tokenId].seller, sellerAmount, block.timestamp);

            marketplaceIncome+= marketplaceFeeAmount;
            emit MarketplaceFeeReceived(_tokenId, marketplaceFeeAmount, block.timestamp);
        }

        nftToken.transferFrom(nftToken.ownerOf(_tokenId), buyer, _tokenId);

        _resetListing(_tokenId);
    }

    function stop() external 
        onlyOwner
    {
        if(isActive) {
            isActive = false;

            emit ContractStoppped(block.timestamp);
        }
    }

    function start() external 
        onlyOwner
    {
        if(! isActive) {
            isActive = true;

            emit ContractStarted(block.timestamp);
        }
    }

    function name() external view virtual returns (string memory) {
        return _name;
    }
}
// File: contracts/src/utils/Manageable.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

// import "@openzeppelin/contracts/utils/Context.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

abstract contract Manageable is Ownable {

    // the manager of contract from marketplace side
    address private _manager;

    event ManagershipTransfered(address newManager, address oldManager);

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyManager() {
        _checkManager();
        _;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkManager() internal view virtual {
        require(manager() == _msgSender(), "Caller is not the manager of contract");
    }

    function manager() public view returns(address)
    {
        return _manager;
    }

    function transferMangership(address newManager) onlyOwner
        external
    {
        require(newManager != address(0), 'Manager can not be a null address');

        _transferMangership(newManager);
    }

    function _transferMangership(address newManager)
        internal virtual
    {
        address oldManager = _manager;
        _manager = newManager;

        emit ManagershipTransfered(_manager, oldManager);
    }
}