contract GamerMonkeys is ERC721, Ownable {
    using Counters for Counters.Counter;
    using Strings for uint256;

    Counters.Counter private tokenIdCounter;

    string baseURI;
    string public baseExtension = ".json";
    string public preRevealURI;
    uint256 public currentPrice = 0.025 ether;
    uint256 public totalGamerMonkeys = 5555;
    uint256 public maxFreeGamerMonkeys = 555;
    bool public paused = true;
    bool public revealed = false;

    constructor(string memory _initBaseURI, string memory _preRevealURI)
        ERC721("Gamer Monkeys", "GM")
    {
        setBaseURI(_initBaseURI);
        setPreRevealURI(_preRevealURI);
        
    }

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


    function freeGamerMonkey(uint256 mintAmount) public {
        uint256 supply = tokenIdCounter.current();
        require(!paused, "Sale paused");
        require(supply + mintAmount < maxFreeGamerMonkeys, "No more free Gamer Monkeys left");
        mintGamerMonkeys(msg.sender, mintAmount);
    }

    function mint(uint256 mintAmount) external payable {
        uint256 supply = tokenIdCounter.current();
        require(!paused, "Sale paused");
        require(mintAmount < 21, "You can only mint 20 Gamer Monkeys");
        require(
            supply + mintAmount < totalGamerMonkeys + 1,
            "Exceeds maximum Gamer Monkey supply"
        );
        require(
            msg.value >= currentPrice * mintAmount,
            "Not enough Ether sent"
        );

        mintGamerMonkeys(msg.sender, mintAmount);
    }

    function mintGamerMonkeys(address addr, uint256 mintAmount) private {
        for (uint256 i = 0; i < mintAmount; i++) {
            tokenIdCounter.increment();
            _safeMint(addr, tokenIdCounter.current());
        }
    }

    function getCurrentId() external view returns (uint256) {
        return tokenIdCounter.current();
    }


    function totalSupply() public view returns (uint256) {
        return tokenIdCounter.current();
    }
    


    function tokenURI(uint256 tokenId)
        public
        view
        virtual
        override
        returns (string memory)
    {
        require(
            _exists(tokenId),
            "ERC721Metadata: URI query for nonexistent token"
        );

        if (revealed == false) {
            return preRevealURI;
        }

        string memory currentBaseURI = _baseURI();
        return
            bytes(currentBaseURI).length > 0
                ? string(
                    abi.encodePacked(
                        currentBaseURI,
                        tokenId.toString(),
                        baseExtension
                    )
                )
                : "";
    }

    //----------------Owner functions--------------------
    function pause(bool val) public onlyOwner {
        paused = val;
    }

    //one way change, cannot be reverted
    function reveal() public onlyOwner {
        revealed = true;
    }

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

    function setPreRevealURI(string memory _newPreRevealURI) public onlyOwner {
        preRevealURI = _newPreRevealURI;
    }

    function withdraw() public onlyOwner {
        uint256 amount = address(this).balance;
        (bool success, ) = msg.sender.call{value: amount}("");
        require(success, "Failed to send Ether");
    }
}
function reveal() public onlyOwner {
        revealed = true;
    }
//one way change, cannot be reverted