# -*- coding: utf-8 -*-

"""
zypev1

This file was automatically generated by APIMATIC v3.0 (
 https://www.apimatic.io ).
"""

from zypev1.api_helper import APIHelper
from zypev1.configuration import Server
from zypev1.controllers.base_controller import BaseController
from zypev1.models.video_entitlement_metadata import VideoEntitlementMetadata
from zypev1.models.check_entitlement_response import CheckEntitlementResponse


class VideoEntitlementsController(BaseController):

    """A Controller to access Endpoints in the zypev1 API."""

    def __init__(self, config, call_back=None):
        super(VideoEntitlementsController, self).__init__(config, call_back)

    def consumer_videos_get(self,
                            id=None,
                            created_at=None,
                            expires_at=None,
                            transaction_id=None,
                            transaction_type=None,
                            video_id=None,
                            q=None,
                            order=None,
                            sort=None,
                            page=None,
                            per_page=None):
        """Does a GET request to /consumer/videos.

        List All Video Entitlements

        Args:
            id (string, optional): Filter by ID + Examples     +
                `id=5992f328ce593000ef000008`     + use
                `id!=5992f328ce593000ef000008` to exclude by id
            created_at (date, optional): Filter by created at date using times
                in ISO8601 format Note: Range filters can be applied by adding
                a suffix: ‘.gt’, ‘.gte’, ‘.lt’, ‘lte’ (Example:
                expires_at.gte) + Examples     + `2017-01-01T00:00:00-00:00` -
                in ISO8601 format     + `1483228800` - Unix timestamps
            expires_at (date, optional): Range filters can be applied by
                adding a suffix: ‘.gt’, ‘.gte’, ‘.lt’, ‘lte’ (Example:
                created_at.gte) + Examples     + `2017-01-01T00:00:00-00:00` -
                in ISO8601 format     + `1483228800` - Unix timestamps
            transaction_id (string, optional): Filter by transaction
            transaction_type (string, optional): + Examples     + purchase    
                + subscription     + rental
            video_id (string, optional): Filter by video
            q (string, optional): Filter by keyword
            order (string, optional): Filter by ascending or descending order
                + Examples     + asc     + desc
            sort (string, optional): Sort on the specified field
            page (float, optional): Number of pages to return
            per_page (float, optional): Number of records to return per page

        Returns:
            list of VideoEntitlementMetadata: Response from the API.

        Raises:
            APIException: When an error occurs while fetching the data from
                the remote API. This exception includes the HTTP Response
                code, an error message, and the HTTP body that was received in
                the request.

        """

        # Prepare query URL
        _url_path = '/consumer/videos'
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_parameters = {
            'id': id,
            'created_at': created_at,
            'expires_at': expires_at,
            'transaction_id': transaction_id,
            'transaction_type': transaction_type,
            'video_id': video_id,
            'q': q,
            'order': order,
            'sort': sort,
            'page': page,
            'per_page': per_page
        }
        _query_builder = APIHelper.append_url_with_query_parameters(
            _query_builder,
            _query_parameters
        )
        _query_url = APIHelper.clean_url(_query_builder)

        # Prepare headers
        _headers = {
            'accept': 'application/json'
        }

        # Prepare and execute request
        _request = self.config.http_client.get(_query_url, headers=_headers)
        _response = self.execute_request(_request)
        self.validate_response(_response)

        decoded = APIHelper.json_deserialize(_response.text, VideoEntitlementMetadata.from_dictionary)

        return decoded

    def consumers_videos_by_consumer_id_get(self,
                                            consumer_id):
        """Does a GET request to /consumers/{consumer_id}/videos.

        List Video Entitlements

        Args:
            consumer_id (string): ID of the consumer whose video entitlements
                will be retrieved

        Returns:
            VideoEntitlementMetadata: Response from the API.

        Raises:
            APIException: When an error occurs while fetching the data from
                the remote API. This exception includes the HTTP Response
                code, an error message, and the HTTP body that was received in
                the request.

        """

        # Prepare query URL
        _url_path = '/consumers/{consumer_id}/videos'
        _url_path = APIHelper.append_url_with_template_parameters(_url_path, {
            'consumer_id': {'value': consumer_id, 'encode': True}
        })
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_url = APIHelper.clean_url(_query_builder)

        # Prepare headers
        _headers = {
            'accept': 'application/json'
        }

        # Prepare and execute request
        _request = self.config.http_client.get(_query_url, headers=_headers)
        _response = self.execute_request(_request)
        self.validate_response(_response)

        decoded = APIHelper.json_deserialize(_response.text, VideoEntitlementMetadata.from_dictionary)

        return decoded

    def consumers_videos_by_consumer_id_post(self,
                                             body,
                                             consumer_id):
        """Does a POST request to /consumers/{consumer_id}/videos.

        Create Video Entitlement

        Args:
            body (VideoEntitlementRequest): TODO: type description here.
            consumer_id (string): TODO: type description here.

        Returns:
            VideoEntitlementMetadata: Response from the API.

        Raises:
            APIException: When an error occurs while fetching the data from
                the remote API. This exception includes the HTTP Response
                code, an error message, and the HTTP body that was received in
                the request.

        """

        # Prepare query URL
        _url_path = '/consumers/{consumer_id}/videos'
        _url_path = APIHelper.append_url_with_template_parameters(_url_path, {
            'consumer_id': {'value': consumer_id, 'encode': True}
        })
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_url = APIHelper.clean_url(_query_builder)

        # Prepare headers
        _headers = {
            'accept': 'application/json',
            'content-type': 'application/json; charset=utf-8'
        }

        # Prepare and execute request
        _request = self.config.http_client.post(_query_url, headers=_headers, parameters=APIHelper.json_serialize(body))
        _response = self.execute_request(_request)
        self.validate_response(_response)

        decoded = APIHelper.json_deserialize(_response.text, VideoEntitlementMetadata.from_dictionary)

        return decoded

    def consumers_videos_by_consumer_id_and_id_get(self,
                                                   consumer_id,
                                                   id):
        """Does a GET request to /consumers/{consumer_id}/videos/{id}.

        View Video Entitlement

        Args:
            consumer_id (string): ID of the consumer whose video entitlements
                will be retrieved
            id (string): ID of the video entitlement

        Returns:
            VideoEntitlementMetadata: Response from the API.

        Raises:
            APIException: When an error occurs while fetching the data from
                the remote API. This exception includes the HTTP Response
                code, an error message, and the HTTP body that was received in
                the request.

        """

        # Prepare query URL
        _url_path = '/consumers/{consumer_id}/videos/{id}'
        _url_path = APIHelper.append_url_with_template_parameters(_url_path, {
            'consumer_id': {'value': consumer_id, 'encode': True},
            'id': {'value': id, 'encode': True}
        })
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_url = APIHelper.clean_url(_query_builder)

        # Prepare headers
        _headers = {
            'accept': 'application/json'
        }

        # Prepare and execute request
        _request = self.config.http_client.get(_query_url, headers=_headers)
        _response = self.execute_request(_request)
        self.validate_response(_response)

        decoded = APIHelper.json_deserialize(_response.text, VideoEntitlementMetadata.from_dictionary)

        return decoded

    def consumers_videos_by_consumer_id_and_id_put(self,
                                                   consumer_id,
                                                   id,
                                                   body):
        """Does a PUT request to /consumers/{consumer_id}/videos/{id}.

        Update Video Entitlement

        Args:
            consumer_id (string): ID of the consumer whose video entitlements
                will be retrieved
            id (string): ID of the video entitlement
            body (VideoEntitlementRequest): TODO: type description here.

        Returns:
            VideoEntitlementMetadata: Response from the API.

        Raises:
            APIException: When an error occurs while fetching the data from
                the remote API. This exception includes the HTTP Response
                code, an error message, and the HTTP body that was received in
                the request.

        """

        # Prepare query URL
        _url_path = '/consumers/{consumer_id}/videos/{id}'
        _url_path = APIHelper.append_url_with_template_parameters(_url_path, {
            'consumer_id': {'value': consumer_id, 'encode': True},
            'id': {'value': id, 'encode': True}
        })
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_url = APIHelper.clean_url(_query_builder)

        # Prepare headers
        _headers = {
            'accept': 'application/json',
            'content-type': 'application/json; charset=utf-8'
        }

        # Prepare and execute request
        _request = self.config.http_client.put(_query_url, headers=_headers, parameters=APIHelper.json_serialize(body))
        _response = self.execute_request(_request)
        self.validate_response(_response)

        decoded = APIHelper.json_deserialize(_response.text, VideoEntitlementMetadata.from_dictionary)

        return decoded

    def consumers_videos_by_consumer_id_and_id_delete(self,
                                                      consumer_id,
                                                      id):
        """Does a DELETE request to /consumers/{consumer_id}/videos/{id}.

        Delete a Video Entitlement

        Args:
            consumer_id (string): ID of the consumer whose video entitlements
                will be retrieved
            id (string): ID of the video entitlement

        Returns:
            void: Response from the API.

        Raises:
            APIException: When an error occurs while fetching the data from
                the remote API. This exception includes the HTTP Response
                code, an error message, and the HTTP body that was received in
                the request.

        """

        # Prepare query URL
        _url_path = '/consumers/{consumer_id}/videos/{id}'
        _url_path = APIHelper.append_url_with_template_parameters(_url_path, {
            'consumer_id': {'value': consumer_id, 'encode': True},
            'id': {'value': id, 'encode': True}
        })
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_url = APIHelper.clean_url(_query_builder)

        # Prepare and execute request
        _request = self.config.http_client.delete(_query_url)
        _response = self.execute_request(_request)
        self.validate_response(_response)

    def videos_entitled_by_id_get(self,
                                  id,
                                  access_token):
        """Does a GET request to /videos/{id}/entitled.

        Check Entitlement

        Args:
            id (string): ID of the video entitlement
            access_token (string): OAuth token of a consumer

        Returns:
            CheckEntitlementResponse: Response from the API.

        Raises:
            APIException: When an error occurs while fetching the data from
                the remote API. This exception includes the HTTP Response
                code, an error message, and the HTTP body that was received in
                the request.

        """

        # Prepare query URL
        _url_path = '/videos/{id}/entitled'
        _url_path = APIHelper.append_url_with_template_parameters(_url_path, {
            'id': {'value': id, 'encode': True}
        })
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_parameters = {
            'access_token': access_token
        }
        _query_builder = APIHelper.append_url_with_query_parameters(
            _query_builder,
            _query_parameters
        )
        _query_url = APIHelper.clean_url(_query_builder)

        # Prepare headers
        _headers = {
            'accept': 'application/json'
        }

        # Prepare and execute request
        _request = self.config.http_client.get(_query_url, headers=_headers)
        _response = self.execute_request(_request)
        self.validate_response(_response)

        decoded = APIHelper.json_deserialize(_response.text, CheckEntitlementResponse.from_dictionary)

        return decoded
