/**
 * 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.
 */

package com.microsoft.azure.cognitiveservices.vision.contentmoderator.implementation;

import com.microsoft.azure.cognitiveservices.vision.contentmoderator.models.ScreenTextOptionalParameter;
import retrofit2.Retrofit;
import com.microsoft.azure.cognitiveservices.vision.contentmoderator.TextModerations;
import com.google.common.base.Joiner;
import com.google.common.reflect.TypeToken;
import com.microsoft.azure.cognitiveservices.vision.contentmoderator.models.APIErrorException;
import com.microsoft.azure.cognitiveservices.vision.contentmoderator.models.DetectedLanguage;
import com.microsoft.azure.cognitiveservices.vision.contentmoderator.models.Screen;
import com.microsoft.rest.ServiceCallback;
import com.microsoft.rest.ServiceFuture;
import com.microsoft.rest.ServiceResponse;
import java.io.IOException;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.POST;
import retrofit2.http.Query;
import retrofit2.Response;
import rx.functions.Func1;
import rx.Observable;

/**
 * An instance of this class provides access to all the operations defined
 * in TextModerations.
 */
public class TextModerationsImpl implements TextModerations {
    /** The Retrofit service to perform REST calls. */
    private TextModerationsService service;
    /** The service client containing this operation class. */
    private ContentModeratorClientImpl client;

    /**
     * Initializes an instance of TextModerationsImpl.
     *
     * @param retrofit the Retrofit instance built from a Retrofit Builder.
     * @param client the instance of the service client containing this operation class.
     */
    public TextModerationsImpl(Retrofit retrofit, ContentModeratorClientImpl client) {
        this.service = retrofit.create(TextModerationsService.class);
        this.client = client;
    }

    /**
     * The interface defining all the services for TextModerations to be
     * used by Retrofit to perform actually REST calls.
     */
    interface TextModerationsService {
        @Headers({ "Content-Type: text/plain", "x-ms-logging-context: com.microsoft.azure.cognitiveservices.vision.contentmoderator.TextModerations screenText" })
        @POST("contentmoderator/moderate/v1.0/ProcessText/Screen/")
        Observable<Response<ResponseBody>> screenText(@Query("language") String language, @Query("autocorrect") Boolean autocorrect, @Query("PII") Boolean pII, @Query("listId") String listId, @Query("classify") Boolean classify, @Header("Content-Type") String textContentType, @Body RequestBody textContent, @Header("accept-language") String acceptLanguage, @Header("x-ms-parameterized-host") String parameterizedHost, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: text/plain", "x-ms-logging-context: com.microsoft.azure.cognitiveservices.vision.contentmoderator.TextModerations detectLanguage" })
        @POST("contentmoderator/moderate/v1.0/ProcessText/DetectLanguage")
        Observable<Response<ResponseBody>> detectLanguage(@Header("Content-Type") String textContentType, @Body RequestBody textContent, @Header("accept-language") String acceptLanguage, @Header("x-ms-parameterized-host") String parameterizedHost, @Header("User-Agent") String userAgent);

    }


    /**
     * Detect profanity and match against custom and shared blacklists.
     * Detects profanity in more than 100 languages and match against custom and shared blacklists.
     *
     * @param textContentType The content type. Possible values include: 'text/plain', 'text/html', 'text/xml', 'text/markdown'
     * @param textContent Content to screen.
     * @param screenTextOptionalParameter the object representing the optional parameters to be set before calling this API
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws APIErrorException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the Screen object if successful.
     */
    public Screen screenText(String textContentType, byte[] textContent, ScreenTextOptionalParameter screenTextOptionalParameter) {
        return screenTextWithServiceResponseAsync(textContentType, textContent, screenTextOptionalParameter).toBlocking().single().body();
    }

    /**
     * Detect profanity and match against custom and shared blacklists.
     * Detects profanity in more than 100 languages and match against custom and shared blacklists.
     *
     * @param textContentType The content type. Possible values include: 'text/plain', 'text/html', 'text/xml', 'text/markdown'
     * @param textContent Content to screen.
     * @param screenTextOptionalParameter the object representing the optional parameters to be set before calling this API
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<Screen> screenTextAsync(String textContentType, byte[] textContent, ScreenTextOptionalParameter screenTextOptionalParameter, final ServiceCallback<Screen> serviceCallback) {
        return ServiceFuture.fromResponse(screenTextWithServiceResponseAsync(textContentType, textContent, screenTextOptionalParameter), serviceCallback);
    }

    /**
     * Detect profanity and match against custom and shared blacklists.
     * Detects profanity in more than 100 languages and match against custom and shared blacklists.
     *
     * @param textContentType The content type. Possible values include: 'text/plain', 'text/html', 'text/xml', 'text/markdown'
     * @param textContent Content to screen.
     * @param screenTextOptionalParameter the object representing the optional parameters to be set before calling this API
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the Screen object
     */
    public Observable<Screen> screenTextAsync(String textContentType, byte[] textContent, ScreenTextOptionalParameter screenTextOptionalParameter) {
        return screenTextWithServiceResponseAsync(textContentType, textContent, screenTextOptionalParameter).map(new Func1<ServiceResponse<Screen>, Screen>() {
            @Override
            public Screen call(ServiceResponse<Screen> response) {
                return response.body();
            }
        });
    }

