contract DoodleCatsClub is ERC721Enumerable, Ownable {
  using Strings for uint256;

  string baseURI;
  string public baseExtension = ".json";
  uint256 public maxSupply = 3333;
  bool public paused = true;
  // Prices   
  uint256 public presaleCost = 0.05 ether;
  uint256 public publicSaleCost = 0.07 ether;
  uint256 public freeMintCost = 0 ether;
  // Active sales
  bool public freeMintActive = false;
  bool public presaleActive = false;
  bool public publicsaleActive = false;
  // Max mint amount   
  uint256 public freeMaxMintAmountGreator61 = 1;
  uint256 public freeMaxMintAmount = 2;
  uint256 private freeMintMaxSupply = 101;
  uint256 public presaleMaxMintAmount = 3;
  uint256 public publicsaleMaxMintAmount = 3;
  uint256 public maxMintPerWallet = 5;
  // Whitelisted address
  address[] public whitelistedAddresses;
  address[] public freeMintWhitelistedAddresses;
  mapping(address => uint256) public addressMintedBalance;
  address public ownerWallet = 0xB836A10A3F1ef90A8FCF64ACaf32D9B74E487ded;
  uint256 public ownerWalletPercent = 50;
  string public notRevealedUri;
  bool public revealed = true;

  constructor() ERC721("Doodle Cats Club", "DCC") {
    setBaseURI("https://ipfs.io/ipfs/QmTsueCHSWw1qscc5g7FQc5vC8S8ciNVX83WwCwQiz3JR9/");
    setNotRevealedURI("http://ipfs.io/ipfs/QmToTehgmcCj38gNxqHrEkVVBc9fR1ab5dyk1CDRQ1y1yW/hidden.json");
    // mint(20);
  }

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

  function cost () internal view virtual returns (uint256) {
      if(freeMintActive) {
          return freeMintCost;
      }

      if(presaleActive) {
          return presaleCost;
      }

      return publicSaleCost;
  }

  // public
  function mint(uint256 _mintAmount) public payable {
    uint256 supply = totalSupply();
    require(!paused, "The contract has been paused");
    require((freeMintActive || presaleActive || publicsaleActive), "Sale is not active yet");
    require(_mintAmount > 0, "Mint amount should be 1 or greater than 1");
    if(freeMintActive) {
        require(supply + _mintAmount <= freeMintMaxSupply, "Mint limit exceeded the free mint limit");
    }
    uint256 maxMintAmountLimit = maxMintLimit();
    require(_mintAmount <= maxMintAmountLimit, "Mint amount exceeded the max mint amount");
    uint256 walletMintedAmount = addressMintedBalance[msg.sender];
    uint256 maxMintPerWalletLimitBal = maxMintPerWalletLimit();
    require(_mintAmount + walletMintedAmount <= maxMintPerWalletLimitBal, "Mint amount exceeded the max per wallet mint amount");
    require(supply + _mintAmount <= maxSupply, "Mint amount exceeded the max supply");

    if (msg.sender != owner()) {
        if(onlyWhitelisted() == true) {
            require(isWhitelisted(msg.sender), "User is not whitelisted");
        }
      require(msg.value >= cost() * _mintAmount, "Insufficient funds");
    }

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

  function maxMintPerWalletLimit() internal view returns (uint256) {
      if(freeMintActive) {
          if(totalSupply() < 61) {
              return freeMaxMintAmount;
          }else {
              return freeMaxMintAmountGreator61;
          }
      }

      if(presaleActive) {
          return presaleMaxMintAmount;
      }

      return maxMintPerWallet;
  }

  function maxMintLimit() internal view returns (uint256) {
      if(freeMintActive) {
          if(totalSupply() < 61) {
              return freeMaxMintAmount;
          }else {
              return freeMaxMintAmountGreator61;
          }
      }

      if(presaleActive) {
          return presaleMaxMintAmount;
      }

      return publicsaleMaxMintAmount;
  }

  function onlyWhitelisted() internal view returns (bool) {
    if(freeMintActive) {
          return true;
      }

      if(presaleActive) {
          return true;
      }

      return false;
  }

  function isWhitelisted(address _user) public view returns (bool) {
    address[] memory whitelistedAddress = !presaleActive ? freeMintWhitelistedAddresses : whitelistedAddresses;
    for (uint i = 0; i < whitelistedAddress.length; i++) {
      if (whitelistedAddress[i] == _user) {
          return true;
      }
    }
    return false;
  }

  function reveal(bool _reveal) public onlyOwner() {
      revealed = _reveal;
  }

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

  function walletOfOwner(address _owner)
    public
    view
    returns (uint256[] memory)
  {
    uint256 ownerTokenCount = balanceOf(_owner);
    uint256[] memory tokenIds = new uint256[](ownerTokenCount);
    for (uint256 i; i < ownerTokenCount; i++) {
      tokenIds[i] = tokenOfOwnerByIndex(_owner, i);
    }
    return tokenIds;
  }

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

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

    string memory currentBaseURI = _baseURI();
    return bytes(currentBaseURI).length > 0
        ? string(abi.encodePacked(currentBaseURI, tokenId.toString(), baseExtension))
        : "";
  }
  
  function setFreeMintCost(uint256 _newCost) public onlyOwner {
    freeMintCost = _newCost;
  }
  function setPresaleCost(uint256 _newCost) public onlyOwner {
    presaleCost = _newCost;
  }
  function setPublicsaleCost(uint256 _newCost) public onlyOwner {
    publicSaleCost = _newCost;
  }

  function setfreeMaxMintAmountgreator61(uint256 _newmaxMintAmount) public onlyOwner {
    freeMaxMintAmountGreator61 = _newmaxMintAmount;
  }
  function setfreeMaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner {
    freeMaxMintAmount = _newmaxMintAmount;
  }
  function setPresaleMaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner {
    presaleMaxMintAmount = _newmaxMintAmount;
  }
  function setPublicMaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner {
    publicsaleMaxMintAmount = _newmaxMintAmount;
  }

  function setOwnerWallet(address walletAddress) public onlyOwner {
        ownerWallet = walletAddress;
    }

    function setOwnerWalletPercent(uint256 percent) public onlyOwner {
        ownerWalletPercent = percent;
    }

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

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

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

  function setFreeMintActive(bool _state) public onlyOwner {  
    freeMintActive = _state;
  }
  function setPresaleActive(bool _state) public onlyOwner {
    presaleActive = _state;
  }
  function setPublicSaleActive(bool _state) public onlyOwner {
    publicsaleActive = _state;
  }

  function setwhitelistUsersForFree(address[] calldata _users) public onlyOwner {
    delete freeMintWhitelistedAddresses;
    freeMintWhitelistedAddresses = _users;
  }
  
  function whitelistUsers(address[] calldata _users) public onlyOwner {
    delete whitelistedAddresses;
    whitelistedAddresses = _users;
  }
 
  function withdraw() public payable onlyOwner {
    require(address(this).balance > 0, "Balance is 0");
    uint256 remainingPer = 100 -  ownerWalletPercent;
    payable(ownerWallet).transfer(address(this).balance / 100 * ownerWalletPercent);
    payable(owner()).transfer(address(this).balance / 100 * remainingPer);
  }
}
function _baseURI() internal view virtual override returns (string memory) {
  return baseURI;
}

// internal