# -*- 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.device_pin import DevicePin
from zypev1.models.consumer_metadata import ConsumerMetadata


class DeviceLinkingController(BaseController):

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

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

    def pin_acquire_post(self,
                         linked_device_id,
                         mtype,
                         body):
        """Does a POST request to /pin/acquire.

        Acquire Device Pin

        Args:
            linked_device_id (string): Unique ID of the user's device
            mtype (string): type of Device to link to
            body (object): TODO: type description here.

        Returns:
            DevicePin: 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 = '/pin/acquire'
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_parameters = {
            'linked_device_id': linked_device_id,
            'type': mtype
        }
        _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',
            '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, DevicePin.from_dictionary)

        return decoded

    def pin_status_get(self,
                       linked_device_id):
        """Does a GET request to /pin/status.

        Retrieve Device Pin

        Args:
            linked_device_id (string): Unique ID of the user's device

        Returns:
            DevicePin: 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 = '/pin/status'
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_parameters = {
            'linked_device_id': linked_device_id
        }
        _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, DevicePin.from_dictionary)

        return decoded

    def pin_link_put(self,
                     consumer_id,
                     pin):
        """Does a PUT request to /pin/link.

        Link

        Args:
            consumer_id (string): ID of consumer to link device to
            pin (string): The value for the acquired device pin

        Returns:
            ConsumerMetadata: 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 = '/pin/link'
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_parameters = {
            'consumer_id': consumer_id,
            'pin': pin
        }
        _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.put(_query_url, headers=_headers)
        _response = self.execute_request(_request)
        self.validate_response(_response)

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

        return decoded

    def pin_unlink_put(self,
                       consumer_id,
                       pin):
        """Does a PUT request to /pin/unlink.

        Unlink

        Args:
            consumer_id (string): ID of consumer to link device to
            pin (string): The value for the acquired device pin

        Returns:
            ConsumerMetadata: 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 = '/pin/unlink'
        _query_builder = self.config.get_base_uri()
        _query_builder += _url_path
        _query_parameters = {
            'consumer_id': consumer_id,
            'pin': pin
        }
        _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.put(_query_url, headers=_headers)
        _response = self.execute_request(_request)
        self.validate_response(_response)

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

        return decoded