    /**
     * Detect profanity and match against custom and shared blacklists.
     * Detects profanity in more than 100 languages and match against custom and shared blacklists.
     *
     * @param textContentType The content type. Possible values include: 'text/plain', 'text/html', 'text/xml', 'text/markdown'
     * @param textContent Content to screen.
     * @param screenTextOptionalParameter the object representing the optional parameters to be set before calling this API
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the Screen object
     */
    public Observable<ServiceResponse<Screen>> screenTextWithServiceResponseAsync(String textContentType, byte[] textContent, ScreenTextOptionalParameter screenTextOptionalParameter) {
        if (this.client.baseUrl() == null) {
            throw new IllegalArgumentException("Parameter this.client.baseUrl() is required and cannot be null.");
        }
        if (textContentType == null) {
            throw new IllegalArgumentException("Parameter textContentType is required and cannot be null.");
        }
        if (textContent == null) {
            throw new IllegalArgumentException("Parameter textContent is required and cannot be null.");
        }
        final String language = screenTextOptionalParameter != null ? screenTextOptionalParameter.language() : null;
        final Boolean autocorrect = screenTextOptionalParameter != null ? screenTextOptionalParameter.autocorrect() : null;
        final Boolean pII = screenTextOptionalParameter != null ? screenTextOptionalParameter.pII() : null;
        final String listId = screenTextOptionalParameter != null ? screenTextOptionalParameter.listId() : null;
        final Boolean classify = screenTextOptionalParameter != null ? screenTextOptionalParameter.classify() : null;

        return screenTextWithServiceResponseAsync(textContentType, textContent, language, autocorrect, pII, listId, classify);
    }

