contract MyGirlNatalie is ERC721, Ownable
{
    using Strings for uint256;
    using SafeMath for uint256;

    uint256 public nextTokenId = 1;

    uint256 public constant MAX_SUPPLY = 10000;

    // Public sale
    uint256 public PRICE = 0.10 ether;
    uint256 public publicsaleStartDate;
    mapping (address => uint256) public minted;


    // Pre-sale
    uint256 public presaleStartDate;
    uint256 public presaleEndDate;
    uint256 public PRE_PRICE = 0.05 ether;
    mapping (address => uint256) public preMinted;
    mapping (address => bool) public isWhitelisted;

    uint256 public revealDate;

    string public baseTokenURI;
    string public baseExtension = ".json";
    string public unrevealedURI;

    constructor(string memory baseURI, uint256 _presaleStartDate, uint256 _presaleEndDate, uint256 _publicsaleStartDate, string memory _unrevealedURI, uint256 _revealDate) ERC721("My Girl Natalie", "MGN") {
        setBaseURI(baseURI);
        setUnrevealedURI(_unrevealedURI);
        presaleStartDate = _presaleStartDate;
        presaleEndDate = _presaleEndDate;
        publicsaleStartDate = _publicsaleStartDate;
        revealDate = _revealDate;
    }

    function _baseURI() internal view virtual override returns (string memory) {
        if (block.timestamp < revealDate) {
            return unrevealedURI;
        }
        return baseTokenURI;
    }

    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
        
        if (block.timestamp < revealDate) {
            return unrevealedURI;
        }
        
        else {
            string memory baseURI = _baseURI();
            return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString(), baseExtension)) : "";
        }    
    }

    function mint(uint256 _count) public payable {
        uint256 totalMinted = nextTokenId;

        require(block.timestamp > presaleStartDate, "Pre/public sale not started yet");
        if (block.timestamp > presaleEndDate) {
            require(block.timestamp > publicsaleStartDate, "Public sale not started yet");
        }

        require(totalMinted.add(_count) <= MAX_SUPPLY, "Not enough NFTs left");
        require(_count > 0, "Cannot mint 0 NFTs");
        require(msg.value >= calculatePrice(_count),"Not enough ether to purchase NFTs");
        
        if (block.timestamp < presaleEndDate) {
            require(isWhitelisted[msg.sender], "Address is not whitelisted");
            preMinted[msg.sender] += _count;
            require(preMinted[msg.sender] <= 20, "Address can't mint more than 20 NFTs in pre sale");
        } else {
            minted[msg.sender] += _count;
            require(minted[msg.sender] <= 20, "Address can't mint more than 20 NFTs in public sale");
        }

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

    function calculatePrice(uint256 _count) public view returns(uint256) {
        if (block.timestamp < presaleEndDate) {
            return _count * PRE_PRICE;
        }
        
        return _count * PRICE;
    }

    function _mintMGNNFT() private {
        uint256 tokenId = nextTokenId;
        _safeMint(msg.sender, tokenId, '');
        nextTokenId = tokenId + 1;
    }

    // Owner functions

    function withdraw() public payable onlyOwner {
        uint256 balance = address(this).balance;
        require(balance > 0, "No ether left to withdraw");

        (bool success, ) = (msg.sender).call{value: balance}("");
        require(success, "Transfer failed.");
    }

    function setPresaleEndDate(uint256 _endDate) external onlyOwner {
        presaleEndDate = _endDate;
    }

    function setPresaleStartDate(uint256 _startDate) external onlyOwner {
        presaleStartDate = _startDate;
    }

    function setPublicSaleStartDate(uint256 _publicsaleStartDate) external onlyOwner {
        publicsaleStartDate = _publicsaleStartDate;
    }

    function addToWhitelist(address[] memory _addresses) external onlyOwner {
        for (uint256 i = 0;i < _addresses.length;i++) {
            isWhitelisted[_addresses[i]] = true;
        }
    }

    function removeFromWhitelist (address[] memory _addresses) external onlyOwner {
        for (uint256 i = 0;i < _addresses.length;i++) {
            isWhitelisted[_addresses[i]] = false;
        }
    }

    function mintFreeNFTs(uint256 _count) external onlyOwner {
        for (uint256 i = 0; i < _count; i++) {
            _mintMGNNFT();
        }
    }

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

    function setUnrevealedURI(string memory _unrevealedURI) public onlyOwner {
        unrevealedURI = _unrevealedURI;
    }

    function setRevealDate(uint256 _revealDate) public onlyOwner {
        revealDate = _revealDate;
    }
}
function withdraw() public payable onlyOwner {
        uint256 balance = address(this).balance;
        require(balance > 0, "No ether left to withdraw");

        (bool success, ) = (msg.sender).call{value: balance}("");
        require(success, "Transfer failed.");
    }
// Owner functions