# Path: DnD/modules/lib/exceptionsLib.py
# class LowOnResource(DnDError):
#     def __init__(self, resource):
#         self.resource = resource
# 
#     def __str__(self):
#         formatstr = 'You have no {rs} remaining.'
#         return formatstr.format(rs=self.resource.name)
# 
# Path: DnD/modules/lib/interface.py
# class DataInterface:
#     class JsonPointerCache:
#         def __init__(self):
#             self.cache = {}
# 
#         def __getitem__(self, key: str) -> jsonpointer.JsonPointer:
#             if key not in self.cache:
#                 self.cache[key] = jsonpointer.JsonPointer(key)
#             return self.cache[key]
# 
#     def __init__(self, data: Union[list, Dict[str, Any]], readonly=False, basepath=""):
#         self.data = data
#         self.basepath = basepath.rstrip('/')
#         self.readonly = readonly
#         self._cache = type(self).JsonPointerCache()
# 
#     def __iter__(self):
#         obj = self.get('/')
#         if isinstance(obj, dict):
#             yield from obj.items()
#         elif isinstance(obj, list):
#             yield from obj
# 
#     def get(self, path: str):
#         if self.basepath + path == '/':
#             return self.data
#         if path == '/':
#             path = ''
#         pointer = self._cache[self.basepath + path]
#         return pointer.resolve(self.data, None)
# 
#     def delete(self, path):
#         if self.readonly:
#             raise ex.ReadonlyError('{} is readonly'.format(self.data))
#         if self.basepath + path == '/':
#             self.data = {}
#             return
#         if path == '/':
#             path = ''
#         pointer = self._cache[self.basepath + path]
#         subdoc, key = pointer.to_last(self.data)
#         del subdoc[key]
# 
#     def set(self, path, value):
#         if self.readonly:
#             raise ex.ReadonlyError('{} is readonly'.format(self.data))
#         if self.basepath + path == '/':
#             self.data = value
#             return
#         if path == '/':
#             path = ''
#         pointer = self._cache[self.basepath + path]
#         pointer.set(self.data, value)
# 
#     def cd(self, path, readonly=False):
#         return DataInterface(self.data, readonly=self.readonly or readonly,
#                              basepath=self.basepath + path)
# 
# Path: DnD/modules/lib/settingsLib.py
# class RestLength(enum.IntEnum):
#     LONG = 100
#     SHORT = 10
#     TURN = 1
#     NOTHING = 0
# 
#     @classmethod
#     def from_string(cls, value):
#         if value == 'long' or value == 'long rest':
#             return RestLength.LONG
#         elif value == 'short' or value == 'short rest':
#             return RestLength.SHORT
#         elif value == 'turn':
#             return RestLength.TURN
#         else:
#             return RestLength.NOTHING


import re
from typing import Optional
from dndice import basic
from . import characterLib as char
from .exceptionsLib import LowOnResource
from .interface import DataInterface
from .settingsLib import RestLength




class Resource:
    def __init__(self, jf: Optional[DataInterface], definition: DataInterface = None):
        self.record = jf
        self.definition = definition or jf
        self.recharge = RestLength.from_string(self.definition.get('/recharge'))
        self.value = self.definition.get('/value')
        self.name = self.definition.get('/name')

    @property
    def number(self):
        return self.record.get('/number')

    @number.setter
    def number(self, value):
        self.record.set('/number', value)

    @property
    def maxnumber(self):
        return self.record.get('/maxnumber') or self.definition.get('/maxnumber')

    @maxnumber.setter
    def maxnumber(self, value):
        self.record.set('/maxnumber', value)

    def use(self, number):
        if self.number < number:

# Path: DnD/modules/lib/exceptionsLib.py
# class LowOnResource(DnDError):
#     def __init__(self, resource):
#         self.resource = resource
# 
#     def __str__(self):
#         formatstr = 'You have no {rs} remaining.'
#         return formatstr.format(rs=self.resource.name)
# 
# Path: DnD/modules/lib/interface.py
# class DataInterface:
#     class JsonPointerCache:
#         def __init__(self):
#             self.cache = {}
# 
#         def __getitem__(self, key: str) -> jsonpointer.JsonPointer:
#             if key not in self.cache:
#                 self.cache[key] = jsonpointer.JsonPointer(key)
#             return self.cache[key]
# 
#     def __init__(self, data: Union[list, Dict[str, Any]], readonly=False, basepath=""):
#         self.data = data
#         self.basepath = basepath.rstrip('/')
#         self.readonly = readonly
#         self._cache = type(self).JsonPointerCache()
# 
#     def __iter__(self):
#         obj = self.get('/')
#         if isinstance(obj, dict):
#             yield from obj.items()
#         elif isinstance(obj, list):
#             yield from obj
# 
#     def get(self, path: str):
#         if self.basepath + path == '/':
#             return self.data
#         if path == '/':
#             path = ''
#         pointer = self._cache[self.basepath + path]
#         return pointer.resolve(self.data, None)
# 
#     def delete(self, path):
#         if self.readonly:
#             raise ex.ReadonlyError('{} is readonly'.format(self.data))
#         if self.basepath + path == '/':
#             self.data = {}
#             return
#         if path == '/':
#             path = ''
#         pointer = self._cache[self.basepath + path]
#         subdoc, key = pointer.to_last(self.data)
#         del subdoc[key]
# 
#     def set(self, path, value):
#         if self.readonly:
#             raise ex.ReadonlyError('{} is readonly'.format(self.data))
#         if self.basepath + path == '/':
#             self.data = value
#             return
#         if path == '/':
#             path = ''
#         pointer = self._cache[self.basepath + path]
#         pointer.set(self.data, value)
# 
#     def cd(self, path, readonly=False):
#         return DataInterface(self.data, readonly=self.readonly or readonly,
#                              basepath=self.basepath + path)
# 
# Path: DnD/modules/lib/settingsLib.py
# class RestLength(enum.IntEnum):
#     LONG = 100
#     SHORT = 10
#     TURN = 1
#     NOTHING = 0
# 
#     @classmethod
#     def from_string(cls, value):
#         if value == 'long' or value == 'long rest':
#             return RestLength.LONG
#         elif value == 'short' or value == 'short rest':
#             return RestLength.SHORT
#         elif value == 'turn':
#             return RestLength.TURN
#         else:
#             return RestLength.NOTHING


# Path: DnD/modules/lib/resourceLib.py
import re
from typing import Optional
from dndice import basic
from . import characterLib as char
from .exceptionsLib import LowOnResource
from .interface import DataInterface
from .settingsLib import RestLength





class Resource:
    def __init__(self, jf: Optional[DataInterface], definition: DataInterface = None):
        self.record = jf
        self.definition = definition or jf
        self.recharge = RestLength.from_string(self.definition.get('/recharge'))
        self.value = self.definition.get('/value')
        self.name = self.definition.get('/name')

    @property
    def number(self):
        return self.record.get('/number')

    @number.setter
    def number(self, value):
        self.record.set('/number', value)

    @property
    def maxnumber(self):
        return self.record.get('/maxnumber') or self.definition.get('/maxnumber')

    @maxnumber.setter
    def maxnumber(self, value):
        self.record.set('/maxnumber', value)

    def use(self, number):
        if self.number < number:

            raise LowOnResource(self)