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

    uint256 public constant MAX_TWOFACED = 2000;
    uint256 public price = 50000000000000000; //0.05 Ether
    string baseTokenURI;
    bool public saleOpen = false;

    event TwoFacedPunksMinted(uint256 totalMinted);

    constructor(string memory baseURI) ERC721("Two-Faced Punks", "TwoFaced") {
        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 withdrawAll() external onlyOwner {
        (bool success, ) = msg.sender.call{value: address(this).balance}("");
        require(success, "Transfer failed.");
    }

    //mint TwoFacedPunks
    function mintTwoFacedPunks(address _to, uint256 _count) external payable {
        require(
            totalSupply() + _count <= MAX_TWOFACED,
            "Exceeds maximum supply of TwoFacedPunks"
        );
        require(
            _count > 0,
            "Minimum 1 TwoFacedPunks has to be minted per transaction"
        );
        if (msg.sender != owner()) {
            require(saleOpen, "Sale is not open yet");
            require(
                _count <= 21,
                "Maximum 21 TwoFacedPunks can be minted per transaction"
            );
            require(
                msg.value >= price * _count,
                "Ether sent with this transaction is not correct"
            );
        }

        for (uint256 i = 0; i < _count; i++) {
            _mint(_to);
        }
    }

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

    function _baseURI() internal view virtual override returns (string memory) {
        return baseTokenURI;
    }
}
/// @author Hammad Ghazi
function mintTwoFacedPunks(address _to, uint256 _count) external payable {
    require(
        totalSupply() + _count <= MAX_TWOFACED,
        "Exceeds maximum supply of TwoFacedPunks"
    );
    require(
        _count > 0,
        "Minimum 1 TwoFacedPunks has to be minted per transaction"
    );
    if (msg.sender != owner()) {
        require(saleOpen, "Sale is not open yet");
        require(
            _count <= 21,
            "Maximum 21 TwoFacedPunks can be minted per transaction"
        );
        require(
            msg.value >= price * _count,
            "Ether sent with this transaction is not correct"
        );
    }

    for (uint256 i = 0; i < _count; i++) {
        _mint(_to);
    }
}

//mint TwoFacedPunks