# coding=utf-8
# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------
import functools
from typing import TYPE_CHECKING
import warnings

from azure.core.exceptions import (
    ClientAuthenticationError,
    HttpResponseError,
    ResourceExistsError,
    ResourceNotFoundError,
    map_error,
)
from azure.core.pipeline import PipelineResponse
from azure.core.pipeline.transport import HttpResponse
from azure.core.rest import HttpRequest

from .. import models as _models, rest

if TYPE_CHECKING:
    # pylint: disable=unused-import,ungrouped-imports
    from typing import Any, Callable, Dict, Generic, Optional, TypeVar

    T = TypeVar("T")
    ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]


class QuestionAnsweringClientOperationsMixin(object):
    def query_knowledgebase(
        self,
        knowledgebase_query_parameters,  # type: "_models.KnowledgebaseQueryParameters"
        **kwargs  # type: Any
    ):
        # type: (...) -> "_models.KnowledgebaseAnswers"
        """Answers the specified question using your knowledgebase.

        Answers the specified question using your knowledgebase.

        :keyword project_name: The name of the project to use.
        :paramtype project_name: str
        :param knowledgebase_query_parameters: Post body of the request.
        :type knowledgebase_query_parameters:
         ~azure.ai.language.questionanswering.models.KnowledgebaseQueryParameters
        :keyword deployment_name: The name of the specific deployment of the project to use.
        :paramtype deployment_name: str
        :keyword callable cls: A custom type or function that will be passed the direct response
        :return: KnowledgebaseAnswers, or the result of cls(response)
        :rtype: ~azure.ai.language.questionanswering.models.KnowledgebaseAnswers
        :raises: ~azure.core.exceptions.HttpResponseError
        """
        cls = kwargs.pop("cls", None)  # type: ClsType["_models.KnowledgebaseAnswers"]
        error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
        error_map.update(kwargs.pop("error_map", {}))

        content_type = kwargs.pop("content_type", "application/json")  # type: Optional[str]
        project_name = kwargs.pop("project_name")  # type: str
        deployment_name = kwargs.pop("deployment_name", None)  # type: Optional[str]

        json = self._serialize.body(knowledgebase_query_parameters, "object")

        request = rest.build_query_knowledgebase_request(
            project_name=project_name,
            deployment_name=deployment_name,
            json=json,
            content_type=content_type,
            template_url=self.query_knowledgebase.metadata["url"],
            **kwargs
        )
        path_format_arguments = {
            "Endpoint": self._serialize.url("self._config.endpoint", self._config.endpoint, "str", skip_quote=True),
        }
        request.url = self._client.format_url(request.url, **path_format_arguments)

        pipeline_response = self._client.send_request(request, stream=False, _return_pipeline_response=True, **kwargs)
        response = pipeline_response.http_response

        if response.status_code not in [200]:
            map_error(status_code=response.status_code, response=response, error_map=error_map)
            error = self._deserialize.failsafe_deserialize(_models.ErrorResponse, response)
            raise HttpResponseError(response=response, model=error)

        deserialized = self._deserialize("KnowledgebaseAnswers", pipeline_response)

        if cls:
            return cls(PipelineResponse._convert(pipeline_response), deserialized, {})

        return deserialized

    query_knowledgebase.metadata = {"url": "/:query-knowledgebases"}  # type: ignore

    def query_text(
        self,
        text_query_parameters,  # type: "_models.TextQueryParameters"
        **kwargs  # type: Any
    ):
        # type: (...) -> "_models.TextAnswers"
        """Answers the specified question using the provided text in the body.

        Answers the specified question using the provided text in the body.

        :param text_query_parameters: Post body of the request.
        :type text_query_parameters: ~azure.ai.language.questionanswering.models.TextQueryParameters
        :keyword callable cls: A custom type or function that will be passed the direct response
        :return: TextAnswers, or the result of cls(response)
        :rtype: ~azure.ai.language.questionanswering.models.TextAnswers
        :raises: ~azure.core.exceptions.HttpResponseError
        """
        cls = kwargs.pop("cls", None)  # type: ClsType["_models.TextAnswers"]
        error_map = {401: ClientAuthenticationError, 404: ResourceNotFoundError, 409: ResourceExistsError}
        error_map.update(kwargs.pop("error_map", {}))

        content_type = kwargs.pop("content_type", "application/json")  # type: Optional[str]

        json = self._serialize.body(text_query_parameters, "object")

        request = rest.build_query_text_request(
            json=json, content_type=content_type, template_url=self.query_text.metadata["url"], **kwargs
        )
        path_format_arguments = {
            "Endpoint": self._serialize.url("self._config.endpoint", self._config.endpoint, "str", skip_quote=True),
        }
        request.url = self._client.format_url(request.url, **path_format_arguments)

        pipeline_response = self._client.send_request(request, stream=False, _return_pipeline_response=True, **kwargs)
        response = pipeline_response.http_response

        if response.status_code not in [200]:
            map_error(status_code=response.status_code, response=response, error_map=error_map)
            error = self._deserialize.failsafe_deserialize(_models.ErrorResponse, response)
            raise HttpResponseError(response=response, model=error)

        deserialized = self._deserialize("TextAnswers", pipeline_response)

        if cls:
            return cls(PipelineResponse._convert(pipeline_response), deserialized, {})

        return deserialized

    query_text.metadata = {"url": "/:query-text"}  # type: ignore
