contract NoLimitLlamas is ERC721Enumerable, Ownable, PaymentSplitter {

    using Strings for uint256;

    uint256 public constant maxSupply = 10000;
    uint256 private _price;
    uint256 private _reserved = 300;
    uint256 public maxMintAmount = 30;
    uint256 public nftPerAddressLimit = 5;

    string public provenanceHash = "";
    string public baseURI;
    string public baseExtension = ".json";

    bool public _saleStarted = false;
    bool public onlyWhitelisted = true;
    bool public revealed = false;
    
    mapping(address => bool) private whitelistedAddresses;
    mapping(address => uint256) public addressMintedBalance;
    address[] private _team = [
        0x3b68935DEbf3BE9fcED7702c352dAc0d118223a3,
        0x12Ec2B7A3490CDd3cA67AEb1E212e34b121Bf66F,
        0xcFBec7f118D28fb2a75D1ef77b046733B29328D9,
        0xfE10502945D0BaDB57C9Ab9db6fB8A2F7301d183,
        0x80B08457d05C19FC061e3a0454EF38DebE247Eb2
    ];
    uint256[] private _teamShares = [8500, 1000, 200, 150, 150];

    modifier whenSaleStarted() {
        require(_saleStarted);
        _;
    }

    constructor(
    uint256 _presalePrice,
    string memory _initNotRevealedUri
    ) ERC721("No Limit Llamas", "NLL")
    PaymentSplitter(_team, _teamShares) {
    setPrice(_presalePrice);
    setBaseURI(_initNotRevealedUri);
    }


    function mint(uint256 _nbTokens) external payable whenSaleStarted {
        uint256 supply = totalSupply();
    require(_nbTokens > 0, "need to mint at least 1 NFT");
    require(_nbTokens <= maxMintAmount, "max mint amount per session exceeded");
    require(supply + _nbTokens <= maxSupply - _reserved, "max NFT limit exceeded");

    if (msg.sender != owner()) {
        if(onlyWhitelisted) {
            require(isWhitelisted(msg.sender) == true, "user is not whitelisted");
            uint256 ownerMintedCount = addressMintedBalance[msg.sender];
            require(ownerMintedCount + _nbTokens <= nftPerAddressLimit, "max NFT per address exceeded");
        }
        require(msg.value >= _price * _nbTokens, "insufficient funds");
    }

    for (uint256 i = 1; i <= _nbTokens; i++) {
      addressMintedBalance[msg.sender] = addressMintedBalance[msg.sender] + 1;
      _safeMint(msg.sender, supply + i);
    }
  }

    function flipSaleStarted() public onlyOwner {
        _saleStarted = !_saleStarted;
    }
  
    function isWhitelisted(address _user) public view returns (bool) {
      return whitelistedAddresses[_user];
    }

    function addToWhitelist(address[] calldata _addresses) external onlyOwner {
        for (uint256 i = 0; i < _addresses.length; i++) {
            require(_addresses[i] != address(0), "Can't add a zero address");
            if (whitelistedAddresses[_addresses[i]] == false) {
                whitelistedAddresses[_addresses[i]] = true;
            }
        }
    }

    function removeFromWhitelist(address[] calldata _addresses) external onlyOwner {
        for (uint256 i = 0; i < _addresses.length; i++) {
            require(_addresses[i] != address(0), "Can't remove a zero address");
            if (whitelistedAddresses[_addresses[i]] == true) {
                whitelistedAddresses[_addresses[i]] = false;
            }
        }
    }
    
    function reveal() public onlyOwner {
      revealed = true;
    }
    
    function setOnlyWhitelisted(bool _state) public onlyOwner {
    onlyWhitelisted = _state;
    }

    function setBaseURI(string memory _URI) public onlyOwner {
        baseURI = _URI;
    }

    function _baseURI() internal view override(ERC721) returns(string memory) {
        return baseURI;
    }

    // Make it possible to change the price
    function setPrice(uint256 _newPrice) public onlyOwner {
        _price = _newPrice;
    }

    function getPrice() public view returns (uint256){
        return _price;
    }

    function getReservedLeft() public view returns (uint256) {
        return _reserved;
    }

    // This should be set before sales open.
    function setProvenanceHash(string memory _provenanceHash) public onlyOwner {
        provenanceHash = _provenanceHash;
    }

    // Helper to list all the Llamas of a wallet
    function walletOfOwner(address _owner) public view returns(uint256[] memory) {
        uint256 tokenCount = balanceOf(_owner);

        uint256[] memory tokensId = new uint256[](tokenCount);
        for(uint256 i = 0; i < tokenCount; i++){
            tokensId[i] = tokenOfOwnerByIndex(_owner, i);
        }
        return tokensId;
    }

    function claimReserved(uint256 _number, address _receiver) external onlyOwner {
        uint256 supply = totalSupply();
        require(_number <= _reserved, "That would exceed the max reserved.");

        for (uint256 i = 1; i <= _number; i++) {
            addressMintedBalance[_receiver] = addressMintedBalance[_receiver] + 1;
            _safeMint(_receiver, supply + i);
        }

        _reserved = _reserved - _number;
    }
    
    function setNftPerAddressLimit(uint256 _limit) public onlyOwner() {
    nftPerAddressLimit = _limit;
    }

    function setMaxMintAmount(uint256 _maxMint) public onlyOwner() {
        maxMintAmount = _maxMint;
    }
    
    function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
    baseExtension = _newBaseExtension;
    }
    
    function tokenURI(uint256 tokenId)
    public
    view
    virtual
    override
    returns (string memory)
  {
    require(
      _exists(tokenId),
      "ERC721Metadata: URI query for nonexistent token"
    );
    
    if(revealed == false) {
    return baseURI;
    }

    string memory currentBaseURI = _baseURI();
    return bytes(currentBaseURI).length > 0
        ? string(abi.encodePacked(currentBaseURI, tokenId.toString(), baseExtension))
        : "";
  }
}
// //  //   //       //
//  // //   //       //
//   ////   //////   //////
function setPrice(uint256 _newPrice) public onlyOwner {
    _price = _newPrice;
}

// Make it possible to change the price