from ...utils import rest
from starkcore.utils.api import from_api_json
from starkcore.utils.resource import Resource
from starkcore.utils.checks import check_datetime, check_date
from ..__ledger import _resource as _ledger_resource


class Log(Resource):
    """# ledger.Log object
    Every time a Ledger entity is updated, a corresponding ledger.Log
    is generated for the entity. This log is never generated by the
    user, but it can be retrieved to check additional information
    on the Ledger.
    ## Attributes (return-only):
    - id [string]: unique id returned when the log is created. ex: "5656565656565656"
    - created [datetime.datetime]: creation datetime for the log. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0)
    - type [string]: type of the Ledger event which triggered the log creation. ex: "created", "updated"
    - ledger [Ledger]: Ledger entity to which the log refers to.
    """

    def __init__(self, id, created, type, ledger):
        Resource.__init__(self, id=id)

        self.created = check_datetime(created)
        self.type = type
        self.ledger = from_api_json(_ledger_resource, ledger)


_resource = {"class": Log, "name": "LedgerLog"}


def get(id, user=None):
    """# Retrieve a specific ledger.Log
    Receive a single ledger.Log object previously created by the Stark Infra API by its id
    ## Parameters (required):
    - id [string]: object unique id. ex: "5656565656565656"
    ## Parameters (optional):
    - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call
    ## Return:
    - ledger.Log object with updated attributes
    """
    return rest.get_id(resource=_resource, id=id, user=user)


def query(ids=None, limit=None, after=None, before=None, ledger_id=None, user=None):
    """# Retrieve ledger.Logs
    Receive a generator of ledger.Log objects previously created in the Stark Infra API
    ## Parameters (optional):
    - ids [list of strings, default None]: list of LedgerLog ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"]
    - limit [integer, default None]: maximum number of objects to be retrieved. Unlimited if None. ex: 35
    - after [datetime.date or string, default None] date filter for objects created only after specified date. ex: datetime.date(2020, 3, 10)
    - before [datetime.date or string, default None] date filter for objects created only before specified date. ex: datetime.date(2020, 3, 10)
    - ledger_id [string, default None]: filter logs by Ledger id. ex: "5656565656565656"
    - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call
    ## Return:
    - generator of ledger.Log objects with updated attributes
    """
    return rest.get_stream(
        resource=_resource,
        ids=ids,
        limit=limit,
        after=check_date(after),
        before=check_date(before),
        ledger_id=ledger_id,
        user=user,
    )


def page(ids=None, limit=None, after=None, before=None, ledger_id=None, cursor=None, user=None):
    """# Retrieve paged ledger.Logs
    Receive a list of up to 100 ledger.Log objects previously created in the Stark Infra API and the cursor to the next page.
    Use this function instead of query if you want to manually page your requests.
    ## Parameters (optional):
    - ids [list of strings, default None]: list of LedgerLog ids to filter retrieved objects. ex: ["5656565656565656", "4545454545454545"]
    - limit [integer, default 100]: maximum number of objects to be retrieved. Max = 100. ex: 35
    - after [datetime.date or string, default None] date filter for objects created only after specified date. ex: datetime.date(2020, 3, 10)
    - before [datetime.date or string, default None] date filter for objects created only before specified date. ex: datetime.date(2020, 3, 10)
    - ledger_id [string, default None]: filter logs by Ledger id. ex: "5656565656565656"
    - cursor [string, default None]: cursor returned on the previous page function call
    - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call
    ## Return:
    - list of ledger.Log objects with updated attributes
    - cursor to retrieve the next page of ledger.Log objects
    """
    return rest.get_page(
        resource=_resource,
        cursor=cursor,
        ids=ids,
        limit=limit,
        after=check_date(after),
        before=check_date(before),
        ledger_id=ledger_id,
        user=user,
    )
