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

  string public baseURI;
  string public baseExtension = "";
  string public notRevealedUri;
  uint256 public presaleCost = 0.04 ether;
  uint256 public publicCost = 0.05 ether;
  uint256 public maxSupply = 5000;
  uint256 public maxMintAmount = 20;
  uint256 public nftPerAddressLimit = 200;
  bool public saleIsActive = true;
  bool public preSaleIsActive = true;
  bool public isRevealed = false;
  bool public onlyWhitelistedCanMint = true; //Only Whitelisted Can Mint Turn on for public sale.
  
  address[] public whitelistedAddresses;
 

  mapping(address => uint256) public addressMintedBalance;

  constructor(
    string memory _name,
    string memory _symbol,
    string memory _initBaseURI,
    string memory _initNotRevealedUri
  ) ERC721(_name, _symbol) {
    setBaseURI(_initBaseURI);
    setNotRevealedURI(_initNotRevealedUri);
  }

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

  // public MINT
  function mintSillyGoose(uint256 _mintAmount) public payable {
    require(saleIsActive, "Minting is currently pasued, please check our social media for updates.");
    uint256 supply = totalSupply();
    require(_mintAmount > 0, "need to mint at least 1 Silly Goose");
    require(_mintAmount <= maxMintAmount, "max mint amount per session exceeded");
    require(supply + _mintAmount <= maxSupply, "max Silly Goose mint exceeded supply");

    if (msg.sender != owner()) {
        if(onlyWhitelistedCanMint == true) {
            require(preSaleIsActive == true, "Pre Sale must be active to mint tokens");
            require(isWhitelisted(msg.sender), "You are not whitelisted");
            uint256 ownerMintedCount = addressMintedBalance[msg.sender];
            require(ownerMintedCount + _mintAmount <= nftPerAddressLimit, "You own to many Silly Goose to keep minting.");
            require(msg.value >= presaleCost * _mintAmount, "Not enough ether to buy a silly goose.");
        } else {
         require(msg.value >= publicCost * _mintAmount, "Not enough ether to buy a silly goose.");
        }
    }

    for (uint256 i = 1; i <= _mintAmount; i++) {
      addressMintedBalance[msg.sender]++;
      _safeMint(msg.sender, supply + i);
    }
  }
  
  //For Airdrop Purposes
  function airDrop(address[] calldata _addresses) public payable onlyOwner {
    uint256 supply = totalSupply();
    supply += 1;
    for (uint i=0; i<_addresses.length; i++) {
      _safeMint(_addresses[i], supply + i);
    }
  }
    
  //Check to see if user is whitelisted
  function isWhitelisted(address _user) public view returns (bool) {
    for (uint i = 0; i < whitelistedAddresses.length; i++) {
      if (whitelistedAddresses[i] == _user) {
          return true;
      }
    }
    return false;
  }

  //Get the tokens that exist for this wallet
  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"
    );
    
    if(isRevealed == false) {
        return notRevealedUri;
    }

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

  //Reveal Silly Goose NFTs
  function reveal() public onlyOwner() {
      isRevealed = true;
  }
  
  //Update NFTS per Address Limit
  function setNftPerAddressLimit(uint256 _limit) public onlyOwner() {
    nftPerAddressLimit = _limit;
  }
  
  //Set Public Sale Cost
  function setCost(uint256 _newCost) public onlyOwner() {
    publicCost = _newCost;
  }
  
  //Set Presale  Cost
  function setPresaleCost(uint256 _newCost) public onlyOwner() {
    presaleCost = _newCost;
  }

  //Set Max Mint Amount
  function setmaxMintAmount(uint256 _newmaxMintAmount) public onlyOwner() {
    maxMintAmount = _newmaxMintAmount;
  }

  //Set Base URI for a Silly Goose Object
  function setBaseURI(string memory _newBaseURI) public onlyOwner {
    baseURI = _newBaseURI;
  }

  //Update Base Extension if necessary
  function setBaseExtension(string memory _newBaseExtension) public onlyOwner {
    baseExtension = _newBaseExtension;
  }
  
  //Update Not Revealed URI
  function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
    notRevealedUri = _notRevealedURI;
  }
  
  //Pause Presale
  function pausePreSale() external onlyOwner {
    preSaleIsActive = false;
  }
  
  //Start Presale 
  function startPreSale() external onlyOwner {
    preSaleIsActive = true;
  }

  //Pause Sale
  function pauseSale() public onlyOwner {
    saleIsActive = false;
  }
  
  //Start Sale
  function startSale() public onlyOwner {
    saleIsActive = true;
  }
  
  //Only Whitelist Can Mint
  function setOnlyWhitelistedMint() public onlyOwner {
    onlyWhitelistedCanMint = true;
  }
  
  //Everyone Can Mint
  function everyoneCanMint() public onlyOwner {
    onlyWhitelistedCanMint = false;    
  }
  
  //Array of whitelisted users []
  function whitelistUsers(address[] calldata _users) public onlyOwner {
    delete whitelistedAddresses;
    whitelistedAddresses = _users;
  }
 
  //Withdraw balance from contract
  function withdraw() public payable onlyOwner {
    (bool success, ) = payable(msg.sender).call{value: address(this).balance}("");
    require(success, "SGCC: The transfer failed");
  }
}
function startSale() public onlyOwner {
    saleIsActive = true;
  }
//Start Sale