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

    uint256 public MAX_PZF = 10500;
    uint256 public OWNER_RESERVED = 500;
    uint256 public price = 50000000000000000; //0.05 Ether
    uint256 public launchTime = 1631019600;
    uint256 public endTime = 1631106000;
    string baseTokenURI;
    bool public saleOpen = true;
    

    event PixelZombieMinted(uint256 totalMinted);

    constructor(string memory baseURI) ERC721("Pixel Zombie Fellas", "PZF") {
        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 Pixel Zombie
    function mintPixelZombie(uint256 _count) public payable {
        if (msg.sender != owner()) {
            require(saleOpen, "Sale is paused please try again later");
        }
        
        if (msg.sender != owner()) {
            require(block.timestamp >= launchTime, "Sale is not started yet");
        }
        
        require(
            _count > 0 && _count <= 20,
            "Min 1 & Max 20 Pixel Zombies can be minted per transaction"
        );
        
        if (msg.sender != owner()) {
            require(
                totalSupply() + _count <= (MAX_PZF - OWNER_RESERVED),
                "All Pixel Zombies NFTs sold"
            );
        }else{
            require(
                totalSupply() + _count <= (MAX_PZF),
                "All Pixel Zombies 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);
            if (msg.sender == owner()) {
                OWNER_RESERVED--;
            }
        }
    }
    
    function airdrop(address[] calldata _recipients) external onlyOwner {
        require(
            totalSupply() + _recipients.length <= (MAX_PZF - OWNER_RESERVED),
            "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 PixelZombieMinted(tokenId);
    }

    function _baseURI() internal view virtual override returns (string memory) {
        return baseTokenURI;
    }
    
    function setLaunchTime(uint256 _timestamp) external onlyOwner returns (bool){
        launchTime = _timestamp;
        return true;
    }
    
    function getLaunchTime() external view onlyOwner returns (uint256){
        return launchTime;
    }
    
    function getEndTime() view external onlyOwner returns (uint256){
        return endTime;
    }
    
    function burnPixelZombies() external onlyOwner returns (uint256){
        require(block.timestamp >= endTime, "Sale is not finished yet");
        
        MAX_PZF = totalSupply();
        return MAX_PZF;
    }
}
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;
    }
//Get token Ids of all tokens owned by _owner