// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

interface IERC721 {
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
    function balanceOf(address owner) external view returns (uint256 balance);
    function ownerOf(uint256 tokenId) external view returns (address owner);
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;
    function approve(address to, uint256 tokenId) external;
    function getApproved(uint256 tokenId) external view returns (address operator);
    function setApprovalForAll(address operator, bool _approved) external;
    function isApprovedForAll(address owner, address operator) external view returns (bool);
}

abstract contract Context {
    uint private unlocked = 1;
    modifier lock() virtual {
        require(unlocked == 1, 'DOGERC721: LOCKED');
        unlocked = 0;
        _;
        unlocked = 1;
    }
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

contract ERC721 is Context, IERC721 {
    // Mapping from token ID to owner address
    mapping(uint256 => address) private _owners;

    // Mapping owner address to token count
    mapping(address => uint256) private _balances;

    // Mapping from token ID to approved address
    mapping(uint256 => address) private _tokenApprovals;

    // Mapping from owner to operator approvals
    mapping(address => mapping(address => bool)) private _operatorApprovals;

    function balanceOf(address owner) public view virtual override returns (uint256) {
        require(owner != address(0), "ERC721: balance query for the zero address");
        return _balances[owner];
    }

    function ownerOf(uint256 tokenId) public view virtual override returns (address) {
        address owner = _owners[tokenId];
        require(owner != address(0), "ERC721: owner query for nonexistent token");
        return owner;
    }

    function approve(address to, uint256 tokenId) public virtual lock override {
        address owner = _owners[tokenId];
        require(to != owner, "ERC721: approval to current owner");

        require(
            _msgSender() == owner || isApprovedForAll(owner, _msgSender()),
            "ERC721: approve caller is not owner nor approved for all"
        );

        _approve(to, tokenId);
    }

    function getApproved(uint256 tokenId) public view virtual override returns (address) {
        require(_exists(tokenId), "ERC721: approved query for nonexistent token");

        return _tokenApprovals[tokenId];
    }

    function setApprovalForAll(address operator, bool approved) public virtual lock override {
        require(operator != _msgSender(), "ERC721: approve to caller");

        _operatorApprovals[_msgSender()][operator] = approved;
        emit ApprovalForAll(_msgSender(), operator, approved);
    }

    function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
        return _operatorApprovals[owner][operator];
    }

    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual lock override {
        //solhint-disable-next-line max-line-length
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");

        _transfer(from, to, tokenId);
    }

    function _exists(uint256 tokenId) internal view virtual returns (bool) {
        return _owners[tokenId] != address(0);
    }

    function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
        require(_exists(tokenId), "ERC721: operator query for nonexistent token");
        address owner = ERC721.ownerOf(tokenId);
        return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
    }

    function _mint(address to, uint256 tokenId) internal virtual {
        require(to != address(0), "ERC721: mint to the zero address");
        require(!_exists(tokenId), "ERC721: token already minted");

        _beforeTokenTransfer(address(0), to, tokenId);

        _balances[to] += 1;
        _owners[tokenId] = to;

        emit Transfer(address(0), to, tokenId);
    }

    function _burn(uint256 tokenId) internal virtual {
        address owner = ERC721.ownerOf(tokenId);

        _beforeTokenTransfer(owner, address(0), tokenId);

        // Clear approvals
        _approve(address(0), tokenId);

        _balances[owner] -= 1;
        delete _owners[tokenId];

        emit Transfer(owner, address(0), tokenId);
    }

    function _transfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual {
        require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer of token that is not own");
        require(to != address(0), "ERC721: transfer to the zero address");

        _beforeTokenTransfer(from, to, tokenId);

        // Clear approvals from the previous owner
        _approve(address(0), tokenId);

        _balances[from] -= 1;
        _balances[to] += 1;
        _owners[tokenId] = to;

        emit Transfer(from, to, tokenId);
    }

    function _approve(address to, uint256 tokenId) internal virtual {
        _tokenApprovals[tokenId] = to;
        emit Approval(ERC721.ownerOf(tokenId), to, tokenId);
    }

    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual {}
}

interface IERC721Enumerable is IERC721 {
    function totalSupply() external view returns (uint256);

    function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256 tokenId);

    function tokenByIndex(uint256 index) external view returns (uint256);
}

