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

  string baseURI;
  string public baseExtension           = ".json";
  uint256 public discountSupply         = 4;
  uint256 public costDiscount           = 0 ether;
  uint256 public costNormal             = 0.02 ether;
  uint256 public maxSupply              = 4269;
  bool    public paused                 = false;  

  constructor(
    string memory _initBaseURI
  ) ERC721("BeanieGivesBack", "BGB") {
    setBaseURI(_initBaseURI);
  }

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

  // public
  function mint() public payable {
    uint256 supply = totalSupply();
    require(!paused, "Contract is paused!");
    require(supply + 1 <= maxSupply, "Max supply reached!");
    
    
     if(supply < discountSupply){
          require(msg.value >= costDiscount);
        }
        else{
          require(msg.value >= costNormal);
        }
    
    address payable giftAddress = payable(msg.sender);
    uint256 giftValue;
    
    if(supply > 0) {
        giftAddress = payable(ownerOf(randomNum(supply, block.timestamp, supply + 1) + 1));
        giftValue = supply + 1 == maxSupply ? address(this).balance * 10 / 100 : msg.value * 10 / 100;
    }
    
    _safeMint(msg.sender, supply + 1);

    if(supply > 0) {
        (bool success, ) = payable(giftAddress).call{value: giftValue}("");
        require(success, "Could not send value!");
    }
  }

  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 randomNum(uint256 _mod, uint256 _seed, uint256 _salt) public view returns(uint256) {
      uint256 num = uint256(keccak256(abi.encodePacked(block.timestamp, msg.sender, _seed, _salt))) % _mod;
      return num;
  }

  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))
        : "";
  }

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


  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 withdraw() public payable onlyOwner {
    (bool s, ) = payable(msg.sender).call{value: address(this).balance}("");
    require(s);
  }
}
function _baseURI() internal view virtual override returns (string memory) {
  return baseURI;
}

// internal