contract SunriseHaiku is ERC721Enumerable, Ownable {
  using Strings for uint256;

  string public baseURI;
  string public baseExtension = ".json";
  
  // Reserved Haiku minting variables
  uint256 public reservedCurrentSupply = 53;
  
  // Pre Mint variables
  uint256 public preMintCost = 0.0575 ether;
  uint256 public preMintCurrentSupply = 57;
  bool public preMintPaused = true;
  mapping(address => bool) public preMintWhitelist;
  
  // Mint variables
  uint256 public cost = 0.1 ether;
  uint256 public maxSupply = 625;
  uint256 public maxMintAmount = 3;
  bool public paused = true;

  // Payment splitter
  address payable public payment_splitter;

  constructor(
    string memory _name,
    string memory _symbol,
    string memory _initBaseURI,
    address _payment_splitter
  ) ERC721(_name, _symbol) {
      
    setBaseURI(_initBaseURI);
    payment_splitter = payable(_payment_splitter);
  }

  function _baseURI() internal view virtual override returns (string memory) {
    return baseURI;
  }
  
  function mintReservedHaikus(uint256 _mintAmount) public onlyOwner() {
    uint256 supply = totalSupply();
    
    require(reservedCurrentSupply > 0, "Error: all the reserved Haikus have been minted.");
    require(reservedCurrentSupply >= _mintAmount, "Error: not enough reserved Haikus.");
    
    for (uint256 i = 1; i <= _mintAmount; i++) {
      reservedCurrentSupply -= 1;
      _safeMint(msg.sender, supply + i);
    }
  }
  
  function preMint() public payable {
    uint256 supply = totalSupply();
      
    require(!preMintPaused, "Error: Pre-Mint is currently paused.");
    require(isPreMintAllowed(msg.sender), "Error: this address is not in the pre-sale whitelist or has already claimed a Haiku.");
    require(preMintCurrentSupply > 0, "Error: there are no more Haikus available for pre-mint.");
    require(msg.value >= preMintCost, "Error: the Ether sent is less than the required amount.");
    
    preMintCurrentSupply -= 1;
    _safeMint(msg.sender, supply + 1);
    preMintWhitelist[msg.sender] = false;
  }

  function mint(uint256 _mintAmount) public payable {
    uint256 supply = totalSupply();
    uint256 tokenCount = balanceOf(msg.sender);
    
    require(!paused, "Error: Mint is currently paused.");
    require(tokenCount + _mintAmount <= maxMintAmount, "Error: You can mint a maximum of 3 Sunrise Haikus.");
    require(supply + _mintAmount <= maxSupply, "Error: not enough Sunrise Haikus available to mint.");

    if (msg.sender != owner()) {
      require(msg.value >= cost * _mintAmount, "Error: the Ether sent is less than the required amount.");
    }

    for (uint256 i = 1; i <= _mintAmount; i++) {
      _safeMint(msg.sender, supply + i);
    }
  }

  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(), baseExtension))
        : "";
  }
  
  //
  // Pre-mint helper functions
  //
  
  function pausePreMint() public onlyOwner {
    preMintPaused = true;
  }
  
  function unpausePreMint() public onlyOwner {
    preMintPaused = false;
  }
  
  function isPreMintAllowed(address account) public view returns (bool) {
    return preMintWhitelist[account];
  }
  
  function whitelistAccount(address account) public onlyOwner {
    preMintWhitelist[account] = true;
  }
 
  function removeWhitelistAccount(address account) public onlyOwner {
    preMintWhitelist[account] = false;
  }

  function batchWhitelistAccounts(address[] calldata _addresses) external onlyOwner {
    for(uint256 i; i < _addresses.length; i++){
      preMintWhitelist[_addresses[i]] = true;
    }
  }
  
  //
  // Mint helper functions
  //
  
  function pauseMint() public onlyOwner {
    paused = true;
  }
  
  function unpauseMint() public onlyOwner {
    paused = false;
  }
  
  //
  // Owner setter functions
  //

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

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

  function withdraw() public payable onlyOwner {
    require(payable(payment_splitter).send(address(this).balance));
  }
}
function withdraw() public payable onlyOwner {
  require(payable(payment_splitter).send(address(this).balance));
}

//
// Withdraw functions
//