from starkcore.utils.api import from_api_json
from starkcore.utils.resource import Resource
from starkcore.utils.checks import check_datetime, check_date
from ...utils import rest
from ..__issuingtoken import _resource as _issuing_token_resource
from starkcore.error import Error


class Log(Resource):
    """# issuingtoken.Log object
    Every time an IssuingToken entity is modified, a corresponding issuingtoken.Log
    is generated for the entity. This log is never generated by the user.
    ## Attributes (return-only):
    - id [string]: unique id returned when the log is created. ex: "5656565656565656"
    - token [IssuingToken]: IssuingToken entity to which the log refers to.
    - errors [list of strings]: list of errors linked to this IssuingToken event.
    - type [string]: type of the IssuingToken event which triggered the log creation. ex: "active", "blocked", "canceled", "frozen" or "pending"
    - created [datetime.datetime]: creation datetime for the log. ex: datetime.datetime(2020, 3, 10, 10, 30, 0, 0)
    """

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

        self.token = from_api_json(_issuing_token_resource, token)
        self.type = type
        self.created = created
        self.errors = errors


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


def get(id, user=None):
    """# Retrieve a specific issuingtoken.Log
    Receive a single issuingtoken.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:
    - issuingtoken.Log object with updated attributes
    """
    return rest.get_id(resource=_resource, id=id, user=user)


def query(limit=None, after=None, before=None, types=None, token_ids=None, ids=None, user=None):
    """# Retrieve issuingtoken.Log
    Receive a generator of issuingtoken.Log objects previously created in the Stark Infra API
    ## Parameters (optional):
    - 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)
    - types [list of strings, default None]: filter for log event types. ex: ["active", "blocked", "canceled", "frozen", "pending"]
    - token_ids [list of strings, default None]: list of IssuingToken ids to filter logs. ex: ["5656565656565656", "4545454545454545"]
    - ids [list of strings, default None]: list of ids to filter logs. ex: ["5656565656565656", "4545454545454545"]
    - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call
    ## Return:
    - generator of issuingtoken.Log objects with updated attributes
    """
    return rest.get_stream(
        resource=_resource,
        limit=limit,
        after=check_date(after),
        before=check_date(before),
        types=types,
        token_ids=token_ids,
        ids=ids,
        user=user,
    )


def page(limit=None, after=None, before=None, types=None, token_ids=None, cursor = None, ids=None, user=None):
    """# Retrieve paged issuingtoken.Log
    Receive a list of up to 100 issuingtoken.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):
    - cursor [string, default None]: cursor returned on the previous page function call
    - 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)
    - types [list of strings, default None]: filter for log event types. ex: ["active", "blocked", "canceled", "frozen", "pending"]
    - token_ids [list of strings, default None]: list of IssuingToken ids to filter logs. ex: ["5656565656565656", "4545454545454545"]
    - ids [list of strings, default None]: list of ids to filter logs. ex: ["5656565656565656", "4545454545454545"]
    - user [Organization/Project object, default None]: Organization or Project object. Not necessary if starkinfra.user was set before function call
    ## Return:
    - list of issuingtoken.Log objects with updated attributes
    - cursor to retrieve the next page of issuingtoken.Log objects
    """
    return rest.get_page(
        resource=_resource,
        limit=limit,
        after=check_date(after),
        before=check_date(before),
        types=types,
        token_ids=token_ids,
        cursor=cursor,
        ids=ids,
        user=user,
    )
