﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net.Http.Headers;
using System.Text;
using CoddoTech.Portable.RestClient.Constants;
using Newtonsoft.Json;

namespace CoddoTech.Portable.RestClient.Client
{
    public static class RestClientConfig
    {
        /// <summary>
        /// Performs initialization of the configuration using default values
        /// </summary>
        static RestClientConfig()
        {
            AcceptHeaders = new Collection<MediaTypeWithQualityHeaderValue>();
            AcceptCharsetHeaders = new Collection<StringWithQualityHeaderValue>();
            AcceptEncodingHeaders = new Collection<StringWithQualityHeaderValue>();
            AcceptLanguageHeaders = new Collection<StringWithQualityHeaderValue>();

            // Accept json by default
            AcceptHeaders.Add(new MediaTypeWithQualityHeaderValue(HttpContentTypes.ApplicationJson));

            // Application defaults
            InitialRequestHeaders = new Collection<KeyValuePair<string, string>>();
            Encoding = Encoding.UTF8;
            ContentType = HttpContentTypes.ApplicationJson;
            Serializer = JsonConvert.SerializeObject;
            RequestTimeout = TimeSpan.FromSeconds(30);
        }

        #region Accept Headers

        public static ICollection<MediaTypeWithQualityHeaderValue> AcceptHeaders { get; set; }

        public static ICollection<StringWithQualityHeaderValue> AcceptCharsetHeaders { get; set; }

        public static ICollection<StringWithQualityHeaderValue> AcceptEncodingHeaders { get; set; }

        public static ICollection<StringWithQualityHeaderValue> AcceptLanguageHeaders { get; set; }

        #endregion

        #region Security

        /// <summary>
        /// Authorization header
        /// </summary>
        public static AuthenticationHeaderValue Authorization { get; set; }

        #endregion

        #region Request Defaults

        /// <summary>
        /// The default server url that should be used if not explicitely specified elsewhere
        /// </summary>
        public static string DefaultServerUrl { get; set; }

        /// <summary>
        /// Request headers that will be included by default in every request
        /// </summary>
        public static ICollection<KeyValuePair<string, string>> InitialRequestHeaders { get; set; }

        /// <summary>
        /// Encoding used for the request content
        /// </summary>
        public static Encoding Encoding { get; set; }

        /// <summary>
        /// Content type of the request body
        /// </summary>
        public static string ContentType { get; set; }

        /// <summary>
        /// Function used for serializing objects into strings (i.e. POCO to JSON)
        /// </summary>
        public static Func<object, string> Serializer { get; set; }

        /// <summary>
        /// Period of time after which the request fails automatically
        /// </summary>
        public static TimeSpan RequestTimeout { get; set; }

        #endregion

        #region Validation

        public static string UrlValidationRegex { get; set; } = @"^(http(s)?:\/\/)?[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-‌​\.\?\,\'\/\\\+&amp;%\$#_]*)?$";

        #endregion
    }
}
