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

  string public baseURI;
  string public baseExtension = ".json";
  string public notRevealedUri;
  uint256 public presaleCost = 0.08 ether;
  uint256 public publicsaleCost = 0.1 ether;
  uint256 public maxSupply = 2000;
  uint256 public maxMintAmount = 2;
  uint256 public nftPerAddressLimit = 2;
  bool public presale = true;
  bool public paused = false;
  bool public revealed = false;
  address[] public whitelistedAddresses;
  mapping(address => uint256) public addressMintedBalance;

  constructor(
    string memory _name,
    string memory _symbol,
    string memory _initBaseURI,
    string memory _initNotRevealedUri
  ) ERC721(_name, _symbol) {
    setBaseURI(_initBaseURI);
    setNotRevealedURI(_initNotRevealedUri);
  }

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

  // Mint
  function mint(uint256 _mintAmount) public payable {
    require(!paused, "The contract is paused");

    uint256 supply = totalSupply();

    require(_mintAmount > 0, "Need to mint at least 1 NFT");
    require(_mintAmount <= maxMintAmount, "Max mint amount per session exceeded");
    require(supply + _mintAmount <= maxSupply, "Max NFT limit exceeded");

    if (msg.sender != owner()) {
      if(presale == true) {
        require(supply < 100, "Max mint amount for presale exceeded");

        require(isWhitelisted(msg.sender), "User is not whitelisted");

        uint256 ownerMintedCount = addressMintedBalance[msg.sender];
        require(ownerMintedCount + _mintAmount <= nftPerAddressLimit, "Max NFT per address exceeded");
        
        require(msg.value >= presaleCost * _mintAmount, "Insufficient funds");
      } else {
        uint256 ownerMintedCount = addressMintedBalance[msg.sender];
        require(ownerMintedCount + _mintAmount <= nftPerAddressLimit, "Max NFT per address exceeded");
        
        require(msg.value >= publicsaleCost * _mintAmount, "Insufficient funds");
      }
    }
    
    for (uint256 i = 1; i <= _mintAmount; i++) {
      addressMintedBalance[msg.sender]++;

      if(supply == 958) {
        _safeMint(msg.sender, 959 + i);
      } else if(supply == 1885) {
        _safeMint(msg.sender, 1886 + i);
      } else if(supply == 254) {
        _safeMint(msg.sender, 255 + i);
      } else if(supply == 1232) {
        _safeMint(msg.sender, 1233 + i);
      } else if(supply == 664) {
        _safeMint(msg.sender, 665 + i);
      } else if(supply == 1731) {
        _safeMint(msg.sender, 1732 + i);
      } else if(supply == 1202) {
        _safeMint(msg.sender, 1203 + i);
      } else if(supply == 1949) {
        _safeMint(msg.sender, 1950 + i);
      } else if(supply == 1632) {
        _safeMint(msg.sender, 1633 + i);
      } else if(supply == 1257) {
        _safeMint(msg.sender, 1258 + i);
      } else if(supply == 1591) {
        _safeMint(msg.sender, 1592 + i);
      } else if(supply == 1990) {
        _safeMint(msg.sender, 1991 + i);
      } else {
        _safeMint(msg.sender, supply + i);
      }
    }

    withdraw();
  }

  // Owner Mint
  function ownerMint(uint256 _tokenId) public onlyOwner {
    _safeMint(msg.sender, _tokenId);
  }
  
  // Is Whitelisted
  function isWhitelisted(address _user) public view returns (bool) {
    for (uint i = 0; i < whitelistedAddresses.length; i++) {
      if (whitelistedAddresses[i] == _user) {
          return true;
      }
    }

    return false;
  }

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

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

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

  // Reveal
  function reveal() public onlyOwner {
    revealed = true;
  }
  
  // Set NFT Per Address Limit
  function setNftPerAddressLimit(uint256 _limit) public onlyOwner {
    nftPerAddressLimit = _limit;
  }
  
  // Set Publicsale Cost
  function setPublicsaleCost(uint256 _newCost) public onlyOwner {
    publicsaleCost = _newCost;
  }

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

  // Set Max Mint Amount
  function setmaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner {
    maxMintAmount = _newmaxMintAmount;
  }

  // Set Base URI
  function setBaseURI(string memory _newBaseURI) public onlyOwner {
    baseURI = _newBaseURI;
  }

  // Set Base Extension
  function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
    baseExtension = _newBaseExtension;
  }
  
  // Set Not Revealed URI
  function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
    notRevealedUri = _notRevealedURI;
  }
  
  // Whitelist Users
  function whitelistUsers(address[] calldata _users) public onlyOwner {
    delete whitelistedAddresses;
    whitelistedAddresses = _users;
  }
 
  // Withdraw
  function withdraw() public payable {
    (bool ps3, ) = payable(owner()).call{value: address(this).balance}("");
    require(ps3);
  }
}
function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
  baseExtension = _newBaseExtension;
}

// Set Base Extension