/*-------------------------------------------------------------------------------------------------------
         * // ** FindAndReplaceDocument **
         * // This method implements the "v1/document/findandreplace" Web API call
         * //
         * // Parameters:
         * //  - FindAndReplaceBody FindAndReplaceBody
         * //  - string TemplateName (default = null)
         * //  - ReturnFormat ReturnFormat (default = PDF)
         * //
         * // Return value: A List of byte[]
         *-----------------------------------------------------------------------------------------------------*/
        /// <summary>
        /// This method replaces strings in a document.
        /// </summary>
        /// <param name="findAndReplaceBody">The FindAndReplaceBody object contains the replace data, optionally a template and merge settings.</param>
        /// <param name="templateName">The name of the template in the template storage.</param>
        /// <param name="returnFormat">The document format of the resulting document.</param>
        /// <param name="test">Specifies whether it is a test run or not. A test run is not counted against the quota and created documents contain a watermark.</param>
        public byte[] FindAndReplaceDocument(FindAndReplaceBody findAndReplaceBody,
                                             string templateName       = null,
                                             ReturnFormat returnFormat = ReturnFormat.PDF,
                                             bool test = false)
        {
            // create a new HttpClient using the Factory method CreateHttpClient
            using (HttpClient client = CreateHttpClient())
            {
                var sFindAndReplaceBody = JsonConvert.SerializeObject(findAndReplaceBody, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });

                // set the endpoint and pass the query paramaters
                // FindAndReplaceBody is posted as a JSON object
                HttpResponseMessage response = client.PostAsync("v1/document/findandreplace?templateName=" + templateName +
                                                                "&returnFormat=" + returnFormat.ToString() + "&test=" + test.ToString(),
                                                                new StringContent(sFindAndReplaceBody, Encoding.UTF8, "application/json")).Result;

                // if successful, return the document list
                if (response.IsSuccessStatusCode)
                {
                    return((byte[])JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result, typeof(byte[])));
                }
                else
                {
                    // throw exception with the message from the endpoint
                    throw new ArgumentException(response.Content.ReadAsStringAsync().Result);
                }
            }
        }
        /*-------------------------------------------------------------------------------------------------------
         * // ** MergeDocument **
         * // This method implements the "v1/document/merge" Web API call
         * //
         * // Parameters:
         * //  - MergeBody MergeBody
         * //  - string TemplateName (default = null)
         * //  - ReturnFormat ReturnFormat (default = PDF)
         * //  - bool Append (default = true)
         * //
         * // Return value: A List of byte[]
         *-----------------------------------------------------------------------------------------------------*/
        /// <summary>
        /// This method merges a template with data.
        /// </summary>
        /// <param name="mergeBody">The MergeBody object contains the data, optionally a template and merge settings.</param>
        /// <param name="templateName">The name of the template in the template storage.</param>
        /// <param name="returnFormat">The document format of the resulting document.</param>
        /// <param name="append">Specifies whether the resulting documents should be appended or not.</param>
        /// <param name="test">Specifies whether it is a test run or not. A test run is not counted against the quota and created documents contain a watermark.</param>
        public List <byte[]> MergeDocument(MergeBody mergeBody,
                                           string templateName       = null,
                                           ReturnFormat returnFormat = ReturnFormat.PDF,
                                           bool append = true,
                                           bool test   = false)
        {
            // create a new HttpClient using the Factory method CreateHttpClient
            using (HttpClient client = CreateHttpClient())
            {
                // set the endpoint and pass the query paramaters
                // MergeBody is posted as a JSON object
                HttpResponseMessage response = client.PostAsync("v1/document/merge?templateName=" + templateName +
                                                                "&returnFormat=" + returnFormat.ToString() +
                                                                "&append=" + append.ToString() + "&test=" + test.ToString(), mergeBody, formatter).Result;

                // if sucessful, return the image list
                if (response.IsSuccessStatusCode)
                {
                    List <byte[]> bResults = new List <byte[]>();

                    foreach (string sResult in response.Content.ReadAsAsync <List <string> >().Result)
                    {
                        bResults.Add(Convert.FromBase64String(sResult));
                    }

                    return(bResults);
                    //return response.Content.ReadAsAsync<List<string>>().Result;
                }
                else
                {
                    // throw exception with the message from the endpoint
                    throw new ArgumentException(response.Content.ReadAsStringAsync().Result);
                }
            }
        }
        /*-------------------------------------------------------------------------------------------------------
         * // ** FindAndReplaceDocument **
         * // This method implements the "v1/document/findandreplace" Web API call
         * //
         * // Parameters:
         * //  - FindAndReplaceBody FindAndReplaceBody
         * //  - string TemplateName (default = null)
         * //  - ReturnFormat ReturnFormat (default = PDF)
         * //
         * // Return value: A List of byte[]
         *-----------------------------------------------------------------------------------------------------*/
        /// <summary>
        /// This method replaces strings in a document.
        /// </summary>
        /// <param name="findAndReplaceBody">The FindAndReplaceBody object contains the replace data, optionally a template and merge settings.</param>
        /// <param name="templateName">The name of the template in the template storage.</param>
        /// <param name="returnFormat">The document format of the resulting document.</param>
        /// <param name="test">Specifies whether it is a test run or not. A test run is not counted against the quota and created documents contain a watermark.</param>
        public byte[] FindAndReplaceDocument(FindAndReplaceBody findAndReplaceBody,
                                             string templateName       = null,
                                             ReturnFormat returnFormat = ReturnFormat.PDF,
                                             bool test = false)
        {
            // create a new HttpClient using the Factory method CreateHttpClient
            using (HttpClient client = CreateHttpClient())
            {
                // set the endpoint and pass the query paramaters
                // FindAndReplaceBody is posted as a JSON object
                HttpResponseMessage response = client.PostAsync("v1/document/findandreplace?templateName=" + templateName +
                                                                "&returnFormat=" + returnFormat.ToString() + "&test=" + test.ToString(),
                                                                findAndReplaceBody, formatter).Result;

                // if successful, return the document list
                if (response.IsSuccessStatusCode)
                {
                    return(Convert.FromBase64String(response.Content.ReadAsAsync <string>().Result));
                }
                else
                {
                    // throw exception with the message from the endpoint
                    throw new ArgumentException(response.Content.ReadAsStringAsync().Result);
                }
            }
        }
        /*-------------------------------------------------------------------------------------------------------
         * // ** MergeDocument **
         * // This method implements the "v1/document/merge" Web API call
         * //
         * // Parameters:
         * //  - MergeBody MergeBody
         * //  - string TemplateName (default = null)
         * //  - ReturnFormat ReturnFormat (default = PDF)
         * //  - bool Append (default = true)
         * //
         * // Return value: A List of byte[]
         *-----------------------------------------------------------------------------------------------------*/
        /// <summary>
        /// This method merges a template with data.
        /// </summary>
        /// <param name="mergeBody">The MergeBody object contains the data, optionally a template and merge settings.</param>
        /// <param name="templateName">The name of the template in the template storage.</param>
        /// <param name="returnFormat">The document format of the resulting document.</param>
        /// <param name="append">Specifies whether the resulting documents should be appended or not.</param>
        /// <param name="test">Specifies whether it is a test run or not. A test run is not counted against the quota and created documents contain a watermark.</param>
        public List <byte[]> MergeDocument(MergeBody mergeBody,
                                           string templateName       = null,
                                           ReturnFormat returnFormat = ReturnFormat.PDF,
                                           bool append = true,
                                           bool test   = false)
        {
            // create a new HttpClient using the Factory method CreateHttpClient
            using (HttpClient client = CreateHttpClient())
            {
                var sMergeBody = JsonConvert.SerializeObject(mergeBody, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                });

                // set the endpoint and pass the query paramaters
                // MergeBody is posted as a JSON object
                HttpResponseMessage response = client.PostAsync("v1/document/merge?templateName=" + templateName +
                                                                "&returnFormat=" + returnFormat.ToString() +
                                                                "&append=" + append.ToString() + "&test=" + test.ToString(),
                                                                new StringContent(sMergeBody, Encoding.UTF8, "application/json")).Result;

                // if sucessful, return the image list
                if (response.IsSuccessStatusCode)
                {
                    List <byte[]> bResults = new List <byte[]>();

                    foreach (string sResult in (List <string>)JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result, typeof(List <string>)))
                    {
                        bResults.Add(Convert.FromBase64String(sResult));
                    }

                    return(bResults);
                    //return response.Content.ReadAsAsync<List<string>>().Result;
                }
                else
                {
                    // throw exception with the message from the endpoint
                    throw new ArgumentException(response.Content.ReadAsStringAsync().Result);
                }
            }
        }
        /*-------------------------------------------------------------------------------------------------------
         * // ** AppendDocument **
         * // This method implements the "v1/document/append" Web API call
         * //
         * // Parameters:
         * //  - AppendBody AppendBody
         * //  - ReturnFormat ReturnFormat (default = PDF)
         * //
         * // Return value: byte[]
         *-----------------------------------------------------------------------------------------------------*/
        /// <summary>
        /// This method merges a template with data.
        /// </summary>
        /// <param name="appendBody">The AppendBody object contains the documents for the append process.</param>
        /// <param name="returnFormat">The document format of the resulting document.</param>
        /// <param name="test">Specifies whether it is a test run or not. A test run is not counted against the quota and created documents contain a watermark.</param>
        public byte[] AppendDocument(AppendBody appendBody,
                                     ReturnFormat returnFormat = ReturnFormat.PDF,
                                     bool test = false)
        {
            // create a new HttpClient using the Factory method CreateHttpClient
            using (HttpClient client = CreateHttpClient())
            {
                var sAppendBody = JsonConvert.SerializeObject(appendBody, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                });

                // set the endpoint and pass the query paramaters
                // MergeBody is posted as a JSON object
                HttpResponseMessage response = client.PostAsync("v1/document/append?returnFormat=" + returnFormat.ToString() +
                                                                "&test=" + test.ToString(),
                                                                new StringContent(sAppendBody, Encoding.UTF8, "application/json")).Result;

                // if sucessful, return the image list
                if (response.IsSuccessStatusCode)
                {
                    string sResult = (string)JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result, typeof(string));
                    return(Convert.FromBase64String(sResult));
                }
                else
                {
                    // throw exception with the message from the endpoint
                    throw new ArgumentException(response.Content.ReadAsStringAsync().Result);
                }
            }
        }
        /*-------------------------------------------------------------------------------------------------------
         * // ** AppendDocument **
         * // This method implements the "v1/document/append" Web API call
         * //
         * // Parameters:
         * //  - AppendBody AppendBody
         * //  - ReturnFormat ReturnFormat (default = PDF)
         * //
         * // Return value: byte[]
         *-----------------------------------------------------------------------------------------------------*/
        /// <summary>
        /// This method appends documents to a single resulting document
        /// </summary>
        /// <param name="appendBody">The AppendBody object contains the documents for the append process.</param>
        /// <param name="returnFormat">The document format of the resulting document.</param>
        /// <param name="test">Specifies whether it is a test run or not. A test run is not counted against the quota and created documents contain a watermark.</param>
        public byte[] AppendDocument(AppendBody appendBody,
                                     ReturnFormat returnFormat = ReturnFormat.PDF,
                                     bool test = false)
        {
            // create a new HttpClient using the Factory method CreateHttpClient
            using (HttpClient client = CreateHttpClient())
            {
                // set the endpoint and pass the query paramaters
                // MergeBody is posted as a JSON object
                HttpResponseMessage response = client.PostAsync("v1/document/append?returnFormat=" + returnFormat.ToString() +
                                                                "&test=" + test.ToString(), appendBody, formatter).Result;

                // if sucessful, return the image list
                if (response.IsSuccessStatusCode)
                {
                    string sResult = response.Content.ReadAsAsync <string>().Result;
                    return(Convert.FromBase64String(sResult));
                }
                else
                {
                    // throw exception with the message from the endpoint
                    throw new ArgumentException(response.Content.ReadAsStringAsync().Result);
                }
            }
        }