contract CFH is ERC721, ERC721Enumerable, Ownable {

    using Strings for uint256;

    mapping(uint256 => bool) private _isOccupiedId;
    uint256[] private _occupiedList;
    uint256 public  tokenPresalePriceInETH = 40000000000000000; // 0.04 ETH
    uint256 public tokenPublicsalePriceInETH = 60000000000000000; // 0.06 ETH
    uint256 public  tokenPresalePriceInMORE = 250000000000; // 2500 MORE
    uint256 public tokenPublicsalePriceInMORE = 500000000000; // 5000 MORE
    uint public constant maxTokenPerWallet = 5;
    uint256 public MAX_TOKENS = 1000;
    bool public saleIsActive = false;
    bool public preSaleIsActive = false;
    bool public revealed = false;
    string public notRevealedUri;
    string public baseExtension = ".json";
    string private _baseURIextended;
    address private _MORE;
    // WhiteLists for presale.
    mapping (address => bool) private _isWhiteListed;
    mapping (address => uint) private _numberOfWallets;
    
    event AddWhiteListWallet(address _wallet );
    event RemoveWhiteListWallet(address _wallet );
    event CreateCryptoCFH(address to, uint256 indexed id);
    constructor(
        address MORE
    ) ERC721("Crypto Food Hall", "CFH") {
        _MORE = MORE;
    }

    function reveal() public onlyOwner {
        revealed = true;
    }

    function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
        notRevealedUri = _notRevealedURI;
    }

    function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal override(ERC721, ERC721Enumerable) {
        super._beforeTokenTransfer(from, to, tokenId);
    }

    function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721, ERC721Enumerable) returns (bool) {
        return super.supportsInterface(interfaceId);
    }

    function setBaseURI(string memory baseURI_) external onlyOwner() {
        _baseURIextended = baseURI_;
    }

    function _baseURI() internal view virtual override returns (string memory) {
        return _baseURIextended;
    }

    function tokenURI(uint256 tokenId)
        public
        view
        virtual
        override
        returns (string memory)
    {
        require(
        _exists(tokenId),
        "ERC721Metadata: URI query for nonexistent token"
        );
        
        if(revealed == false) {
            return notRevealedUri;
        }

        string memory currentBaseURI = _baseURI();
        return bytes(currentBaseURI).length > 0
            ? string(abi.encodePacked(currentBaseURI, tokenId.toString(), baseExtension))
            : "";
    }

    function flipSaleState() public onlyOwner {
        saleIsActive = !saleIsActive;
    }

    function flipPreSaleState() public onlyOwner {
        preSaleIsActive = !preSaleIsActive;
    }

    function addWhiteListWallet (address _wallet) public onlyOwner {
        _isWhiteListed[_wallet] = true;
        emit AddWhiteListWallet(_wallet);
    }
    
    function removeWhiteListWallet (address _wallet) public onlyOwner {
        _isWhiteListed[_wallet] = false;
        emit RemoveWhiteListWallet(_wallet);
    }

    function checkSale (address sender, uint[] memory tokenIds) internal view {
        require(tokenIds.length <= maxTokenPerWallet, "Exceeded max token purchase");
        require(_numberOfWallets[sender] + tokenIds.length <= maxTokenPerWallet, "Exceeded max token purchase per wallet");
        require(totalSupply() + tokenIds.length <= MAX_TOKENS, "Purchase would exceed max supply of tokens");
        for (uint256 i = 0; i < tokenIds.length; i++) {
            require(_isOccupiedId[tokenIds[i]] == false, "MINT: Those ids already have been used for other customers");
        }
    }

    function mainMint (address sender, uint[] memory tokenIds) internal {
        for(uint i = 0; i < tokenIds.length; i++) {
            _mintAnElement(sender, tokenIds[i]);
        }
        
        _numberOfWallets[sender] = _numberOfWallets[sender] + tokenIds.length;
    }
    
    function preSaleTokenInETH(uint[] memory tokenIds) public payable {
        require(preSaleIsActive, "PreSale must be active to mint Tokens");
        require(!saleIsActive, "Could not pre-mint after sales active");
        require(_isWhiteListed[msg.sender], "You are not allowed to mint before crowd sale");
        checkSale(msg.sender, tokenIds);
        require(tokenPresalePriceInETH * tokenIds.length <= msg.value, "Ether value sent is not correct");
        mainMint(msg.sender, tokenIds);
    }

    function preSaleTokenInMore (uint[] memory tokenIds) public virtual {
        require(preSaleIsActive, "PreSale must be active to mint Tokens");
        require(!saleIsActive, "Could not pre-mint after sales active");
        require(_isWhiteListed[msg.sender], "You are not allowed to mint before crowd sale");
        checkSale(msg.sender, tokenIds);
        require(tokenPresalePriceInMORE * tokenIds.length <= IERC20(_MORE).balanceOf(msg.sender), "Have not enough MORE");
        IERC20(_MORE).transferFrom(msg.sender, address(this), tokenPresalePriceInMORE * tokenIds.length);
        mainMint(msg.sender, tokenIds);
    }
    
    function mintTokenInETH(uint[] memory tokenIds) public payable {
        require(saleIsActive, "Sale must be active to mint Tokens");
        checkSale(msg.sender, tokenIds);
        require(tokenPublicsalePriceInETH * tokenIds.length <= msg.value, "Ether value sent is not correct");
        mainMint(msg.sender, tokenIds);
    }

    function mintTokenInMORE(uint[] memory tokenIds) public virtual {
        require(saleIsActive, "Sale must be active to mint Tokens");
        checkSale(msg.sender, tokenIds);
        require(tokenPublicsalePriceInMORE * tokenIds.length <= IERC20(_MORE).balanceOf(msg.sender), "Have not enough MORE");
        IERC20(_MORE).transferFrom(msg.sender, address(this), tokenPublicsalePriceInMORE * tokenIds.length);
        mainMint(msg.sender, tokenIds);
    }

    function _mintAnElement(address _to, uint256 _id) private {
        _safeMint(_to, _id);
        _isOccupiedId[_id] = true;
        _occupiedList.push(_id);
        emit CreateCryptoCFH(_to, _id);
    }

    function occupiedList() public view returns (uint256[] memory) {
      return _occupiedList;
    }

    function changePresalePriceInETH ( uint _price ) public onlyOwner {
        tokenPresalePriceInETH = _price;
    }
    
    function changePublicsalePriceInETH ( uint _price ) public onlyOwner {
        tokenPublicsalePriceInETH = _price;
    }

    function changePresalePriceInMORE ( uint _price ) public onlyOwner {
        tokenPresalePriceInMORE = _price;
    }

    function changePublicsalePriceInMORE ( uint _price ) public onlyOwner {
        tokenPublicsalePriceInMORE = _price;
    }
    // Update dev address by the previous dev.

    function withdraw() public onlyOwner {
        uint256 balance = address(this).balance;
        uint256 tokenBalance = IERC20(_MORE).balanceOf(address(this));
        payable(msg.sender).transfer(balance);
        IERC20(_MORE).transfer(msg.sender, tokenBalance);
    }

}
function withdraw() public onlyOwner {
        uint256 balance = address(this).balance;
        uint256 tokenBalance = IERC20(_MORE).balanceOf(address(this));
        payable(msg.sender).transfer(balance);
        IERC20(_MORE).transfer(msg.sender, tokenBalance);
    }
// Update dev address by the previous dev.