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

    uint256 public MAX_SUPPLY = 3333;
    uint256 public MAX_ALLOWED = 5;
    uint256 public OWNER_RESERVED = 333;
    uint256 public price = 50000000000000000; //0.05 Ether
    string baseTokenURI;
    
    bool public saleOpen = true;
    
    event NFTMinted(uint256 totalMinted);

    constructor(string memory baseURI) ERC721("SHIBOKI", "SHBK") {
        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;
    }

    function setMaxAllowd(uint256 _maxAllowed) public onlyOwner {
        MAX_ALLOWED = _maxAllowed;
    }
    
    function setOwnerReserved(uint256 _ownerReserved) public onlyOwner {
        OWNER_RESERVED = _ownerReserved;
    }
    
    function getPrice() external view returns(uint256){
        return price;
    }
    
    //Close sale
    function pauseSale() public onlyOwner {
        saleOpen = false;
    }
    
     //Open sale
    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 == true), "Sale is not open please try again later");
        }
        
        require(
            _count > 0 && _count <= MAX_ALLOWED,
           "You have reached the NFT minting limit per transaction"
        );
        
        require(balanceOf(msg.sender) < MAX_ALLOWED, "You have reached maximum NFT minting limit per account");
        
        if (msg.sender != owner()) {
            require(
                totalSupply() + _count <= (MAX_SUPPLY - OWNER_RESERVED),
                "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);
            if (msg.sender == owner()) {
                if(OWNER_RESERVED > 0){
                    OWNER_RESERVED--;
                }
            }
        }
    }
    
    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 mintNFT(uint256 _count) public payable {
    if (msg.sender != owner()) {
        require((saleOpen == true), "Sale is not open please try again later");
    }
        
    require(
        _count > 0 && _count <= MAX_ALLOWED,
       "You have reached the NFT minting limit per transaction"
    );
        
    require(balanceOf(msg.sender) < MAX_ALLOWED, "You have reached maximum NFT minting limit per account");
        
    if (msg.sender != owner()) {
        require(
            totalSupply() + _count <= (MAX_SUPPLY - OWNER_RESERVED),
            "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);
        if (msg.sender == owner()) {
            if(OWNER_RESERVED > 0){
                OWNER_RESERVED--;
            }
        }
    }
}

//mint NFT