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

"""
instagramapi

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

from deprecation import deprecated
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.media_search_response import MediaSearchResponse
from instagramapi.models.media_entry_response import MediaEntryResponse


class MediaController(BaseController):

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

    @deprecated()
    def get_a_list_of_currently_popular_media(self):
        """Does a GET request to /media/popular.

        Get a list of what media is most popular at the moment. Can return mix
        of `image` and `video` types.
        **Warning:** [Deprecated](http://instagram.com/developer/changelog/)
        for Apps created **on or after** Nov 17, 2015

        Returns:
            MediaSearchResponse: Response from the API. Found media resources
                (without likes information).

        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('/media/popular')
            .http_method(HttpMethodEnum.GET)
            .header_param(Parameter()
                          .key('accept')
                          .value('application/json'))
            .auth(Or(Single('api_key'), Single('instagram_auth')))
        ).response(
            ResponseHandler()
            .deserializer(APIHelper.json_deserialize)
            .deserialize_into(MediaSearchResponse.from_dictionary)
        ).execute()

    def search_for_media_in_a_given_area(self,
                                         lat,
                                         lng,
                                         min_timestamp=None,
                                         max_timestamp=None,
                                         distance=None):
        """Does a GET request to /media/search.

        Search for media in a given area. The default time span is set to 5
        days. The time span must not exceed 7 days.
        Defaults time stamps cover the last 5 days. Can return mix of `image`
        and `video` types.

        Args:
            lat (float): Latitude of the center search coordinate. If used,
                `lng` is required.
            lng (float): Longitude of the center search coordinate. If used,
                `lat` is required.
            min_timestamp (int, optional): A unix timestamp. All media
                returned will be taken later than this timestamp.
            max_timestamp (int, optional): A unix timestamp. All media
                returned will be taken earlier than this timestamp.
            distance (int, optional): Default is 1km (distance=1000), max
                distance is 5km.

        Returns:
            MediaSearchResponse: Response from the API. Found media resources
                (without likes information) in a given area.

        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('/media/search')
            .http_method(HttpMethodEnum.GET)
            .query_param(Parameter()
                         .key('lat')
                         .value(lat))
            .query_param(Parameter()
                         .key('lng')
                         .value(lng))
            .query_param(Parameter()
                         .key('min_timestamp')
                         .value(min_timestamp))
            .query_param(Parameter()
                         .key('max_timestamp')
                         .value(max_timestamp))
            .query_param(Parameter()
                         .key('distance')
                         .value(distance))
            .header_param(Parameter()
                          .key('accept')
                          .value('application/json'))
            .auth(Or(Single('api_key'), Single('instagram_auth')))
        ).response(
            ResponseHandler()
            .deserializer(APIHelper.json_deserialize)
            .deserialize_into(MediaSearchResponse.from_dictionary)
        ).execute()

    def get_information_about_a_media_object(self,
                                             shortcode):
        """Does a GET request to /media/shortcode/{shortcode}.

        This endpoint returns the same response as `GET /media/{media-id}`.
        A media object's shortcode can be found in its shortlink URL. An
        example shortlink is
        `http://instagram.com/p/D/`, its corresponding shortcode is `D`.

        Args:
            shortcode (str): The short code of the media resource.

        Returns:
            MediaEntryResponse: Response from the API. Media resource
                information.

        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('/media/shortcode/{shortcode}')
            .http_method(HttpMethodEnum.GET)
            .template_param(Parameter()
                            .key('shortcode')
                            .value(shortcode)
                            .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(MediaEntryResponse.from_dictionary)
        ).execute()

    def get_information_about_a_media_object_1(self,
                                               media_id):
        """Does a GET request to /media/{media-id}.

        Get information about a media object. The returned type key will allow
        you to differentiate between image and
        video media.
        **Note:** if you authenticate with an OAuth Token, you will receive
        the user_has_liked key which quickly tells
        you whether the current user has liked this media item.

        Args:
            media_id (str): The ID of the media resource.

        Returns:
            MediaEntryResponse: Response from the API. Media resource
                information.

        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('/media/{media-id}')
            .http_method(HttpMethodEnum.GET)
            .template_param(Parameter()
                            .key('media-id')
                            .value(media_id)
                            .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(MediaEntryResponse.from_dictionary)
        ).execute()
