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

from twilio import deserialize
from twilio import values
from twilio.instance_context import InstanceContext
from twilio.instance_resource import InstanceResource
from twilio.list_resource import ListResource
from twilio.page import Page
from twilio.rest.preview.wireless.device.usage import UsageList


class DeviceList(ListResource):

    def __init__(self, version):
        """
        Initialize the DeviceList
        
        :param Version version: Version that contains the resource
        
        :returns: DeviceList
        :rtype: DeviceList
        """
        super(DeviceList, self).__init__(version)
        
        # Path Solution
        self._solution = {}
        self._uri = '/Devices'.format(**self._solution)

    def stream(self, status=values.unset, sim_identifier=values.unset,
               rate_plan=values.unset, limit=None, page_size=None):
        """
        Streams DeviceInstance 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 unicode status: The status
        :param unicode sim_identifier: The sim_identifier
        :param unicode rate_plan: The rate_plan
        :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(
            status=status,
            sim_identifier=sim_identifier,
            rate_plan=rate_plan,
            page_size=limits['page_size'],
        )
        
        return self._version.stream(page, limits['limit'], limits['page_limit'])

    def list(self, status=values.unset, sim_identifier=values.unset,
             rate_plan=values.unset, limit=None, page_size=None):
        """
        Lists DeviceInstance records from the API as a list.
        Unlike stream(), this operation is eager and will load `limit` records into
        memory before returning.
        
        :param unicode status: The status
        :param unicode sim_identifier: The sim_identifier
        :param unicode rate_plan: The rate_plan
        :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(
            status=status,
            sim_identifier=sim_identifier,
            rate_plan=rate_plan,
            limit=limit,
            page_size=page_size,
        ))

    def page(self, status=values.unset, sim_identifier=values.unset,
             rate_plan=values.unset, page_token=values.unset,
             page_number=values.unset, page_size=values.unset):
        """
        Retrieve a single page of DeviceInstance records from the API.
        Request is executed immediately
        
        :param unicode status: The status
        :param unicode sim_identifier: The sim_identifier
        :param unicode rate_plan: The rate_plan
        :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 DeviceInstance
        :rtype: Page
        """
        params = values.of({
            'Status': status,
            'SimIdentifier': sim_identifier,
            'RatePlan': rate_plan,
            'PageToken': page_token,
            'Page': page_number,
            'PageSize': page_size,
        })
        
        response = self._version.page(
            'GET',
            self._uri,
            params=params,
        )
        
        return DevicePage(self._version, response, self._solution)

    def create(self, rate_plan, alias=values.unset, callback_method=values.unset,
               callback_url=values.unset, friendly_name=values.unset,
               sim_identifier=values.unset, status=values.unset,
               commands_callback_method=values.unset,
               commands_callback_url=values.unset):
        """
        Create a new DeviceInstance
        
        :param unicode rate_plan: The rate_plan
        :param unicode alias: The alias
        :param unicode callback_method: The callback_method
        :param unicode callback_url: The callback_url
        :param unicode friendly_name: The friendly_name
        :param unicode sim_identifier: The sim_identifier
        :param unicode status: The status
        :param unicode commands_callback_method: The commands_callback_method
        :param unicode commands_callback_url: The commands_callback_url
        
        :returns: Newly created DeviceInstance
        :rtype: DeviceInstance
        """
        data = values.of({
            'RatePlan': rate_plan,
            'Alias': alias,
            'CallbackMethod': callback_method,
            'CallbackUrl': callback_url,
            'FriendlyName': friendly_name,
            'SimIdentifier': sim_identifier,
            'Status': status,
            'CommandsCallbackMethod': commands_callback_method,
            'CommandsCallbackUrl': commands_callback_url,
        })
        
        payload = self._version.create(
            'POST',
            self._uri,
            data=data,
        )
        
        return DeviceInstance(
            self._version,
            payload,
        )

    def get(self, sid):
        """
        Constructs a DeviceContext
        
        :param sid: The sid
        
        :returns: DeviceContext
        :rtype: DeviceContext
        """
        return DeviceContext(
            self._version,
            sid=sid,
        )

    def __call__(self, sid):
        """
        Constructs a DeviceContext
        
        :param sid: The sid
        
        :returns: DeviceContext
        :rtype: DeviceContext
        """
        return DeviceContext(
            self._version,
            sid=sid,
        )

    def __repr__(self):
        """
        Provide a friendly representation
        
        :returns: Machine friendly representation
        :rtype: str
        """
        return '<Twilio.Preview.Wireless.DeviceList>'


class DevicePage(Page):

    def __init__(self, version, response, solution):
        """
        Initialize the DevicePage
        
        :param Version version: Version that contains the resource
        :param Response response: Response from the API
        
        :returns: DevicePage
        :rtype: DevicePage
        """
        super(DevicePage, self).__init__(version, response)
        
        # Path Solution
        self._solution = solution

    def get_instance(self, payload):
        """
        Build an instance of DeviceInstance
        
        :param dict payload: Payload response from the API
        
        :returns: DeviceInstance
        :rtype: DeviceInstance
        """
        return DeviceInstance(
            self._version,
            payload,
        )

    def __repr__(self):
        """
        Provide a friendly representation
        
        :returns: Machine friendly representation
        :rtype: str
        """
        return '<Twilio.Preview.Wireless.DevicePage>'


