# coding=utf-8
"""
This code was generated by
\ / _    _  _|   _  _
 | (_)\/(_)(_|\/| |(/_  v1.0.0
      /       /       
"""

from twilio import deserialize
from twilio import serialize
from twilio import values
from twilio.instance_resource import InstanceResource
from twilio.list_resource import ListResource
from twilio.page import Page
from twilio.rest.api.v2010.account.usage.record.all_time import AllTimeList
from twilio.rest.api.v2010.account.usage.record.daily import DailyList
from twilio.rest.api.v2010.account.usage.record.last_month import LastMonthList
from twilio.rest.api.v2010.account.usage.record.monthly import MonthlyList
from twilio.rest.api.v2010.account.usage.record.this_month import ThisMonthList
from twilio.rest.api.v2010.account.usage.record.today import TodayList
from twilio.rest.api.v2010.account.usage.record.yearly import YearlyList
from twilio.rest.api.v2010.account.usage.record.yesterday import YesterdayList


class RecordList(ListResource):

    def __init__(self, version, account_sid):
        """
        Initialize the RecordList
        
        :param Version version: Version that contains the resource
        :param account_sid: A 34 character string that uniquely identifies this resource.
        
        :returns: RecordList
        :rtype: RecordList
        """
        super(RecordList, self).__init__(version)
        
        # Path Solution
        self._solution = {
            'account_sid': account_sid,
        }
        self._uri = '/Accounts/{account_sid}/Usage/Records.json'.format(**self._solution)
        
        # Components
        self._all_time = None
        self._daily = None
        self._last_month = None
        self._monthly = None
        self._this_month = None
        self._today = None
        self._yearly = None
        self._yesterday = None

    def stream(self, category=values.unset, start_date_before=values.unset,
               start_date=values.unset, start_date_after=values.unset,
               end_date_before=values.unset, end_date=values.unset,
               end_date_after=values.unset, limit=None, page_size=None):
        """
        Streams RecordInstance records from the API as a generator stream.
        This operation lazily loads records as efficiently as possible until the limit
        is reached.
        The results are returned as a generator, so this operation is memory efficient.
        
        :param record.category category: Only include usage of a given category
        :param date start_date_before: Filter by start date
        :param date start_date: Filter by start date
        :param date start_date_after: Filter by start date
        :param date end_date_before: Filter by end date
        :param date end_date: Filter by end date
        :param date end_date_after: Filter by end date
        :param int limit: Upper limit for the number of records to return. stream()
                          guarantees to never return more than limit.  Default is no limit
        :param int page_size: Number of records to fetch per request, when not set will use
                              the default value of 50 records.  If no page_size is defined
                              but a limit is defined, stream() will attempt to read the
                              limit with the most efficient page size, i.e. min(limit, 1000)
        
        :returns: Generator that will yield up to limit results
        :rtype: generator
        """
        limits = self._version.read_limits(limit, page_size)
        
        page = self.page(
            category=category,
            start_date_before=start_date_before,
            start_date=start_date,
            start_date_after=start_date_after,
            end_date_before=end_date_before,
            end_date=end_date,
            end_date_after=end_date_after,
            page_size=limits['page_size'],
        )
        
        return self._version.stream(page, limits['limit'], limits['page_limit'])

    def list(self, category=values.unset, start_date_before=values.unset,
             start_date=values.unset, start_date_after=values.unset,
             end_date_before=values.unset, end_date=values.unset,
             end_date_after=values.unset, limit=None, page_size=None):
        """
        Lists RecordInstance records from the API as a list.
        Unlike stream(), this operation is eager and will load `limit` records into
        memory before returning.
        
        :param record.category category: Only include usage of a given category
        :param date start_date_before: Filter by start date
        :param date start_date: Filter by start date
        :param date start_date_after: Filter by start date
        :param date end_date_before: Filter by end date
        :param date end_date: Filter by end date
        :param date end_date_after: Filter by end date
        :param int limit: Upper limit for the number of records to return. list() guarantees
                          never to return more than limit.  Default is no limit
        :param int page_size: Number of records to fetch per request, when not set will use
                              the default value of 50 records.  If no page_size is defined
                              but a limit is defined, list() will attempt to read the limit
                              with the most efficient page size, i.e. min(limit, 1000)
        
        :returns: Generator that will yield up to limit results
        :rtype: generator
        """
        return list(self.stream(
            category=category,
            start_date_before=start_date_before,
            start_date=start_date,
            start_date_after=start_date_after,
            end_date_before=end_date_before,
            end_date=end_date,
            end_date_after=end_date_after,
            limit=limit,
            page_size=page_size,
        ))

    def page(self, category=values.unset, start_date_before=values.unset,
             start_date=values.unset, start_date_after=values.unset,
             end_date_before=values.unset, end_date=values.unset,
             end_date_after=values.unset, page_token=values.unset,
             page_number=values.unset, page_size=values.unset):
        """
        Retrieve a single page of RecordInstance records from the API.
        Request is executed immediately
        
        :param record.category category: Only include usage of a given category
        :param date start_date_before: Filter by start date
        :param date start_date: Filter by start date
        :param date start_date_after: Filter by start date
        :param date end_date_before: Filter by end date
        :param date end_date: Filter by end date
        :param date end_date_after: Filter by end date
        :param str page_token: PageToken provided by the API
        :param int page_number: Page Number, this value is simply for client state
        :param int page_size: Number of records to return, defaults to 50
        
        :returns: Page of RecordInstance
        :rtype: Page
        """
        params = values.of({
            'Category': category,
            'StartDate<': serialize.iso8601_date(start_date_before),
            'StartDate': serialize.iso8601_date(start_date),
            'StartDate>': serialize.iso8601_date(start_date_after),
            'EndDate<': serialize.iso8601_date(end_date_before),
            'EndDate': serialize.iso8601_date(end_date),
            'EndDate>': serialize.iso8601_date(end_date_after),
            'PageToken': page_token,
            'Page': page_number,
            'PageSize': page_size,
        })
        
        response = self._version.page(
            'GET',
            self._uri,
            params=params,
        )
        
        return RecordPage(self._version, response, self._solution)

    @property
    def all_time(self):
        """
        Access the all_time
        
        :returns: AllTimeList
        :rtype: AllTimeList
        """
        if self._all_time is None:
            self._all_time = AllTimeList(
                self._version,
                account_sid=self._solution['account_sid'],
            )
        return self._all_time

    @property
    def daily(self):
        """
        Access the daily
        
        :returns: DailyList
        :rtype: DailyList
        """
        if self._daily is None:
            self._daily = DailyList(
                self._version,
                account_sid=self._solution['account_sid'],
            )
        return self._daily

    @property
    def last_month(self):
        """
        Access the last_month
        
        :returns: LastMonthList
        :rtype: LastMonthList
        """
        if self._last_month is None:
            self._last_month = LastMonthList(
                self._version,
                account_sid=self._solution['account_sid'],
            )
        return self._last_month

    @property
    def monthly(self):
        """
        Access the monthly
        
        :returns: MonthlyList
        :rtype: MonthlyList
        """
        if self._monthly is None:
            self._monthly = MonthlyList(
                self._version,
                account_sid=self._solution['account_sid'],
            )
        return self._monthly

    @property
    def this_month(self):
        """
        Access the this_month
        
        :returns: ThisMonthList
        :rtype: ThisMonthList
        """
        if self._this_month is None:
            self._this_month = ThisMonthList(
                self._version,
                account_sid=self._solution['account_sid'],
            )
        return self._this_month

    @property
    def today(self):
        """
        Access the today
        
        :returns: TodayList
        :rtype: TodayList
        """
        if self._today is None:
            self._today = TodayList(
                self._version,
                account_sid=self._solution['account_sid'],
            )
        return self._today

    @property
    def yearly(self):
        """
        Access the yearly
        
        :returns: YearlyList
        :rtype: YearlyList
        """
        if self._yearly is None:
            self._yearly = YearlyList(
                self._version,
                account_sid=self._solution['account_sid'],
            )
        return self._yearly

    @property
    def yesterday(self):
        """
        Access the yesterday
        
        :returns: YesterdayList
        :rtype: YesterdayList
        """
        if self._yesterday is None:
            self._yesterday = YesterdayList(
                self._version,
                account_sid=self._solution['account_sid'],
            )
        return self._yesterday

    def __repr__(self):
        """
        Provide a friendly representation
        
        :returns: Machine friendly representation
        :rtype: str
        """
        return '<Twilio.Api.V2010.RecordList>'


