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

  string public baseURI;
  string public baseExtension = ".json";

  uint256 public maxSupply = 8888;
  uint256 private _reserved = 138;
  uint256 private _mintPrice = 0.08 ether;

  uint256 public maxOGMintAmount = 3;
  uint256 public mintPerOGWallet = 3;
  uint256 public maxMintAmount = 2;
  uint256 public mintPerWallet = 2;

  bool public paused = true;
  bool public revealed = false;
  bool public onlyWhitelisted = true;

  mapping(address => bool) private oglist;
  mapping(address => bool) private whitelist;
  mapping(address => uint256) private walletCount;

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

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

  // public
  function mint(uint256 _mintAmount) public payable nonReentrant {
    require(!paused, "Minting is not live");
    uint256 supply = totalSupply();

    require(_mintAmount > 0, "Minimum mint amount is 1");

    if (onlyWhitelisted) {
      require(oglist[msg.sender] || whitelist[msg.sender], "Address is not whitelisted");
      if (oglist[msg.sender]) {
        require(_mintAmount <= maxOGMintAmount, "Max mint for wallet is 3 GCC");
        require(
          walletCount[msg.sender] + _mintAmount <= maxOGMintAmount, 
          "Max mint for wallet is 3 GCC"
        );  
      } else {
        require(_mintAmount <= maxMintAmount, "Max mint for wallet is 2 GCC");
        require(
          walletCount[msg.sender] + _mintAmount <= maxMintAmount, 
          "Max mint for wallet is 2 GCC"
        );
      }
    } else {
      require(_mintAmount <= maxMintAmount, "Max mint for wallet is 2 GCC");
      require(
          walletCount[msg.sender] + _mintAmount <= maxMintAmount, 
          "Max mint for wallet is 2 GCC"
        );
    }

    require(supply + _mintAmount <= maxSupply - _reserved, "Exceeds max supply");

    if (msg.sender != owner()) {
        require(msg.value >= _mintPrice * _mintAmount, "Ether sent is not correct");
    }

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

    walletCount[msg.sender] += _mintAmount;
  }

  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
    override(ERC721)
    returns (string memory)
  {
    require(
      _exists(tokenId),
      "ERC721Metadata: URI query for nonexistent token"
    );

    if (revealed == false) {
        return _baseURI();
    }
    
    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 setMintPerOGWallet(uint256 _newLimit) public onlyOwner {
    mintPerOGWallet = _newLimit;
  }

  function setMintPerWallet(uint256 _newLimit) public onlyOwner {
    mintPerWallet = _newLimit;
  }

  function setmaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner {
    maxMintAmount = _newmaxMintAmount;
  }

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

  function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
    baseExtension = _newBaseExtension;
  }

  function toggleWhitelist(bool _state) public onlyOwner {
    onlyWhitelisted = _state;
  }

  function pause(bool _state) public onlyOwner {
    paused = _state;
  }
 
  function ogAddresses(address[] memory _addresses) public onlyOwner {
    for (uint256 i; i < _addresses.length; i++) {
      oglist[_addresses[i]] = true;
    }
  }

  function whitelistAddresses(address[] memory _addresses) public onlyOwner {
    for (uint256 i; i < _addresses.length; i++) {
      whitelist[_addresses[i]] = true;
    }
  }

  function gccMint(uint256 _mintAmount) public onlyOwner {
    require(_mintAmount <= _reserved, "Exceeds reserved supply");

    uint256 supply = totalSupply();

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

    _reserved -= _mintAmount;
  }

  function withdraw() public onlyOwner {
    require(
      payable(owner()).send(address(this).balance),
      "Withdraw failed"
    );
  }
}
function _baseURI() internal view virtual override returns (string memory) {
  return baseURI;
}

// internal