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

"""
instagramapi

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

from instagramapi.api_helper import APIHelper
from instagramapi.configuration import Server
from instagramapi.controllers.base_controller import BaseController
from apimatic_core.request_builder import RequestBuilder
from apimatic_core.response_handler import ResponseHandler
from apimatic_core.types.parameter import Parameter
from instagramapi.http.http_method_enum import HttpMethodEnum
from apimatic_core.authentication.multiple.single_auth import Single
from apimatic_core.authentication.multiple.or_auth_group import Or
from instagramapi.models.tag_search_response import TagSearchResponse
from instagramapi.models.tag_info_response import TagInfoResponse
from instagramapi.models.tag_media_list_response import TagMediaListResponse


class TagsController(BaseController):

    """A Controller to access Endpoints in the instagramapi API."""
    def __init__(self, config):
        super(TagsController, self).__init__(config)

    def search_for_tags_by_name(self,
                                q):
        """Does a GET request to /tags/search.

        Search for tags by name.

        Args:
            q (str): A valid tag name without a leading \#. (eg. snowy,
                nofilter)

        Returns:
            TagSearchResponse: Response from the API. List of found tags and
                their statistics.

        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.

        """

        return super().new_api_call_builder.request(
            RequestBuilder().server(Server.DEFAULT)
            .path('/tags/search')
            .http_method(HttpMethodEnum.GET)
            .query_param(Parameter()
                         .key('q')
                         .value(q))
            .header_param(Parameter()
                          .key('accept')
                          .value('application/json'))
            .auth(Or(Single('api_key'), Single('instagram_auth')))
        ).response(
            ResponseHandler()
            .deserializer(APIHelper.json_deserialize)
            .deserialize_into(TagSearchResponse.from_dictionary)
        ).execute()

    def get_information_about_a_tag_object(self,
                                           tag_name):
        """Does a GET request to /tags/{tag-name}.

        Get information about a tag object.

        Args:
            tag_name (str): The tag name.

        Returns:
            TagInfoResponse: Response from the API. Tag information response.

        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.

        """

        return super().new_api_call_builder.request(
            RequestBuilder().server(Server.DEFAULT)
            .path('/tags/{tag-name}')
            .http_method(HttpMethodEnum.GET)
            .template_param(Parameter()
                            .key('tag-name')
                            .value(tag_name)
                            .should_encode(True))
            .header_param(Parameter()
                          .key('accept')
                          .value('application/json'))
            .auth(Or(Single('api_key'), Single('instagram_auth')))
        ).response(
            ResponseHandler()
            .deserializer(APIHelper.json_deserialize)
            .deserialize_into(TagInfoResponse.from_dictionary)
        ).execute()

    def get_a_list_of_recently_tagged_media(self,
                                            tag_name,
                                            count=None,
                                            min_tag_id=None,
                                            max_tag_id=None):
        """Does a GET request to /tags/{tag-name}/media/recent.

        Get a list of recently tagged media. Use the `max_tag_id` and
        `min_tag_id` parameters in the pagination
        response to paginate through these objects.

        Args:
            tag_name (str): The tag name.
            count (int, optional): Count of tagged media to return.
            min_tag_id (str, optional): Return media before this `min_tag_id`.
            max_tag_id (str, optional): Return media after this `max_tag_id`.

        Returns:
            TagMediaListResponse: Response from the API. List of media entries
                with this tag.

        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.

        """

        return super().new_api_call_builder.request(
            RequestBuilder().server(Server.DEFAULT)
            .path('/tags/{tag-name}/media/recent')
            .http_method(HttpMethodEnum.GET)
            .template_param(Parameter()
                            .key('tag-name')
                            .value(tag_name)
                            .should_encode(True))
            .query_param(Parameter()
                         .key('count')
                         .value(count))
            .query_param(Parameter()
                         .key('min_tag_id')
                         .value(min_tag_id))
            .query_param(Parameter()
                         .key('max_tag_id')
                         .value(max_tag_id))
            .header_param(Parameter()
                          .key('accept')
                          .value('application/json'))
            .auth(Or(Single('api_key'), Single('instagram_auth')))
        ).response(
            ResponseHandler()
            .deserializer(APIHelper.json_deserialize)
            .deserialize_into(TagMediaListResponse.from_dictionary)
        ).execute()