class RecordPage(Page):

    def __init__(self, version, response, solution):
        """
        Initialize the RecordPage
        
        :param Version version: Version that contains the resource
        :param Response response: Response from the API
        :param account_sid: A 34 character string that uniquely identifies this resource.
        
        :returns: RecordPage
        :rtype: RecordPage
        """
        super(RecordPage, self).__init__(version, response)
        
        # Path Solution
        self._solution = solution

    def get_instance(self, payload):
        """
        Build an instance of RecordInstance
        
        :param dict payload: Payload response from the API
        
        :returns: RecordInstance
        :rtype: RecordInstance
        """
        return RecordInstance(
            self._version,
            payload,
            account_sid=self._solution['account_sid'],
        )

    def __repr__(self):
        """
        Provide a friendly representation
        
        :returns: Machine friendly representation
        :rtype: str
        """
        return '<Twilio.Api.V2010.RecordPage>'


class RecordInstance(InstanceResource):

    def __init__(self, version, payload, account_sid):
        """
        Initialize the RecordInstance
        
        :returns: RecordInstance
        :rtype: RecordInstance
        """
        super(RecordInstance, self).__init__(version)
        
        # Marshaled Properties
        self._properties = {
            'account_sid': payload['account_sid'],
            'api_version': payload['api_version'],
            'category': payload['category'],
            'count': payload['count'],
            'count_unit': payload['count_unit'],
            'description': payload['description'],
            'end_date': deserialize.iso8601_datetime(payload['end_date']),
            'price': deserialize.decimal(payload['price']),
            'price_unit': payload['price_unit'],
            'start_date': deserialize.iso8601_datetime(payload['start_date']),
            'subresource_uris': payload['subresource_uris'],
            'uri': payload['uri'],
            'usage': payload['usage'],
            'usage_unit': payload['usage_unit'],
        }
        
        # Context
        self._context = None
        self._solution = {
            'account_sid': account_sid,
        }

    @property
    def account_sid(self):
        """
        :returns: The Account that accrued the usage
        :rtype: unicode
        """
        return self._properties['account_sid']

    @property
    def api_version(self):
        """
        :returns: The api_version
        :rtype: unicode
        """
        return self._properties['api_version']

    @property
    def category(self):
        """
        :returns: The category of usage
        :rtype: record.category
        """
        return self._properties['category']

    @property
    def count(self):
        """
        :returns: The number of usage events (e.g. the number of calls).
        :rtype: unicode
        """
        return self._properties['count']

    @property
    def count_unit(self):
        """
        :returns: The unit in which `Count` is measured
        :rtype: unicode
        """
        return self._properties['count_unit']

    @property
    def description(self):
        """
        :returns: A human-readable description of the usage category.
        :rtype: unicode
        """
        return self._properties['description']

    @property
    def end_date(self):
        """
        :returns: The last date usage is included in this record
        :rtype: datetime
        """
        return self._properties['end_date']

    @property
    def price(self):
        """
        :returns: The total price of the usage
        :rtype: unicode
        """
        return self._properties['price']

    @property
    def price_unit(self):
        """
        :returns: The currency in which `Price` is measured
        :rtype: unicode
        """
        return self._properties['price_unit']

    @property
    def start_date(self):
        """
        :returns: The first date usage is included in this record
        :rtype: datetime
        """
        return self._properties['start_date']

    @property
    def subresource_uris(self):
        """
        :returns: Subresources Uris for this UsageRecord
        :rtype: unicode
        """
        return self._properties['subresource_uris']

    @property
    def uri(self):
        """
        :returns: The URI for this resource
        :rtype: unicode
        """
        return self._properties['uri']

    @property
    def usage(self):
        """
        :returns: The amount of usage
        :rtype: unicode
        """
        return self._properties['usage']

    @property
    def usage_unit(self):
        """
        :returns: The units in which `Usage` is measured
        :rtype: unicode
        """
        return self._properties['usage_unit']

    def __repr__(self):
        """
        Provide a friendly representation
        
        :returns: Machine friendly representation
        :rtype: str
        """
        return '<Twilio.Api.V2010.RecordInstance>'
