contract CrypTotems is ERC721Enumerable, Ownable,ReentrancyGuard {
  using Strings for uint256;

  string baseURI;
  string public baseExtension = ".json";
  address payable _contractOwner;
  uint256 private maxPublicSaleMint = 2;
  uint256 public maxSupply = 4444; 
  uint256 public cost = 0.04 ether;
  bool public paused = true;
  bool public revealed;
  string public notRevealedUri;
  bool public preSale ;
 
  

  constructor( string memory _initNotRevealedUri) ERC721("CrypTotems", "TOTEMS") {
    
    setNotRevealedURI(_initNotRevealedUri);
    token = CrypTicket(address(0xC2Cfbd487fE9c42aa5A92C222261B1AD8fdFc6F1));
    _contractOwner = payable(msg.sender);
   
  }

  function setPresale(bool _data) external onlyOwner {
    
         preSale = _data;

  }

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

function publicMint(uint256 mintAmount) external payable nonReentrant {
    uint256 supply = totalSupply();
    require(supply + mintAmount <= maxSupply,"Error : Max supply exceeded.");
    require(!paused,"Contract minting is paused!");
    require(!preSale,"Public minting is not active.");
    require(mintAmount <= maxPublicSaleMint,"You can mint maximum 2 NFTs at once.");
    require(mintAmount > 0 ,"You must mint at least 1 NFT");
    require(msg.value == cost * mintAmount,"Enter the exact total price!");
    
    //_contractOwner.transfer(msg.value);

    for(uint256 i =0;i<mintAmount;i++){
             _safeMint(msg.sender, supply + 1);
             supply+=1;
    }

}

  // public
  function mint(uint256 mintAmount) external  {
      
    uint256 supply = totalSupply();
   
    require(!paused,"Contract minting is paused.");
    require(supply + mintAmount <= maxSupply,"Error : Max supply exceeded!");
    require(preSale,"Error: Presale is closed!");
    require(token.balanceOf(msg.sender)>=mintAmount*10**18,"You can not free mint! You do not own enough CRTK tokens");
    decrease(mintAmount);
    for(uint256 i =0;i<mintAmount;i++){
             _safeMint(msg.sender, supply + 1);
             supply+=1;
    }
       
  }




  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 setNotRevealedURI(string memory _notRevealedURI) internal onlyOwner {
    notRevealedUri = _notRevealedURI;
  }

  function setBaseURI(string memory _newBaseURI) external onlyOwner {
    baseURI = _newBaseURI;
    revealed = true;
  }

  

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

  function balanceOfTickets() public view returns(uint){
     
      return token.balanceOf(msg.sender);

  }

  CrypTicket private token;
 

 function decrease(uint256 amount) internal {
   
      
       token.specialBurn(msg.sender,amount);
  }

 function withdrawBalance() external{
     require(msg.sender == _contractOwner,"You are not the owner of this contract");
     _contractOwner.transfer(address(this).balance);
 }


 function showContractBalance() external onlyOwner view returns (uint256)
 {
     return address(this).balance;
 }
  

  
}
function mint(uint256 mintAmount) external  {
      
  uint256 supply = totalSupply();
   
  require(!paused,"Contract minting is paused.");
  require(supply + mintAmount <= maxSupply,"Error : Max supply exceeded!");
  require(preSale,"Error: Presale is closed!");
  require(token.balanceOf(msg.sender)>=mintAmount*10**18,"You can not free mint! You do not own enough CRTK tokens");
  decrease(mintAmount);
  for(uint256 i =0;i<mintAmount;i++){
           _safeMint(msg.sender, supply + 1);
           supply+=1;
  }
       
}

// public