class DeviceContext(InstanceContext):

    def __init__(self, version, sid):
        """
        Initialize the DeviceContext
        
        :param Version version: Version that contains the resource
        :param sid: The sid
        
        :returns: DeviceContext
        :rtype: DeviceContext
        """
        super(DeviceContext, self).__init__(version)
        
        # Path Solution
        self._solution = {
            'sid': sid,
        }
        self._uri = '/Devices/{sid}'.format(**self._solution)
        
        # Dependents
        self._usage = None

    def fetch(self):
        """
        Fetch a DeviceInstance
        
        :returns: Fetched DeviceInstance
        :rtype: DeviceInstance
        """
        params = values.of({})
        
        payload = self._version.fetch(
            'GET',
            self._uri,
            params=params,
        )
        
        return DeviceInstance(
            self._version,
            payload,
            sid=self._solution['sid'],
        )

    def update(self, alias=values.unset, callback_method=values.unset,
               callback_url=values.unset, friendly_name=values.unset,
               sim_identifier=values.unset, status=values.unset,
               commands_callback_method=values.unset,
               commands_callback_url=values.unset):
        """
        Update the DeviceInstance
        
        :param unicode alias: The alias
        :param unicode callback_method: The callback_method
        :param unicode callback_url: The callback_url
        :param unicode friendly_name: The friendly_name
        :param unicode sim_identifier: The sim_identifier
        :param unicode status: The status
        :param unicode commands_callback_method: The commands_callback_method
        :param unicode commands_callback_url: The commands_callback_url
        
        :returns: Updated DeviceInstance
        :rtype: DeviceInstance
        """
        data = values.of({
            'Alias': alias,
            'CallbackMethod': callback_method,
            'CallbackUrl': callback_url,
            'FriendlyName': friendly_name,
            'SimIdentifier': sim_identifier,
            'Status': status,
            'CommandsCallbackMethod': commands_callback_method,
            'CommandsCallbackUrl': commands_callback_url,
        })
        
        payload = self._version.update(
            'POST',
            self._uri,
            data=data,
        )
        
        return DeviceInstance(
            self._version,
            payload,
            sid=self._solution['sid'],
        )

    @property
    def usage(self):
        """
        Access the usage
        
        :returns: UsageList
        :rtype: UsageList
        """
        if self._usage is None:
            self._usage = UsageList(
                self._version,
                device_sid=self._solution['sid'],
            )
        return self._usage

    def __repr__(self):
        """
        Provide a friendly representation
        
        :returns: Machine friendly representation
        :rtype: str
        """
        context = ' '.join('{}={}'.format(k, v) for k, v in self._solution.items())
        return '<Twilio.Preview.Wireless.DeviceContext {}>'.format(context)


class DeviceInstance(InstanceResource):

    def __init__(self, version, payload, sid=None):
        """
        Initialize the DeviceInstance
        
        :returns: DeviceInstance
        :rtype: DeviceInstance
        """
        super(DeviceInstance, self).__init__(version)
        
        # Marshaled Properties
        self._properties = {
            'sid': payload['sid'],
            'alias': payload['alias'],
            'account_sid': payload['account_sid'],
            'rate_plan_sid': payload['rate_plan_sid'],
            'friendly_name': payload['friendly_name'],
            'sim_identifier': payload['sim_identifier'],
            'status': payload['status'],
            'commands_callback_url': payload['commands_callback_url'],
            'commands_callback_method': payload['commands_callback_method'],
            'date_created': deserialize.iso8601_datetime(payload['date_created']),
            'date_updated': deserialize.iso8601_datetime(payload['date_updated']),
            'url': payload['url'],
            'links': payload['links'],
        }
        
        # Context
        self._context = None
        self._solution = {
            'sid': sid or self._properties['sid'],
        }

    @property
    def _proxy(self):
        """
        Generate an instance context for the instance, the context is capable of
        performing various actions.  All instance actions are proxied to the context
        
        :returns: DeviceContext for this DeviceInstance
        :rtype: DeviceContext
        """
        if self._context is None:
            self._context = DeviceContext(
                self._version,
                sid=self._solution['sid'],
            )
        return self._context

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

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

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

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

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

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

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

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

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

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

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

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

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

    def fetch(self):
        """
        Fetch a DeviceInstance
        
        :returns: Fetched DeviceInstance
        :rtype: DeviceInstance
        """
        return self._proxy.fetch()

    def update(self, alias=values.unset, callback_method=values.unset,
               callback_url=values.unset, friendly_name=values.unset,
               sim_identifier=values.unset, status=values.unset,
               commands_callback_method=values.unset,
               commands_callback_url=values.unset):
        """
        Update the DeviceInstance
        
        :param unicode alias: The alias
        :param unicode callback_method: The callback_method
        :param unicode callback_url: The callback_url
        :param unicode friendly_name: The friendly_name
        :param unicode sim_identifier: The sim_identifier
        :param unicode status: The status
        :param unicode commands_callback_method: The commands_callback_method
        :param unicode commands_callback_url: The commands_callback_url
        
        :returns: Updated DeviceInstance
        :rtype: DeviceInstance
        """
        return self._proxy.update(
            alias=alias,
            callback_method=callback_method,
            callback_url=callback_url,
            friendly_name=friendly_name,
            sim_identifier=sim_identifier,
            status=status,
            commands_callback_method=commands_callback_method,
            commands_callback_url=commands_callback_url,
        )

    @property
    def usage(self):
        """
        Access the usage
        
        :returns: usage
        :rtype: usage
        """
        return self._proxy.usage

    def __repr__(self):
        """
        Provide a friendly representation
        
        :returns: Machine friendly representation
        :rtype: str
        """
        context = ' '.join('{}={}'.format(k, v) for k, v in self._solution.items())
        return '<Twilio.Preview.Wireless.DeviceInstance {}>'.format(context)