abstract contract ERC721Enumerable is ERC721, IERC721Enumerable {
    // Mapping from owner to list of owned token IDs
    mapping(address => mapping(uint256 => uint256)) private _ownedTokens;

    // Mapping from token ID to index of the owner tokens list
    mapping(uint256 => uint256) private _ownedTokensIndex;

    // Array with all token ids, used for enumeration
    uint256[] private _allTokens;

    // Mapping from token id to position in the allTokens array
    mapping(uint256 => uint256) private _allTokensIndex;

    function tokenOfOwnerByIndex(address owner, uint256 index) public view virtual override returns (uint256) {
        require(index < ERC721.balanceOf(owner), "ERC721Enumerable: owner index out of bounds");
        return _ownedTokens[owner][index];
    }

    function totalSupply() public view virtual override returns (uint256) {
        return _allTokens.length;
    }

    function tokenByIndex(uint256 index) public view virtual override returns (uint256) {
        require(index < ERC721Enumerable.totalSupply(), "ERC721Enumerable: global index out of bounds");
        return _allTokens[index];
    }

    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual override {
        super._beforeTokenTransfer(from, to, tokenId);

        if (from == address(0)) {
            _addTokenToAllTokensEnumeration(tokenId);
        } else if (from != to) {
            _removeTokenFromOwnerEnumeration(from, tokenId);
        }
        if (to == address(0)) {
            _removeTokenFromAllTokensEnumeration(tokenId);
        } else if (to != from) {
            _addTokenToOwnerEnumeration(to, tokenId);
        }
    }

    function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
        uint256 length = ERC721.balanceOf(to);
        _ownedTokens[to][length] = tokenId;
        _ownedTokensIndex[tokenId] = length;
    }

    function _addTokenToAllTokensEnumeration(uint256 tokenId) private {
        _allTokensIndex[tokenId] = _allTokens.length;
        _allTokens.push(tokenId);
    }

    function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private {
        uint256 lastTokenIndex = ERC721.balanceOf(from) - 1;
        uint256 tokenIndex = _ownedTokensIndex[tokenId];

        // When the token to delete is the last token, the swap operation is unnecessary
        if (tokenIndex != lastTokenIndex) {
            uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];

            _ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
            _ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
        }

        // This also deletes the contents at the last position of the array
        delete _ownedTokensIndex[tokenId];
        delete _ownedTokens[from][lastTokenIndex];
    }

    function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private {
        uint256 lastTokenIndex = _allTokens.length - 1;
        uint256 tokenIndex = _allTokensIndex[tokenId];

        uint256 lastTokenId = _allTokens[lastTokenIndex];

        _allTokens[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
        _allTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index

        // This also deletes the contents at the last position of the array
        delete _allTokensIndex[tokenId];
        _allTokens.pop();
    }
}

interface IDDFERC721PoolPair is IERC721Enumerable {
    function name() external view returns (string memory);
    function symbol() external view returns (string memory);
    function tokenURI(uint256 tokenId) external view returns (string memory);

    function mint(address owner, uint256 tokenId) external;
    function burn(address owner, uint256 lpTokenId) external;
    function updateTokenTime(address owner, uint256 lpTokenId) external;
    function setInterestRate(uint32 _reward)  external;
    function tokenInfo(uint256 lpTokenId) external view returns (uint32, uint32, uint32);
}

