namespace TranslationLibrary.Resources
{
    using System;
    using System.ServiceModel;
    using System.ServiceModel.Channels;

    internal class Translate
    {
        private readonly LanguageServiceClient.LanguageServiceClient _client;

        public Translate()
        {
            Binding binding = new BasicHttpBinding
                                  {
                                      Name = "MyServicesSoap",
                                      CloseTimeout = TimeSpan.FromMinutes(1),
                                      OpenTimeout = TimeSpan.FromMinutes(1),
                                      ReceiveTimeout = TimeSpan.FromMinutes(10),
                                      SendTimeout = TimeSpan.FromMinutes(1)
                                  };

            const string endpoint = "http://api.microsofttranslator.com/V2/soap.svc";

            EndpointAddress endpointAddress = new EndpointAddress(endpoint);

            _client = new LanguageServiceClient.LanguageServiceClient(binding, endpointAddress);
        }

        /// <summary>
        /// Returns a tokenized AppID which can be used as the AppID parameter in any method
        /// </summary>
        /// <param name="appId">A string containing the Bing AppId.</param>
        /// <param name="minRatingRead">An int to define the minimum rating translations require to be returned. The recommended minRatingRead is 5 as this will only include automatic translations and authority approved translations.</param>
        /// <param name="maxRatingWrite">An int to define the maximum rating that a user can write with using this token. The recommended maxRatingWrite is from 1 to 4 for anonymous users, and from 6 to 10 for authoritative users whom you trust.</param>
        /// <param name="expireSeconds">An int that defines the duration in seconds from now that the token is valid. The value can be between 1 and 86400 (24 hours).</param>
        /// <returns>An appId token</returns>
        public string GetAppIdToken(string appId, int minRatingRead, int maxRatingWrite, int expireSeconds)
        {
            return _client.GetAppIdToken(appId, minRatingRead, maxRatingWrite, expireSeconds);
        }

        /// <summary>
        /// Translates a text string from one language to another.
        /// </summary>
        /// <param name="appId">A string containing the Bing AppID.</param>
        /// <param name="text">A string representing the text to translate.</param>
        /// <param name="from">A string representing the language code of the translation text. If left empty the response will include the result of language auto-detection.</param>
        /// <param name="to">A string representing the language code to translate the text into.</param>
        /// <param name="contentType">The format of the text being translated. The supported formats are "text/plain" and "text/html". Any HTML needs to be well-formed.</param>
        /// <param name="category">The category of the text to translate. The only supported category is "general".</param>
        /// <returns>A string of translated text</returns>
        public string TranslateText(
                                    string appId,
                                    string text,
                                    string from,
                                    string to,
                                    string contentType,
                                    string category)
        {
            return _client.Translate(appId, text, from, to, contentType, category);
        }

        /// <summary>
        /// Gets the translated speech.
        /// </summary>
        /// <param name="appId">A string containing the Bing AppID.</param>
        /// <param name="text">A string containing a sentence or sentences of the specified language to be spoken for the wave stream.</param>
        /// <param name="language">A string representing the supported language code to speak the text in. The code must be present in the list of codes returned from the method GetLanguagesForSpeak.</param>
        /// <param name="format">Optional. A string specifying the content-type ID. The default value is "audio/wav" which is the only currently allowed value.</param>
        /// <returns>Returns a string which is a URL to a wave stream of the passed-in text being spoken in the desired language.</returns>
        public string GetTranslatedSpeech(
                                            string appId,
                                            string text,
                                            string language,
                                            string format)
        {
            return _client.Speak(appId, text, language, format);
        }

        /// <summary>
        /// Gets the languages for translate.
        /// </summary>
        /// <param name="appId">The app id.</param>
        /// <returns>A array of translated languages</returns>
        public string[] GetLanguagesForTranslate(string appId)
        {
            return _client.GetLanguagesForTranslate(appId);
        }
    }
}
