contract GeoArt is ERC721Enumerable, Ownable {
    using Strings for uint256;
    
    string public baseURI;
    string public baseExtension = ".json";
    uint256 public cost = 0.09 ether;
    uint256 public maxSupply = 1080;
    uint256 public maxMintAmount = 20;
    bool public paused = false;
    bool public canUpgrade = false;
    // address public donationAddress = 0x97B7b5000968716280fF7829C8C83E602c8661f0;
    
    
    constructor(
        string memory _initBaseURI
    ) ERC721("GeoArt","GART"){
        setBaseURI(_initBaseURI);
        mint(msg.sender,10);
    }
    
    // internal
    function _baseURI() internal view virtual override returns (string memory){
        return baseURI;
    }

    function upgradeNFT(address _owner, uint256 _tokenId) public payable {
        require(canUpgrade, "GEOART: Upgrading NFTs is not available.");
        require(balanceOf(_owner) > 2, "GEOART: Minimum NFT balance to upgrade = 2.");
        require(ownerOf(_tokenId) == _owner, "GEOART: Cannot upgrade an NFT you do not own.");
        _burn(_tokenId);
        _safeMint(_owner, 1);

    }

  
    // public
    function mint(address _to, uint256 _mintAmount) public payable {
        uint256 supply = totalSupply();
        require(!paused, "GEOART: Contract paused.");
        require(_mintAmount > 0, "GEOART: Amount should be greater than 0.");
        require(_mintAmount <= maxMintAmount, "GEOART: Maximum minting amount = 20/transaction.");
        require(supply + _mintAmount <= maxSupply, "GEOART: No more NFTs to mint, check out OpenSea.");
        
        if(msg.sender != owner()){
            require(msg.value >= cost * _mintAmount, "GEOART: Insufficient funds.");
        }
        for(uint256 i = 1; i <= _mintAmount; i++){
                _safeMint(_to,supply + i);
            }
       
        
    }
    
    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),"GEOART: This NFT does not exist.");
        
        string memory currentBaseURI = _baseURI();
        return
            bytes(currentBaseURI).length > 0
                ? string(abi.encodePacked(currentBaseURI, tokenId.toString(), baseExtension))
                : "";
    }
    
    // Only owner
    function setCost(uint256 _newCost) public onlyOwner(){
        cost = _newCost;
    }
    
    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 pause(bool _state) public onlyOwner(){
        paused = _state;
    }
    
    function withdraw() public payable onlyOwner(){
        require(payable(msg.sender).send(address(this).balance));
    }
    
    function upgradeEnable(bool _state) public onlyOwner(){
        canUpgrade = _state;
    }
}
function _baseURI() internal view virtual override returns (string memory){
    return baseURI;
}

// internal