contract LongLost is ERC721, Ownable {

    using Strings for uint256;
    using Counters for Counters.Counter;

    Counters.Counter private supply;

    string public uriPrefix = "";
    string public uriSuffix = ".json";
    string public hiddenMetadataUri;

    uint256 public cost = 0.04 ether;
    uint256 public maxSupply = 10000;
    uint256 public maxMintAmountPerTx = 10;

    uint8 public publicMintAmount = 20;
    uint8 public allowListMintAmount = 5;

    bool public paused = true;
    bool public isAllowListActive = true;
    bool public revealed = false;

    bytes32 public whitelistMerkleRoot = 0x0758ddc53129da410a7d4536b32e8f2f00e4edf3568e11b199fdc384d06a55d3;

    mapping(address => uint8) private _mintAllowList;
    mapping(address => uint8) private _mintPublicList;


    
    

    constructor() ERC721("Long Lost", "LOST") {
        setHiddenMetadataUri("ipfs://QmZeF9ef2XnK2iiYC2grQ8W8xTMaUchofPEmn1FF1EryNn/hidden.json");
    }

    // -- compliance checks for tx -- 
    //Checks and records # of mints per wallet address for public. 
    function setMintList(uint8 _mintAmount) internal {


        if(!isAllowListActive) {
            require(_mintPublicList[msg.sender] + _mintAmount <= publicMintAmount);
            _mintPublicList[msg.sender] = _mintPublicList[msg.sender] + _mintAmount;
        }

        if(isAllowListActive) {
            require(_mintAllowList[msg.sender] + _mintAmount <= allowListMintAmount);
            _mintAllowList[msg.sender] = _mintAllowList[msg.sender] + _mintAmount;
        }
    }


    //supply limit / tx checks
    modifier mintCompliance(uint256 _mintAmount) {
        require(_mintAmount > 0 && _mintAmount <= maxMintAmountPerTx, "Invalid mint amount!");
        require(supply.current() + _mintAmount <= maxSupply, "Max supply exceeded!");
        _;
    }

    //supply check
    function totalSupply() public view returns (uint256) {
        return supply.current();
    }

    //Contract enablement functions
    function setIsAllowListActive(bool _isAllowListActive) external onlyOwner {
        isAllowListActive = _isAllowListActive;
    }

    function setPaused(bool _state) public onlyOwner {
        paused = _state;
    }
    
    //check minting amounts. Doubles as access check
    function remainingAllowListMints(address userAddress) public view returns (uint256) {
            return allowListMintAmount - _mintAllowList[userAddress];
    }
    

    function remainingPublicMints(address userAddress) public view returns (uint256) { 
            return publicMintAmount - _mintPublicList[userAddress];
    }

    //merkletree 
    function _verify(bytes32 leaf, bytes32[] memory proof) internal view returns (bool) {
        return MerkleProof.verify(proof, whitelistMerkleRoot, leaf);
    }

    function _leaf(address account) internal pure returns (bytes32) {
        return keccak256(abi.encodePacked(account));
    }

    function setWhitelistMerkleRoot(bytes32 _whitelistMerkleRoot) external onlyOwner {
        whitelistMerkleRoot = _whitelistMerkleRoot;
    }

    
    //public mint function
    function mint(uint8 _mintAmount) public payable mintCompliance(_mintAmount) {
        require(!paused, "The contract is paused!");
        require(!isAllowListActive, "Whitelist is still active");

        require(msg.value >= cost * _mintAmount, "Insufficient funds!");

        setMintList(_mintAmount); //compliance check for public minting limits - check minting # per wallet. 
        _mintLoop(msg.sender, _mintAmount);
    }

    //allowlist mint function
    function mintAllowList(uint8 _mintAmount, bytes32[] calldata proof) public payable mintCompliance(_mintAmount) {
        require(!paused, "The contract is paused"); 
        require(isAllowListActive, "Whitelist is paused");
        
        require(_mintAmount <= allowListMintAmount); 
        require(msg.value >= cost * _mintAmount, "Insufficient funds!");
        require(_verify(_leaf(msg.sender), proof), "Invalid proof");

        
        setMintList(_mintAmount);
        _mintLoop(msg.sender, _mintAmount);
    }

    //freemint function
    function mintForAddress(uint8 _mintAmount, address _receiver) public mintCompliance(_mintAmount) onlyOwner {
        _mintLoop(_receiver, _mintAmount);
    }

    //safemint function
    function _mintLoop(address _receiver, uint256 _mintAmount) internal {
        for (uint256 i = 0; i < _mintAmount; i++) {
            supply.increment();
            _safeMint(_receiver, supply.current());
        }
    }

    function walletOfOwner(address _owner)
        public
        view
        returns (uint256[] memory)
        {
            uint256 ownerTokenCount = balanceOf(_owner);
            uint256[] memory ownedTokenIds = new uint256[](ownerTokenCount);
            uint256 currentTokenId = 1;
            uint256 ownedTokenIndex = 0;

    while (ownedTokenIndex < ownerTokenCount && currentTokenId <= maxSupply) {
        address currentTokenOwner = ownerOf(currentTokenId);

            if (currentTokenOwner == _owner) {
            ownedTokenIds[ownedTokenIndex] = currentTokenId;

            ownedTokenIndex++;
            }

        currentTokenId++;
    }

        return ownedTokenIds;
    }

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

    if (revealed == false) {
        return hiddenMetadataUri;
    }

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

    function setRevealed(bool _state) public onlyOwner {
        revealed = _state;
    }

    function setCost(uint256 _cost) public onlyOwner {
        cost = _cost;
    }


    function setHiddenMetadataUri(string memory _hiddenMetadataUri) public onlyOwner {
        hiddenMetadataUri = _hiddenMetadataUri;
    }

    function setUriPrefix(string memory _uriPrefix) public onlyOwner {
        uriPrefix = _uriPrefix;
    }

    function setUriSuffix(string memory _uriSuffix) public onlyOwner {
        uriSuffix = _uriSuffix;
    }

    

    function withdraw() public onlyOwner {
        
        //50% goes to community wallet
        (bool hs, ) = payable(0x086C47A4E99cd41aF68166Fba0ad163546AAA50e).call{value: address(this).balance * 50 / 100}("");
        require(hs);
        //rest to team wallet
        (bool os, ) = payable(owner()).call{value: address(this).balance}("");
        require(os);

    }

    

    function _baseURI() internal view virtual override returns (string memory) {
        return uriPrefix;
    }
}
function mint(uint8 _mintAmount) public payable mintCompliance(_mintAmount) {
        require(!paused, "The contract is paused!");
        require(!isAllowListActive, "Whitelist is still active");

        require(msg.value >= cost * _mintAmount, "Insufficient funds!");

        setMintList(_mintAmount); //compliance check for public minting limits - check minting # per wallet. 
        _mintLoop(msg.sender, _mintAmount);
    }
//public mint function