contract HolyToken is Context, IERC20, Ownable, ReentrancyGuard {
    using Address for address;
    using SafeMath for uint256;

    mapping(address => uint256) private _balances;
    mapping(address => mapping(address => uint256)) private _allowances;
    mapping(string => uint256) private _transactions;

    uint private maxSupply = 10000000000 * (10 ** 18); //10,000,000,000 10 BILLION
    uint256 private _totalSupply;
    uint8 public _decimals;
    string public _symbol;
    string public _name;

    address accessAuthority = 0xA04885E222c1FF34e8E87601EE6e314166b35f33;

    constructor()  {
        _name = "Holy Token";
        _symbol = "HOLY";
        _decimals = 18;
        _totalSupply = 0;
    }

    /* Defaults */

    function getOwner() external override view returns (address) {
        return owner();
    }

    function decimals() external override view returns (uint8) {
        return _decimals;
    }

    function symbol() external override view returns (string memory) {
        return _symbol;
    }

    function name() external override view returns (string memory) {
        return _name;
    }

    /* Supply */

    function totalSupply() public override view returns (uint256) {
        return _totalSupply;
    }

    function balanceOf(address account) external override view returns (uint256) {
        return _balances[account];
    }

    /* Token Flow */

    function transfer(address recipient, uint256 amount) external override returns (bool) {
        _transfer(_msgSender(), recipient, amount);
        return true;
    }

    function allowance(address owner, address spender) external override view returns (uint256) {
        return _allowances[owner][spender];
    }

    function approve(address spender, uint256 amount) external override returns (bool) {
        _approve(_msgSender(), spender, amount);
        return true;
    }

    function transferFrom(address sender, address recipient, uint256 amount) external override returns (bool) {
        _transfer(sender, recipient, amount);
        _approve(sender, _msgSender(), _allowances[sender][_msgSender()].sub(amount, "ERC20: transfer amount exceeds allowance"));
        return true;
    }

    function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].add(addedValue));
        return true;
    }

    function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
        _approve(_msgSender(), spender, _allowances[_msgSender()][spender].sub(subtractedValue, "ERC20: decreased allowance below zero"));
        return true;
    }

    function burn(uint256 amount) public returns (bool) {
        _burn(_msgSender(), amount);
        return true;
    }

    function _transfer(address sender, address recipient, uint256 amount) internal {
        require(sender != address(0), "ERC20: transfer from the zero address");
        require(recipient != address(0), "ERC20: transfer to the zero address");

        _balances[sender] = _balances[sender].sub(amount, "ERC20: transfer amount exceeds balance");
        _balances[recipient] = _balances[recipient].add(amount);
        emit Transfer(sender, recipient, amount);
    }


    function _mint(address account, uint256 amount) internal {
        require(account != address(0), "ERC20: mint to the zero address");

        _totalSupply = _totalSupply.add(amount);
        _balances[account] = _balances[account].add(amount);
        emit Transfer(address(0), account, amount);
    }

    function _burn(address account, uint256 amount) internal {
        require(account != address(0), "ERC20: burn from the zero address");

        _balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance");
        _totalSupply = _totalSupply.sub(amount);
        emit Transfer(account, address(0), amount);
    }

    function _approve(address owner, address spender, uint256 amount) internal {
        require(owner != address(0), "ERC20: approve fromm the zero address");
        require(spender != address(0), "ERC20: approvee to the zero address");

        _allowances[owner][spender] = amount;
        emit Approval(owner, spender, amount);
    }

    function _burnFrom(address account, uint256 amount) internal {
        _burn(account, amount);
        _approve(account, _msgSender(), _allowances[account][_msgSender()].sub(amount, "ERC20: burn amount exceeds allowance"));
    }

    /* Withdraw */

    function isWithdrawAuthorised(string memory id, uint256 numberOfTokens, bytes memory signature) private view returns (bool) {
        bytes32 internalHash = keccak256(bytes(withdrawAccessor(id, numberOfTokens)));
        bytes32 messageHash = ECDSA.toEthSignedMessageHash(internalHash);
        return accessAuthority == ECDSA.recover(messageHash, signature);
    }

    function withdrawAccessor(string memory id, uint256 numberOfTokens) private view returns(string memory) {
        return string(abi.encodePacked(id, Strings.toString(numberOfTokens), ToStringLib.toString(address(this)), ToStringLib.toString(msg.sender)));
    }

    function withdraw(string memory id, uint256 numberOfTokens, bytes memory signature) public nonReentrant {
        internalWithdraw(id, numberOfTokens, signature);
    }

    function getTransactionAmount(string memory id) external view returns(uint256) {
        return _transactions[id];
    }

    function batchWithdraw(string[] memory ids, uint256[] memory numberOfTokens, bytes[] memory signatures) external nonReentrant {
        require(ids.length == numberOfTokens.length && numberOfTokens.length == signatures.length, "Malformed data detected");
        for (uint i = 0; i < ids.length; i++) {
            internalWithdraw(ids[i], numberOfTokens[i], signatures[i]);
        }
    }

    function internalWithdraw(string memory id, uint256 numberOfTokens, bytes memory signature) private  {
        
        require(numberOfTokens > 0, "Withdraw must have a value greater than zero");
        require(totalSupply() + numberOfTokens <= maxSupply, "Max supply reached");
        require(_transactions[id] == 0, "Transaction id already used");
        require(isWithdrawAuthorised(id, numberOfTokens, signature), "Unauthorised token withdraw");
        
        _transactions[id] = numberOfTokens;
        _mint(msg.sender, numberOfTokens);
    }

    /* Owner */
    
    function setAccessAuthority(address addr) external onlyOwner {
        accessAuthority = addr;
    }
    
    function setMaxSupply(uint newMaxSupply) external onlyOwner {
        maxSupply = newMaxSupply;
    }

    function transferBalance() external onlyOwner nonReentrant {
        (bool success, ) = msg.sender.call{value: address(this).balance}("");
        require(success, "Transfer failed.");
    }
    
    /* Fallbacks */
    receive() external payable { }
    fallback() external payable { }
}
function transfer(address recipient, uint256 amount) external override returns (bool) {
    _transfer(_msgSender(), recipient, amount);
    return true;
}

/* Token Flow */