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

  Counters.Counter private supply;

  string public baseURI;
  string public baseExtension = ".json";
  string public notRevealedUri;
  uint256 public cost = 0.2 ether;
  uint256 public maxSupply = 7777;
  uint256 public maxMintAmount = 50;
  uint256 public nftPerAddressLimit = 50;
  uint256 public nftPerAddressLimitWL = 2;
  bool public paused = true;
  bool public revealed = false;
  bool public onlyWhitelisted = true;
  mapping(address => bool) whitelistedAddresses;

  constructor(
    string memory _initBaseURI,
    string memory _initNotRevealedUri
  ) ERC721("Timepiece Ape Society", "TAS") {
    setBaseURI(_initBaseURI);
    setNotRevealedURI(_initNotRevealedUri);
  }



  // INTERNAL
  function _baseURI() internal view virtual override returns (string memory) {
    return baseURI;
  }

  //MINT
  function mint(uint256 _mintAmount) public payable {
    require(!paused, "the contract is paused");
    require(_mintAmount > 0, "need to mint at least 1 NFT");
    require(_mintAmount <= maxMintAmount, "max mint amount per session exceeded");
    require(supply.current() + _mintAmount <= maxSupply, "max NFT limit exceeded");

    if (msg.sender != owner()) {
        if(onlyWhitelisted == true) {
            require(isWhitelisted(msg.sender), "user is not whitelisted");
            uint256 ownerTokenCount = balanceOf(msg.sender);
            require(ownerTokenCount + _mintAmount <= nftPerAddressLimitWL, "max Whitelist Mint exceeded");
        }
        require(msg.value >= cost * _mintAmount, "insufficient funds");
    }
    else {
          uint256 ownerTokenCount = balanceOf(msg.sender);
          require(ownerTokenCount + _mintAmount <= nftPerAddressLimit, "max NFT per address exceeded");
          require(msg.value >= cost * _mintAmount, "insufficient funds");
    }

     _mintLoop(msg.sender, _mintAmount);
  }

   function mintForOwner(uint256 _mintAmount) public onlyOwner {
    require(!paused);
    require(_mintAmount > 0);
    require(_mintAmount <= maxMintAmount);
    require(supply.current() + _mintAmount <= maxSupply);

    _mintLoop(msg.sender, _mintAmount);
  }
  
  	function _mintLoop(address _receiver, uint256 _mintAmount) internal {
    for (uint256 i = 0; i < _mintAmount; i++) {
      supply.increment();
      _safeMint(_receiver, supply.current());
    }
  }
 
  
  function gift(uint256 _mintAmount, address destination) public onlyOwner {
    require(_mintAmount > 0, "need to mint at least 1 NFT");
    require(supply.current() + _mintAmount <= maxSupply, "max NFT limit exceeded");

   _mintLoop(msg.sender, _mintAmount);
  }

  //PUBLIC VIEWS
  function isWhitelisted(address _user) public view returns (bool) {
    return whitelistedAddresses[_user];
  }

   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;
  }

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

    if (!revealed) {
      return notRevealedUri;
    } else {
      string memory currentBaseURI = _baseURI();
      return
        bytes(currentBaseURI).length > 0
          ? string(
            abi.encodePacked(currentBaseURI, tokenId.toString(), baseExtension)
          )
          : "";
    }
  }
  
    function totalSupply() public view returns (uint256) {
    return supply.current();
  }


  //ONLY OWNER VIEWS
  function getBaseURI() public view onlyOwner returns (string memory) {
    return baseURI;
  }

  function getContractBalance() public view onlyOwner returns (uint256) {
    return address(this).balance;
  }

  //ONLY OWNER SETTERS
  function reveal(bool _state) public onlyOwner {
    revealed = _state;
  }

  function pause(bool _state) public onlyOwner {
    paused = _state;
  }

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

    function setWLlimit(uint256 _limit) public onlyOwner {
    nftPerAddressLimitWL = _limit;
  }


  function setCost(uint256 _newCost) public onlyOwner {
    cost = _newCost;
  }

  function setmaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner {
    maxMintAmount = _newmaxMintAmount;
  }

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

  function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
    baseExtension = _newBaseExtension;
  }

  function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
    notRevealedUri = _notRevealedURI;
  }

  function setMaxSupply(uint256 _maxSupply) public onlyOwner {
    maxSupply = _maxSupply;
  }

    function setOnlyWhitelisted(bool _state) public onlyOwner {
    onlyWhitelisted = _state;
  }

  function whitelistUsers(address[] memory addresses) public onlyOwner {
    for (uint256 i = 0; i < addresses.length; i++) {
      whitelistedAddresses[addresses[i]] = true;
    }
  }

  function withdraw() public payable onlyOwner {
    (bool success, ) = payable(msg.sender).call{value: address(this).balance}("");
    require(success);
  }
}
function getBaseURI() public view onlyOwner returns (string memory) {
  return baseURI;
}

//ONLY OWNER VIEWS