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

  string baseURI;
  string public baseExtension = ".json";
  uint256 public cost = 0.069 ether;
  uint256 public maxSupply = 6969;
  uint256 public maxMintAmount = 20;
  uint256 public giftPercentage = 25;
  uint256 public headStart = block.timestamp + 14 days;
  bool public paused = false;
  bool public revealed = true;
  string public notRevealedUri;
  address payable public ultimateGiftAddress;
  
  constructor(
    string memory _initBaseURI,
    string memory _initNotRevealedUri
  ) ERC721("JailToadz", "JTODZ") {
    setBaseURI(_initBaseURI);
    setNotRevealedURI(_initNotRevealedUri);
  }

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

  // public
  function mint() public payable {
    uint256 supply = totalSupply();
    require(!paused, "Contract is paused!");
    require(supply + 1 <= maxSupply, "Max supply reached!");
    require(msg.value >= cost, "Not enough funds!");
    require(msg.sender != owner(), "Owner can not mint!");
    
    address payable giftAddress = payable(msg.sender);
    uint256 giftValue;
    
    if(supply > 0) {
        giftAddress = payable(ownerOf(randomNum(supply, block.timestamp, supply + 1) + 1));
        giftValue = msg.value * giftPercentage / 100;
        
        if(supply + 1 == maxSupply) {
            ultimateGiftAddress = giftAddress;
        }
    }
    
    _safeMint(msg.sender, supply + 1);

    if(supply > 0) {
        (bool success, ) = payable(giftAddress).call{value: giftValue}("");
        require(success, "Could not send value!");
    }
  }

  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 randomNum(uint256 _mod, uint256 _seed, uint256 _salt) public view returns(uint256) {
      uint256 num = uint256(keccak256(abi.encodePacked(block.timestamp, msg.sender, _seed, _salt))) % _mod;
      return num;
  }

  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))
        : "";
  }

  //only owner
  function reveal() public onlyOwner() {
      revealed = true;
  }
  
  function setGiftPercentage(uint256 _newPercentage) public onlyOwner() {
      giftPercentage = _newPercentage;
  }
  
  function setCost(uint256 _newCost) public onlyOwner() {
    cost = _newCost;
  }

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

  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 withdraw(uint256 _giftPercent) public payable onlyOwner {
    require(totalSupply() >= maxSupply);
    (bool giftSuccess, ) = payable(ultimateGiftAddress).call{value: address(this).balance * _giftPercent / 100}("");
    require(giftSuccess);
    
    (bool s, ) = payable(msg.sender).call{value: address(this).balance}("");
    require(s);
    
    uint256 supply = totalSupply();
    require(supply == maxSupply || block.timestamp >= headStart, "Can not withdraw yet.");
    
  }
}
function reveal() public onlyOwner() {
    revealed = true;
}

//only owner