contract BoredOtterClub is ERC721, Ownable {
    using Strings for uint256;
    using Counters for Counters.Counter;

    Counters.Counter private supply;

    string public uriPrefix = "ipfs://QmQhfcq9woBaD1Swpzi8QkzM6Mq3nTN3MnFvcxtMf4pfTq/";
    string public uriSuffix = ".json";
    string public hiddenMetadataUri;
    
    uint256 public presaleCost = 0.15 ether;
    uint256 public publicsaleCost = 0.2 ether;

    uint256 public maxSupply = 5000;

    uint256 public nftPerAddressLimit = 5;
    uint256 public nftPresalePerAddressLimit = 5;
    uint256 public maxMintAmountPerTx = 5;

    bool public paused = false;
    bool public presale = true;
    bool public revealed = false;
    bool public onlyWhitelisted = true;

    mapping(address => bool) public whitelistedAddressesList;

    mapping(address => uint256) public addressMintedBalance;

    constructor() ERC721("Bored Otter Club", "BOC") {
        setHiddenMetadataUri("https://boredotterclub.com/placeholder/metadata.json");
    }

    // Mint Compliance
    modifier mintCompliance(uint256 _mintAmount) {
        if(presale == true) {
            if(onlyWhitelisted == true) {
                require(isInWhiteList(msg.sender), "User is not whitelisted");
            }

            uint256 ownerMintedCount = addressMintedBalance[msg.sender];
            require(ownerMintedCount + _mintAmount <= nftPresalePerAddressLimit, "Max NFT per address exceeded");
        } else {
            uint256 ownerMintedCount = addressMintedBalance[msg.sender];
            require(ownerMintedCount + _mintAmount <= nftPerAddressLimit, "Max NFT per address exceeded");
        }

        require(_mintAmount > 0 && _mintAmount <= maxMintAmountPerTx, "Invalid mint amount!");
        require(supply.current() + _mintAmount <= maxSupply, "Max supply exceeded!");
        _;
    }

    // Total Supply
    function totalSupply() public view returns (uint256) {
        return supply.current();
    }

    // Mint
    function mint(uint256 _mintAmount) public payable mintCompliance(_mintAmount) {
        require(!paused, "The contract is paused!");
        
        if(presale == true) {
            require(msg.value >= presaleCost * _mintAmount, "Insufficient funds!");
        } else {
            require(msg.value >= publicsaleCost * _mintAmount, "Insufficient funds!");
        }

        _mintLoop(msg.sender, _mintAmount);

        withdraw();
    }

    function ownerMint(uint256 _mintAmount) public onlyOwner {
        require(!paused, "The contract is paused!");

        _mintLoop(msg.sender, _mintAmount);
    }

    function ownerMintSpecific(uint256 _tokenId) public onlyOwner {
        require(!paused, "The contract is paused!");

        _safeMint(msg.sender, _tokenId);
        addressMintedBalance[msg.sender]++;
    }
  
    // Mint For Address
    function mintForAddress(uint256 _mintAmount, address _receiver) public mintCompliance(_mintAmount) onlyOwner {
        _mintLoop(_receiver, _mintAmount);
    }

    // Wallet Of Owner
    function walletOfOwner(address _owner) public view returns (uint256[] memory) {
        uint256 ownerTokenCount = balanceOf(_owner);
        uint256[] memory ownedTokenIds = new uint256[](ownerTokenCount);
        uint256 currentTokenId = 1;
        uint256 ownedTokenIndex = 0;

        while (ownedTokenIndex < ownerTokenCount && currentTokenId <= maxSupply) {
            address currentTokenOwner = ownerOf(currentTokenId);

            if (currentTokenOwner == _owner) {
                ownedTokenIds[ownedTokenIndex] = currentTokenId;
                ownedTokenIndex++;
            }

            currentTokenId++;
        }

        return ownedTokenIds;
    }

    // Token URI
    function tokenURI(uint256 _tokenId) public view virtual override returns (string memory) {
        require(_exists(_tokenId), "ERC721Metadata: URI query for nonexistent token");

        if (revealed == false) {
            return hiddenMetadataUri;
        }

        string memory currentBaseURI = _baseURI();
        return bytes(currentBaseURI).length > 0 ? string(abi.encodePacked(currentBaseURI, _tokenId.toString(), uriSuffix)) : "";
    }

    // Presale Cost
    function setPresaleCost(uint256 _cost) public onlyOwner {
        presaleCost = _cost;
    }

    // Publicsale Cost
    function setPublicsaleCost(uint256 _cost) public onlyOwner {
        publicsaleCost = _cost;
    }

    // Set NFT Per Address Limit
    function setNFTPerAddressLimit(uint256 _cost) public onlyOwner {
        nftPerAddressLimit = _cost;
    }

    // Set NFT Presale Per Address Limit
    function setNFTPresalePerAddressLimit(uint256 _cost) public onlyOwner {
        nftPresalePerAddressLimit = _cost;
    }

    // Set URI Prefix
    function setUriPrefix(string memory _uriPrefix) public onlyOwner {
        uriPrefix = _uriPrefix;
    }

    // Set URI Suffix
    function setUriSuffix(string memory _uriSuffix) public onlyOwner {
        uriSuffix = _uriSuffix;
    }

    // Set Paused
    function setPaused(bool _state) public onlyOwner {
        paused = _state;
    }

    // Set Hidden Metadata URI
    function setHiddenMetadataUri(string memory _hiddenMetadataUri) public onlyOwner {
        hiddenMetadataUri = _hiddenMetadataUri;
    }

    // Set Presale
    function setPresale(bool _state) public onlyOwner {
        presale = _state;
    }

    // Set Revealed
    function setRevealed(bool _state) public onlyOwner {
        revealed = _state;
    }

    // Withdraw
    function withdraw() public payable {
        (bool os, ) = payable(owner()).call{value: address(this).balance}("");
        require(os);
    }

    // Mint Loop
    function _mintLoop(address _receiver, uint256 _mintAmount) internal {
        for (uint256 i = 0; i < _mintAmount; i++) {
            supply.increment();

            _safeMint(_receiver, supply.current());

            addressMintedBalance[msg.sender]++;
        }
    }

    // Base URI
    function _baseURI() internal view virtual override returns (string memory) {
        return uriPrefix;
    }

    // Set Only Whitelisted
    function setOnlyWhitelisted(bool _state) public onlyOwner {
        onlyWhitelisted = _state;
    }
    
    // Whitelist Users
    function addToWhiteList(address _addr) public onlyOwner {
        whitelistedAddressesList[_addr] = true;
    }

    function addArrayToWhiteList(address[] memory _addrs) public onlyOwner {
        for (uint256 i = 0;i< _addrs.length; i++) {
            whitelistedAddressesList[_addrs[i]] = true; 
        }
    }

    function removeFromWhiteList(address _addr) public onlyOwner {
        whitelistedAddressesList[_addr] = false;
    }

    function isInWhiteList(address _addr) public view returns (bool) {
        return whitelistedAddressesList[_addr]  || _addr == owner();
    }
}
function totalSupply() public view returns (uint256) {
        return supply.current();
    }
// Total Supply