contract DDFERC721PoolPair is IDDFERC721PoolPair, ERC721Enumerable {
    address public factory;
    address public token;
    uint32 private interestRate = 30000;
    uint32  private blockStartTime; // uses single storage slot, accessible via getReserves

    mapping(uint256 => uint32) private _lpTokensTimes;

    // Token name
    string private _name;

    // Token symbol
    string private _symbol;

    constructor(){
        factory = msg.sender;
        blockStartTime = uint32(block.timestamp % 2 ** 32);
    }

    // called once by the factory at time of deployment
    function initialize(string memory name_, string memory symbol_, address _token, uint32 _interestRate) external lock {
        require(msg.sender == factory, 'DDFNFT: FORBIDDEN'); // sufficient check
        _name = name_;
        _symbol = symbol_;
        token = _token;
        interestRate = _interestRate;
    }

    /**
     * @dev See {IERC721Metadata-name}.
     */
    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * @dev See {IERC721Metadata-symbol}.
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    function mint(address owner, uint256 tokenId) external lock override {
        require(_msgSender() == IERC721(token).ownerOf(tokenId), "NFTPoolPair: transfer of token that is not owner");
        
        IERC721(token).transferFrom(_msgSender(), address(this), tokenId);
        if(_exists(tokenId)){
            _transfer(address(this), owner, tokenId);
        }else{
            _mint(owner, tokenId); 
        }
        _lpTokensTimes[tokenId] = uint32(block.timestamp % 2 ** 32);
    }

    function burn(address owner, uint256 lpTokenId) external lock override{
        require(_exists(lpTokenId), "burn: operator query for nonexistent token");
        require(ownerOf(lpTokenId) == owner, "ERC721: burn NFT of lpTokenId that is not owner");
        require(_isApprovedOrOwner(_msgSender(), lpTokenId), "ERC721: transfer caller is not owner nor approved");

        IERC721(token).transferFrom(address(this), owner, lpTokenId);
        
        _transfer(owner, address(this), lpTokenId);
        delete _lpTokensTimes[lpTokenId];
    }

    function updateTokenTime(address owner, uint256 lpTokenId) 
        external 
        lock 
        override{
        require(_exists(lpTokenId), "updateTokenTime: operator query for nonexistent token");
        require(ownerOf(lpTokenId) == owner, "DDFERC721PoolPair: retrieve NFT of token that is not owner");
        require(_isApprovedOrOwner(_msgSender(), lpTokenId), "ERC721: transfer caller is not owner nor approved");

        _lpTokensTimes[lpTokenId] = uint32(block.timestamp % 2 ** 32);
    }

    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        require(_exists(tokenId), "DDFERC721PoolPair: URI query for nonexistent token");
        return IDDFERC721PoolPair(token).tokenURI(tokenId);
    }

    function tokenInfo(uint256 lpTokenId) external view override returns (uint32, uint32, uint32){
        return (blockStartTime, _lpTokensTimes[lpTokenId], interestRate);
    }

    function setInterestRate(uint32 _interestRate) external lock override{
        require(msg.sender == factory, "DDFERC721PoolPair: FORBIDDEN"); // sufficient check
        interestRate = _interestRate;
    }
}

interface IDDFERC721Factory {
    function getPair(address token) external view returns (address pair);
    function allTokensLength() external view returns (uint);
    function createPair(string memory name_, string memory symbol_, address token, uint32 interestRate) external returns (address pair);
    function setInterestRate(address token, uint32 _interestRate) external;
}

contract DDFERC721Factory is IDDFERC721Factory {
    mapping(address => address) private pairs;
    address[] private allTokens;
    address private _owner;

    event PairCreated(address indexed token, address pair, uint);

    constructor() {
        _owner = msg.sender;
    }

    modifier onlyOwner() {
        require(_owner == msg.sender, "DDFERC20Factory: caller is not the owner");
        _;
    }
    
    function getPair(address token) external view override returns (address pair){
         pair = pairs[token];
    }

    function allTokensLength() public view override returns (uint) {
        return allTokens.length;
    }

    function findAllTokens() external view returns (address[] memory){
        return allTokens;
    }

    function createPair(string memory name_, string memory symbol_, address token, uint32 interestRate) external onlyOwner override returns (address pair) {
        require(token != address(0), 'DDFERC721Factory: ZERO_ADDRESS');
        require(pairs[token] == address(0), 'DDFERC721Factory: PAIR_EXISTS'); // single check is sufficient
        bytes memory bytecode = type(DDFERC721PoolPair).creationCode;
        bytes32 salt = keccak256(abi.encodePacked(token));
        assembly {
            pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
        }
        DDFERC721PoolPair(pair).initialize(name_, symbol_, token, interestRate);
        pairs[token] = pair;
        allTokens.push(token);
        emit PairCreated(token, pair, allTokens.length);
    }

    function setInterestRate(address token, uint32 _interestRate) external onlyOwner override{
        address pair = pairs[token]; 
        require(pair != address(0), "ERC721: The DDFFactory query for nonexistent pair");

        DDFERC721PoolPair(pair).setInterestRate(_interestRate);
    }
}