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

  string public baseURI;
  string public baseExt = "";
  string public notRevealedUri;
  
  uint256 public cost = 0.02 ether;
  uint256 public maxSupply = 10000;
  uint256 public maxMintQty = 20;
  
  bool public paused = false;
  bool public revealed = false;

  mapping(address => bool) public whitelisted;
  mapping(uint256 => uint256) private tokenMatrix;

  constructor(
    string memory _name,
    string memory _symbol,
    string memory _initBaseURI,
    string memory _initNotRevealedUri
  ) ERC721(_name, _symbol) {
    setBaseURI(_initBaseURI);
    setNotRevealedURI(_initNotRevealedUri);
    mint(msg.sender, 1);
  }

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

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

    if (msg.sender != owner()) {
        if(whitelisted[msg.sender] != true) {
          require(msg.value >= cost * _mintAmount);
        }
    }

    for (uint256 i = 1; i <= _mintAmount; i++) {
        uint256 _upper = maxSupply - totalSupply();
        _safeMint(_to, _getNextToken(_upper, totalSupply()));
    }
    
  }

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

  //only owner
  function reveal() public onlyOwner {
      revealed = true;
  }
  
  function setCost(uint256 _newCost) public onlyOwner {
    cost = _newCost;
  }
  
  function setMaxSupply(uint256 _newMaxSupply) public onlyOwner {
    maxSupply = _newMaxSupply;
  }

  function setmaxMintQty(uint256 _newmaxMintQty) public onlyOwner {
    maxMintQty = _newmaxMintQty;
  }
  
  function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
    notRevealedUri = _notRevealedURI;
  }

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

  function setBaseExt(string memory _newBaseExt) public onlyOwner {
    baseExt = _newBaseExt;
  }

  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);
  }
  
  // random TokenId, code used as ref:
  // https://github.com/1001-digital/erc721-extensions/blob/main/contracts/RandomlyAssigned.sol
  function _getNextToken(uint256 _upper, uint256 _totalMintedTokens) private returns (uint256) {
      uint256 maxIndex = _upper - _totalMintedTokens;
          
      uint256 random = uint256(keccak256(
          abi.encodePacked(
              msg.sender,
              block.coinbase,
              block.difficulty,
              block.gaslimit,
              block.timestamp
          )
      )) % maxIndex;
          
      uint256 value = 0;
      if (tokenMatrix[random] == 0) {
          value = random;
      } else {
          value = tokenMatrix[random];
      }

      if (tokenMatrix[maxIndex - 1] == 0) {
          tokenMatrix[random] = maxIndex - 1;
      } else {
          tokenMatrix[random] = tokenMatrix[maxIndex - 1];
      }
          
      return value;
          
  }
  
}
function _baseURI() internal view virtual override returns (string memory) {
  return baseURI;
}

// internal