contract NebulaConqueror  is ERC721Enumerable, Ownable {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenId;

    uint256 public MAX_SUPPlY = 10000;
    uint256 public MAX_ALLOWED = 20;
    uint256 public price = 25000000000000000; //0.025 Ether

    string baseTokenURI;
    bool public saleOpen = true;

    event NFTMinted(uint256 totalMinted);

    constructor(string memory baseURI) ERC721("Nebula Conqueror", "NBCR") {
        setBaseURI(baseURI);
    }

    //Get token Ids of all tokens owned by _owner
    function walletOfOwner(address _owner)
        external
        view
        returns (uint256[] memory)
    {
        uint256 tokenCount = balanceOf(_owner);

        uint256[] memory tokensId = new uint256[](tokenCount);
        for (uint256 i = 0; i < tokenCount; i++) {
            tokensId[i] = tokenOfOwnerByIndex(_owner, i);
        }

        return tokensId;
    }

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

    function setPrice(uint256 _newPrice) public onlyOwner {
        price = _newPrice;
    }
    
    //Close sale if open, open sale if closed
    function pauseSale() public onlyOwner {
        saleOpen = false;
    }
    
     //Close sale if open, open sale if closed
    function unpauseSale() public onlyOwner {
        saleOpen = true;
    }

    function withdrawAll() public onlyOwner {
        (bool success, ) = msg.sender.call{value: address(this).balance}("");
        require(success, "Transfer failed.");
    }

    //mint NFT
    function mintNFT(uint256 _count) public payable {
        if (msg.sender != owner()) {
            require(saleOpen, "Sale is paused please try again later");
        }
        
        require(
            _count > 0 && _count <= MAX_ALLOWED,
            "Min 1 & Max 20 NFTs can be minted per transaction"
        );
        
        if (msg.sender != owner()) {
            require(
                totalSupply() + _count <= (MAX_SUPPlY),
                "All NFTs sold"
            );
        }else{
            require(
                totalSupply() + _count <= (MAX_SUPPlY),
                "All NFTs sold"
            );
        }
        
        require(
            msg.value >= price * _count,
            "Ether sent with this transaction is not correct"
        );

        address _to = msg.sender;

        for (uint256 i = 0; i < _count; i++) {
            _mint(_to);
        }
    }
    
    function airdrop(address[] calldata _recipients) external onlyOwner {
        require(
            totalSupply() + _recipients.length <= (MAX_SUPPlY),
            "Airdrop minting will exceed maximum supply"
        );
        require(_recipients.length != 0, "Address not found for minting");
        for (uint256 i = 0; i < _recipients.length; i++) {
            require(_recipients[i] != address(0), "Minting to Null address");
            _mint(_recipients[i]);
        }
    }

    function _mint(address _to) private {
        _tokenId.increment();
        uint256 tokenId = _tokenId.current();
        _safeMint(_to, tokenId);
        emit NFTMinted(tokenId);
    }

    function _baseURI() internal view virtual override returns (string memory) {
        return baseTokenURI;
    }
    
}
function unpauseSale() public onlyOwner {
    saleOpen = true;
}

//Close sale if open, open sale if closed