contract NonFungibleToken is Context, AccessControl, ERC721Burnable, ERC721Pausable {
    using Counters for Counters.Counter;

    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
    bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
    address public owner;
    bool public whitelistingEnabled = false;
    bool public mintingEnabled = true;
    uint256 public maxPerWallet = 100;
    uint256 public premintRound = 0;
    uint256 public nftPerCall = 50;
   // mapping (address => bool) public whitelists;
    Counters.Counter private _tokenIdTracker;

    /**
     * @dev Grants `DEFAULT_ADMIN_ROLE`, `MINTER_ROLE` and `PAUSER_ROLE` to the
     * account that deploys the contract.
     *
     * Token URIs will be autogenerated based on `baseURI` and their token IDs.
     * See {ERC721-tokenURI}.
     */
    constructor(string memory name, string memory symbol, string memory baseURI) public ERC721(name, symbol) {
        _setupRole(DEFAULT_ADMIN_ROLE, _msgSender());

        _setupRole(MINTER_ROLE, _msgSender());
        _setupRole(PAUSER_ROLE, _msgSender());

        _setBaseURI(baseURI);

        owner = msg.sender;
    }

   /* function toggleWhitelisting(bool _toggle) public virtual {
        require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "NonFungibleToken: must have admin role");
        whitelistingEnabled = _toggle;

    }*/

    function mintOwner() public virtual {
        require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "NonFungibleToken: must have minter role to mint");
        require(premintRound < 3, "All treasury NFTs have been minted");
        for(uint i = 0; i < nftPerCall; i++){
            _mint(msg.sender, _tokenIdTracker.current() +1);

            _tokenIdTracker.increment();
        }
        premintRound = premintRound + 1;

    }

    function setURI(string memory baseURI) public virtual {
        require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "NonFungibleToken: must have admin role");
        _setBaseURI(baseURI);

    }

    function setMaxPerWallet(uint256 _maxPerWallet) public virtual {
        require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "NonFungibleToken: must have minter role to mint");
        maxPerWallet = _maxPerWallet;

    }


    function setOwner(address _owner) public virtual {
        require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "NonFungibleToken: must have admin role to mint");
        owner = _owner;

    }

    function toggleMinting(bool _bool) public virtual {
        require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "NonFungibleToken: must have admin role to mint");
        mintingEnabled = _bool;

    }

    function toggleWhitelisting(bool _toggle) public virtual {
        require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "NonFungibleToken: must have admin role");
        whitelistingEnabled = _toggle;

    }

    function whitelist(address _account) public virtual {
        require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "NonFungibleToken: must have admin role");
        whitelists[_account] = true;

    }



    function contractURI() public view returns (string memory) {
        return string(abi.encodePacked(baseURI(), "contract-metadata.json"));
    }



    /**
     * @dev Creates a new token for `to`. Its token ID will be automatically
     * assigned (and available on the emitted {IERC721-Transfer} event), and the token
     * URI autogenerated based on the base URI passed at construction.
     *
     * See {ERC721-_mint}.
     *
     * Requirements:
     *
     * - the caller must have the `MINTER_ROLE`.
     */
    function mint(address to) public virtual {
        require(hasRole(MINTER_ROLE, _msgSender()), "NonFungibleToken: must have minter role to mint");
        require(whitelists[to] || ! whitelistingEnabled, "User not whitelisted !");
        
        require(mintingEnabled, "Minting not enabled !");
        // We cannot just use balanceOf to create the new tokenId because tokens
        // can be burned (destroyed), so we need a separate counter.
        _mint(to, _tokenIdTracker.current() + 1);
        _tokenIdTracker.increment();
        require(balanceOf(to) <= maxPerWallet, "Max NFTs reached by wallet");
    }

    /**
     * @dev Pauses all token transfers.
     *
     * See {ERC721Pausable} and {Pausable-_pause}.
     *
     * Requirements:
     *
     * - the caller must have the `PAUSER_ROLE`.
     */
    function pause() public virtual {
        require(hasRole(PAUSER_ROLE, _msgSender()), "NonFungibleToken: must have pauser role to pause");
        _pause();
    }

    /**
     * @dev Unpauses all token transfers.
     *
     * See {ERC721Pausable} and {Pausable-_unpause}.
     *
     * Requirements:
     *
     * - the caller must have the `PAUSER_ROLE`.
     */
    function unpause() public virtual {
        require(hasRole(PAUSER_ROLE, _msgSender()), "NonFungibleToken: must have pauser role to unpause");
        _unpause();
    }

    function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual override(ERC721, ERC721Pausable) {
        super._beforeTokenTransfer(from, to, tokenId);
    }
}
/**
 * @dev {ERC721} token, including:
 *
 *  - ability for holders to burn (destroy) their tokens
 *  - a minter role that allows for token minting (creation)
 *  - a pauser role that allows to stop all token transfers
 *  - token ID and URI autogeneration
 *
 * This contract uses {AccessControl} to lock permissioned functions using the
 * different roles - head to its documentation for details.
 *
 * The account that deploys the contract will be granted the minter and pauser
 * roles, as well as the default admin role, which will let it grant both minter
 * and pauser roles to other accounts.
 */
function mintOwner() public virtual {
    require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "NonFungibleToken: must have minter role to mint");
    require(premintRound < 3, "All treasury NFTs have been minted");
    for(uint i = 0; i < nftPerCall; i++){
        _mint(msg.sender, _tokenIdTracker.current() +1);

        _tokenIdTracker.increment();
    }
    premintRound = premintRound + 1;

}

/* function toggleWhitelisting(bool _toggle) public virtual {
 require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "NonFungibleToken: must have admin role");
 whitelistingEnabled = _toggle;
 
 }*/