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

  string private baseURI;
  string private notRevealedUri;
  string public baseExtension = ".json";
  uint256 public cost = 0.04 ether;
  uint256 public trenchLimit = 918;
  uint256 public maxSupply = 10000;
  uint256 public maxMintAmount = 5;
  uint256 public tokenIndexOffset = 0;
  bool public paused = false;
  bool public revealed = false;
  mapping(address => bool) public whitelisted;

  constructor(
    string memory _initBaseURI,
    string memory _initNotRevealedUri
  ) ERC721("Sovereign Degens", "SVDGN") {
    setBaseURI(_initBaseURI);
    setNotRevealedURI(_initNotRevealedUri);
    _mintReservedTokens();
  }

  //// INTERNAL FUNCTIONS
  function _baseURI() internal view virtual override returns (string memory) {
    return baseURI;
  }
  
  // Preminting 20 for marketing and community initatives
  function _mintReservedTokens() internal onlyOwner {
      uint256 supply = totalSupply();
      uint256 _mintAmount = 20;
      require(supply == 0);
      
      for (uint256 i = 1; i <= _mintAmount; i++) {
      _safeMint(owner(), supply + i);
    }
  }



  //// PUBLIC FUNCTIONS
  function mint(address _to, uint256 _mintAmount) public payable {
    uint256 supply = totalSupply();
    require(!paused);
    require(_mintAmount > 0);
    require(_mintAmount <= maxMintAmount);
    require(supply + _mintAmount <= trenchLimit);

    if (msg.sender != owner()) {
        if(whitelisted[msg.sender] != true) {
          require(msg.value >= cost * _mintAmount, "Did not send enough ether for minting");
        }
    }

    for (uint256 i = 1; i <= _mintAmount; i++) {
      _safeMint(_to, supply + i);
    }
  }

  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"
    );
    
    int256 currentSupply = int256(totalSupply()); 
    int256 _tokenId = int256(tokenId);
    int256 _tokenIndexOffset = int256(tokenIndexOffset);
    uint256 postOffsetTokenId = uint256(((_tokenId + (_tokenIndexOffset -1)) % currentSupply) + 1);
    
    if(revealed == false) {
        return string(abi.encodePacked(notRevealedUri, postOffsetTokenId.toString(), baseExtension));
    }

    string memory currentBaseURI = _baseURI();
    return bytes(currentBaseURI).length > 0
        ? string(abi.encodePacked(currentBaseURI, postOffsetTokenId.toString(), baseExtension))
        : "";
  }
  
  function remainingSupply() public view returns (uint256) {
        uint256 currentSupply = totalSupply();
        return (trenchLimit - currentSupply);
  }

  //// OWNER-ONLY FUNCTIONS
  function setRevealToken() public onlyOwner() {
      revealed = true;
  }
  
  function setCost(uint256 _newCost) public onlyOwner() {
    cost = _newCost;
  }

  function setmaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner() {
    maxMintAmount = _newmaxMintAmount;
  }
  
  function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
    notRevealedUri = _notRevealedURI;
  }
  
  function setTokenIndexOffset(uint256 _newOffsetValue) public onlyOwner {
      tokenIndexOffset = _newOffsetValue;
  }

  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 whitelistUser(address _user) public onlyOwner {
    whitelisted[_user] = true;
  }
 
  function removeWhitelistUser(address _user) public onlyOwner {
    whitelisted[_user] = false;
  }

  function withdraw() public payable onlyOwner {
    (bool success, ) = payable(msg.sender).call{value: address(this).balance}("");
    require(success);
  }

  function setTrench(uint256 _newtrenchLimit, uint256 _newCost) public onlyOwner {
    uint256 currentTrenchLimit = trenchLimit;

    require(_newCost >= cost);
    require(_newtrenchLimit >= currentTrenchLimit);
    require(_newtrenchLimit <= maxSupply);
    
    setCost(_newCost);
    trenchLimit = _newtrenchLimit;
    paused = false;
  }
}
function _baseURI() internal view virtual override returns (string memory) {
  return baseURI;
}

//// INTERNAL FUNCTIONS