contract Magic_doodlefren_yellow is ERC721Enumerable, Ownable, ReentrancyGuard {
  using Strings for uint256;
  string public baseURI;
  bool public paused = true;
  string public baseExtension = ".json";
  uint256 public cost = 0.01 ether;  //cost of newNFT
  uint256 public maxSupply = 10000;  //supply of newNFT
  uint256 public maxMintAmount = 20;  ///newNFT
  bool public allowBuyAll = true;
  address public constant mm_addr = 0xff1b8c3d003ba22557196FFfA0e625b493cF670f;
  mm public mm_object = mm(mm_addr);
  mapping(uint256 => bool) public isDrank;
  uint256[] private arr;

  constructor(
    string memory _name,
    string memory _symbol,
    string memory _initBaseURI
  ) ERC721(_name, _symbol) {
    setBaseURI(_initBaseURI);
  }
  // internal
  function _baseURI() internal view virtual override returns (string memory) {
    return baseURI;
  }

  // public

  function mintYellowMagicDoodleFrenFromMilk(address _to, uint256 milkID) public payable nonReentrant {
    require(!paused);
    require(msg.value >= cost);
    require(totalSupply() + 1 <= maxSupply);
    require(mm_object.ownerOf(milkID)==msg.sender, "You do not own milk");
    mm_object.z_drinkMilk_do_not_use(milkID);//milkID);
    _safeMint(_to, totalSupply() + 1);
    isDrank[milkID] = true;
  }

  function mintAllYellowMagicDoodleFrenFromMilk(address _to) public payable nonReentrant {
    require(allowBuyAll);
    delete arr;
    require(!paused);
    require(mm_object.balanceOf(msg.sender)!=0);
    uint256[] memory wallet = mm_object.walletOfOwner(msg.sender);
    uint256 i; 
    uint256 supply;
    uint256 price;

    for (i=0; i < wallet.length; i++) {
      supply = totalSupply();
      if(mm_object.ownerOf(wallet[i])!=msg.sender) {
        continue;}
      if(isDrank[wallet[i]]==true) {
        continue;}
      if(supply + 1 > maxSupply) {
        continue;}
      arr.push(wallet[i]);
    }

    price = cost * arr.length;
    require(msg.value >= price);
    supply = totalSupply();
    for(i=0; i<arr.length; i++) {
      mm_object.z_drinkMilk_do_not_use(arr[i]);//milkID);
      _safeMint(_to, supply + 1+i);
      isDrank[arr[i]] = true;
    }
  }

  function walletOfOwner(address _owner)
    public
    view
    returns (uint256[] memory)
  {
    uint256 ownerTokenCount = balanceOf(_owner);
    uint256[] memory tokenIds = new uint256[](ownerTokenCount);
    uint256 i = 0;
    for (i; i < ownerTokenCount; i++) {
      tokenIds[i] = tokenOfOwnerByIndex(_owner, i);
    }
    return tokenIds;
  }

  function tokenURI(uint256 tokenId)
    public
    view
    virtual
    override
    returns (string memory)
  {
    require(
      _exists(tokenId),
      "ERC721Metadata: URI query for nonexistent token"
    );

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

  function getSupply() public view returns (uint256) {
      return totalSupply();
  }

  //only owner
  function setCost(uint256 _newCost) public onlyOwner {
    cost = _newCost;
  }

  function setmaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner {
    maxMintAmount = _newmaxMintAmount;
  }

  function setBaseURI(string memory _newBaseURI) public onlyOwner {
    baseURI = _newBaseURI;
  }

  function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
    baseExtension = _newBaseExtension;
  }

  function pause(bool _state) public onlyOwner {
    paused = _state;
  }

  function setAllowBuyAll(bool _state) public onlyOwner {
    allowBuyAll = _state;
  }

  function withdraw() public payable onlyOwner {
    
    (bool os, ) = payable(owner()).call{value: address(this).balance}("");
    require(os);
    
  }
}
function setCost(uint256 _newCost) public onlyOwner {
  cost = _newCost;
}

//only owner