    /**
     * Detect profanity and match against custom and shared blacklists.
     * Detects profanity in more than 100 languages and match against custom and shared blacklists.
     *
     * @param textContentType The content type. Possible values include: 'text/plain', 'text/html', 'text/xml', 'text/markdown'
     * @param textContent Content to screen.
     * @param language Language of the text.
     * @param autocorrect Autocorrect text.
     * @param pII Detect personal identifiable information.
     * @param listId The list Id.
     * @param classify Classify input.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the Screen object
     */
    public Observable<ServiceResponse<Screen>> screenTextWithServiceResponseAsync(String textContentType, byte[] textContent, String language, Boolean autocorrect, Boolean pII, String listId, Boolean classify) {
        if (this.client.baseUrl() == null) {
            throw new IllegalArgumentException("Parameter this.client.baseUrl() is required and cannot be null.");
        }
        if (textContentType == null) {
            throw new IllegalArgumentException("Parameter textContentType is required and cannot be null.");
        }
        if (textContent == null) {
            throw new IllegalArgumentException("Parameter textContent is required and cannot be null.");
        }
        String parameterizedHost = Joiner.on(", ").join("{baseUrl}", this.client.baseUrl());
        RequestBody textContentConverted = RequestBody.create(MediaType.parse("text/plain"), textContent);
        return service.screenText(language, autocorrect, pII, listId, classify, textContentType, textContentConverted, this.client.acceptLanguage(), parameterizedHost, this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<Screen>>>() {
                @Override
                public Observable<ServiceResponse<Screen>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<Screen> clientResponse = screenTextDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<Screen> screenTextDelegate(Response<ResponseBody> response) throws APIErrorException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<Screen, APIErrorException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<Screen>() { }.getType())
                .registerError(APIErrorException.class)
                .build(response);
    }

    @Override
    public TextModerationsScreenTextParameters screenText() {
        return new TextModerationsScreenTextParameters(this);
    }

    /**
     * Internal class implementing TextModerationsScreenTextDefinition.
     */
    class TextModerationsScreenTextParameters implements TextModerationsScreenTextDefinition {
        private TextModerationsImpl parent;
        private String textContentType;
        private byte[] textContent;
        private String language;
        private Boolean autocorrect;
        private Boolean pII;
        private String listId;
        private Boolean classify;

        /**
         * Constructor.
         * @param parent the parent object.
         */
        TextModerationsScreenTextParameters(TextModerationsImpl parent) {
            this.parent = parent;
        }

        @Override
        public TextModerationsScreenTextParameters withTextContentType(String textContentType) {
            this.textContentType = textContentType;
            return this;
        }

        @Override
        public TextModerationsScreenTextParameters withTextContent(byte[] textContent) {
            this.textContent = textContent;
            return this;
        }

        @Override
        public TextModerationsScreenTextParameters withLanguage(String language) {
            this.language = language;
            return this;
        }

        @Override
        public TextModerationsScreenTextParameters withAutocorrect(Boolean autocorrect) {
            this.autocorrect = autocorrect;
            return this;
        }

        @Override
        public TextModerationsScreenTextParameters withPII(Boolean pII) {
            this.pII = pII;
            return this;
        }

        @Override
        public TextModerationsScreenTextParameters withListId(String listId) {
            this.listId = listId;
            return this;
        }

        @Override
        public TextModerationsScreenTextParameters withClassify(Boolean classify) {
            this.classify = classify;
            return this;
        }

        @Override
        public Screen execute() {
        return screenTextWithServiceResponseAsync(textContentType, textContent, language, autocorrect, pII, listId, classify).toBlocking().single().body();
    }

        @Override
        public Observable<Screen> executeAsync() {
            return screenTextWithServiceResponseAsync(textContentType, textContent, language, autocorrect, pII, listId, classify).map(new Func1<ServiceResponse<Screen>, Screen>() {
                @Override
                public Screen call(ServiceResponse<Screen> response) {
                    return response.body();
                }
            });
        }
    }

    /**
     * This operation will detect the language of given input content. Returns the &lt;a href="http://www-01.sil.org/iso639-3/codes.asp"&gt;ISO 639-3 code&lt;/a&gt; for the predominant language comprising the submitted text. Over 110 languages supported.
     *
     * @param textContentType The content type. Possible values include: 'text/plain', 'text/html', 'text/xml', 'text/markdown'
     * @param textContent Content to screen.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws APIErrorException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the DetectedLanguage object if successful.
     */
    public DetectedLanguage detectLanguage(String textContentType, byte[] textContent) {
        return detectLanguageWithServiceResponseAsync(textContentType, textContent).toBlocking().single().body();
    }

    /**
     * This operation will detect the language of given input content. Returns the &lt;a href="http://www-01.sil.org/iso639-3/codes.asp"&gt;ISO 639-3 code&lt;/a&gt; for the predominant language comprising the submitted text. Over 110 languages supported.
     *
     * @param textContentType The content type. Possible values include: 'text/plain', 'text/html', 'text/xml', 'text/markdown'
     * @param textContent Content to screen.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<DetectedLanguage> detectLanguageAsync(String textContentType, byte[] textContent, final ServiceCallback<DetectedLanguage> serviceCallback) {
        return ServiceFuture.fromResponse(detectLanguageWithServiceResponseAsync(textContentType, textContent), serviceCallback);
    }

    /**
     * This operation will detect the language of given input content. Returns the &lt;a href="http://www-01.sil.org/iso639-3/codes.asp"&gt;ISO 639-3 code&lt;/a&gt; for the predominant language comprising the submitted text. Over 110 languages supported.
     *
     * @param textContentType The content type. Possible values include: 'text/plain', 'text/html', 'text/xml', 'text/markdown'
     * @param textContent Content to screen.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the DetectedLanguage object
     */
    public Observable<DetectedLanguage> detectLanguageAsync(String textContentType, byte[] textContent) {
        return detectLanguageWithServiceResponseAsync(textContentType, textContent).map(new Func1<ServiceResponse<DetectedLanguage>, DetectedLanguage>() {
            @Override
            public DetectedLanguage call(ServiceResponse<DetectedLanguage> response) {
                return response.body();
            }
        });
    }

    /**
     * This operation will detect the language of given input content. Returns the &lt;a href="http://www-01.sil.org/iso639-3/codes.asp"&gt;ISO 639-3 code&lt;/a&gt; for the predominant language comprising the submitted text. Over 110 languages supported.
     *
     * @param textContentType The content type. Possible values include: 'text/plain', 'text/html', 'text/xml', 'text/markdown'
     * @param textContent Content to screen.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the DetectedLanguage object
     */
    public Observable<ServiceResponse<DetectedLanguage>> detectLanguageWithServiceResponseAsync(String textContentType, byte[] textContent) {
        if (this.client.baseUrl() == null) {
            throw new IllegalArgumentException("Parameter this.client.baseUrl() is required and cannot be null.");
        }
        if (textContentType == null) {
            throw new IllegalArgumentException("Parameter textContentType is required and cannot be null.");
        }
        if (textContent == null) {
            throw new IllegalArgumentException("Parameter textContent is required and cannot be null.");
        }
        String parameterizedHost = Joiner.on(", ").join("{baseUrl}", this.client.baseUrl());
        RequestBody textContentConverted = RequestBody.create(MediaType.parse("text/plain"), textContent);
        return service.detectLanguage(textContentType, textContentConverted, this.client.acceptLanguage(), parameterizedHost, this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<DetectedLanguage>>>() {
                @Override
                public Observable<ServiceResponse<DetectedLanguage>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<DetectedLanguage> clientResponse = detectLanguageDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<DetectedLanguage> detectLanguageDelegate(Response<ResponseBody> response) throws APIErrorException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<DetectedLanguage, APIErrorException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<DetectedLanguage>() { }.getType())
                .registerError(APIErrorException.class)
                .build(response);
    }

}
