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

    Counters.Counter private _tokenSupply;

    uint256 public constant MAX_SUPPLY = 3333;
    string baseURI;
    string public baseExtension = ".json";
    bool public paused = true;
    bool public staffPurchaced = false;

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

    //PUBLIC
    // This is the new mint function leveraging the counter library
    function mint(uint256 _mintAmount) public payable {

        uint256 mintIndex = _tokenSupply.current() + 1; // Start IDs at 1

        require(!paused, "Melties are paused!");
        require(_mintAmount > 0, "Cant order negative number");
        require(mintIndex + _mintAmount <= MAX_SUPPLY, "This order would exceed the max supply");

        require(_mintAmount <= maxMintAmount(mintIndex), "This order exceeds max mint amount for the current stage");
        require(msg.value >= price(mintIndex) * _mintAmount, "This order doesn't meet the price requirement for the current stage");


        for (uint256 i = 0; i < _mintAmount; i++){
            _safeMint(msg.sender, mintIndex + i);
            _tokenSupply.increment();
        }
    }

    //Staff access 10 (must get before sold out)
    function adminMint() public onlyOwner {
        require(!staffPurchaced, "Staff order has already been fuffiled"); 
        require(_tokenSupply.current() + 10 <= MAX_SUPPLY, "Exceeds max supply");

        uint256 mintIndex = _tokenSupply.current() + 1;
        for (uint256 i = 0; i < 10; i++) { 
            
            _safeMint(msg.sender, mintIndex + i);
            _tokenSupply.increment();
        }
    }


    // How many left
    function remainingSupply() public view returns (uint256) {
        return MAX_SUPPLY - _tokenSupply.current();
    }

    // How many minted
    function tokenSupply() public view returns (uint256) {
        return _tokenSupply.current();
    }

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

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

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

    function price(uint256 _supply) internal pure returns (uint256 _cost){
      if(_supply < 333){
        return 0 ether;
      }else{
          return 0.02 ether;
      }
    } 

    function maxMintAmount(uint256 _supply) internal pure returns (uint256 _maxMintAmount){
      if(_supply < 333){
          return 3;
      }else{
        return 10;
      }
    }

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

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

    function withdraw() public payable onlyOwner {
    (bool os, ) = payable(owner()).call{value: address(this).balance}("");
    require(os);
  }
}
function tokenSupply() public view returns (uint256) {
    return _tokenSupply.current();
}

// How many minted