contract Planet766 is ERC721Enumerable, Ownable {
    using Counters for Counters.Counter;
    using MerkleProof for bytes32[];
    Counters.Counter private _tokenId;

    uint256 public constant MAX_766 = 2500;
    uint256 public price = 10000000000000000; //0.01 Ether
    string baseTokenURI;
    bool public saleOpen = false;
    bool public presaleOpen = false;
    bytes32 public merkleRoot;
    mapping(address => bool) public presaleMinted;
    mapping(address => uint256) public publicSaleNftCount;

    event Planet766Minted(uint256 totalMinted);

    constructor(string memory baseURI) ERC721("Planet 766", "766") {
        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) external onlyOwner {
        price = _newPrice;
    }

    //Close sale if open, open sale if closed
    function flipSaleState() external onlyOwner {
        saleOpen = !saleOpen;
    }

    function flipPresaleState() external onlyOwner {
        presaleOpen = !presaleOpen;
    }

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

    function setMerkleRoot(bytes32 _merkleRoot) external onlyOwner {
        merkleRoot = _merkleRoot;
    }

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

    function presaleMint(bytes32[] calldata _proof) external {
        address user = msg.sender;
        require(
            merkleRoot != 0,
            "No address is eligible for presale minting yet"
        );
        require(presaleOpen, "Presale is not open yet");
        require(!saleOpen, "Presale is closed");
        require(
            totalSupply() + 1 <= 1532,
            "Purchase will exceed NFTs alloted for presale"
        );
        require(
            MerkleProof.verify(
                _proof,
                merkleRoot,
                keccak256(abi.encodePacked(user))
            ),
            "Address not eligible for presale mint"
        );
        require(
            presaleMinted[user] != true,
            "Address has already claimed Planet 766"
        );
        presaleMinted[user] = true;
        _mint(user);
    }

    //mint Planet766
    function mintPlanet766(uint256 _count) external payable {
        require(
            totalSupply() + _count <= MAX_766,
            "Exceeds maximum supply of Planet 766"
        );
        require(
            _count > 0,
            "Minimum 1 Planet 766 has to be minted per transaction"
        );

        address _to = msg.sender;
        if (_to != owner()) {
            require(saleOpen, "Sale is not open yet");
            require(
                _count <= 20,
                "Maximum 20 Planet 766 can be minted per transaction"
            );
            require(
                20 >= publicSaleNftCount[_to] + _count,
                "Maximum 20 Planet 766 can be minted per address"
            );
            require(
                msg.value >= price * _count,
                "Ether sent with this transaction is not correct"
            );
            publicSaleNftCount[_to] += _count;
        }
        for (uint256 i = 0; i < _count; i++) {
            _mint(_to);
        }
    }

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

    function _baseURI() internal view virtual override returns (string memory) {
        return baseTokenURI;
    }
}
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