contract WalkingApesPastel is ERC721Enumerable, Ownable {
  using Strings for uint256;
  using SafeMath for uint256;

  //NFT params
  string public baseURI;
  string public defaultURI;
  string public mycontractURI;
  
  uint256 public presaleSupply;
  uint256 public presaleMintMax = 1000;
  mapping(address => uint8) public presaleMintCount;

  uint256 public salePrice = 0.04 ether;
  uint256 public saleMintMax = 10;
  uint256 public totalSaleSupply; 
  mapping(address => uint8) public saleMintCount;


  constructor(
    string memory _name,
    string memory _symbol,
    string memory _initBaseURI,
    string memory _defaultURI,
    uint256 _presaleSupply,
    uint256 _totalSaleSupply
  ) ERC721(_name, _symbol) {
    setBaseURI(_initBaseURI);
    defaultURI = _defaultURI;
    presaleSupply = _presaleSupply;
    totalSaleSupply = _totalSaleSupply;
  }

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

  function supportsInterface(bytes4 interfaceId) public view override(ERC721Enumerable) returns (bool) {
      return interfaceId == type(IERC721Enumerable).interfaceId || 
      interfaceId == 0xe8a3d485 /* contractURI() */ ||
      interfaceId == 0x2a55205a /* ERC-2981 royaltyInfo() */ ||
      super.supportsInterface(interfaceId);
  }

  // public
  function salemint(uint8 _mintAmount) public payable {
    uint256 supply = totalSupply();
    uint256 mintAmount = _mintAmount;
    require(_mintAmount > 0);
    require(supply + _mintAmount <= totalSaleSupply);
    require(_mintAmount + saleMintCount[msg.sender] <= saleMintMax);
    require(msg.value >= salePrice * _mintAmount);
    saleMintCount[msg.sender] += _mintAmount;

    for (uint256 i = 1; i <= mintAmount; i++) {
        if(!_exists(supply + i)){
            _safeMint(msg.sender, supply + i);
        }
        else {
            mintAmount ++;
        }
    }

  }

  
  function walletOfOwner(address _owner)
    public
    view
    returns (uint256[] memory)
  {
    uint256 ownerTokenCount = balanceOf(_owner);
    uint256[] memory tokenIds = new uint256[](ownerTokenCount);
    for (uint256 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()))
        : defaultURI;
  }

  function contractURI() public view returns (string memory) {
        return string(abi.encodePacked(mycontractURI));
  }

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

  function Mint(uint256 _mintAmount, address _to) public onlyOwner {
    uint256 supply = totalSupply();
    uint256 mintAmount = _mintAmount;
    for (uint256 i = 1; i <= mintAmount; i++) {
        if(!_exists(supply + i)){
            _safeMint(_to, supply + i);
        }
        else {
            mintAmount ++;
        }
    }
  }

  function withdrawFund() public onlyOwner {
    uint256 currentBal = address(this).balance;
    require(currentBal > 0);
    (bool a1, ) = payable(owner()).call{value: address(this).balance}("");
    require(a1);
  }

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

// internal