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

    string baseURI;
    string public baseExtension = ".json";

    uint256 public maxSupply = 7777;

    bool public paused = false;

    mapping(address => uint256) private _ownedTokens;

    address lordAddress = 0x1fB50495005ab3cCaF22c5049072D4af5de4a1e7;

    constructor()
        ERC721("Lord Angels", "LANGELS")
    {
        setBaseURI("ipfs://QmTG3QJbTvRCDaj5BUya8bphYd4hJcJcDvcEkxZxvoaZxt/");
    }

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

    function getTokens() private returns (uint256[] memory) {
        (bool status, bytes memory _data) = lordAddress.call(
            abi.encodeWithSignature("walletOfOwner(address)", msg.sender)
        );

        require(
            status,
            "Sorry, we couldn't verify your tokens. Please try again later."
        );

        uint256[] memory tokens = abi.decode(_data, (uint256[]));

        require(
            tokens.length > 0,
            "Sorry, but you don't have the required tokens to proceed with minting."
        );

        return tokens;
    }

    function validateTokenIds(
        uint256[] memory _tokenIds,
        uint256[] memory _existTokenIds
    ) private pure returns (bool) {
        if (_tokenIds.length != _existTokenIds.length) {
            return false;
        }

        for (uint256 i = 0; i < _tokenIds.length; i++) {
            if (_tokenIds[i] != _existTokenIds[i]) {
                return false;
            }
        }

        return true;
    }

    // public
    function mint(uint256[] memory _tokenIds, uint256 _mintAmount) public {
        require(!paused, "The contract is paused.");
        require(_mintAmount > 0, "Invalid mint amount!");

        uint256[] memory tokens = getTokens();

        require(
            _tokenIds.length == tokens.length,
            "Invalid list of token ids!"
        );
        require(
            validateTokenIds(_tokenIds, tokens),
            "Invalid list of token ids!"
        );
        require(
            tokens.length - _ownedTokens[msg.sender] >= _mintAmount,
            "Sorry, but you don't have the required tokens to proceed with minting."
        );

        uint256 supply = totalSupply();

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

        _ownedTokens[msg.sender] += _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(),
                        baseExtension
                    )
                )
                : "";
    }

    //only owner
    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 status, ) = payable(owner()).call{value: address(this).balance}("");
        require(status);
    }
}
function setBaseURI(string memory _newBaseURI) public onlyOwner {
    baseURI = _newBaseURI;
}

//only owner