﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StringExtensions.cs" company="DevOpsGuys Ltd">
//   Copyright (c) DevOpsGuys Ltd. All rights reserved.
// </copyright>
// <summary>
//   Common string extension methods.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DevOpsGuys.Providers.Membership.Stormpath.Extensions
{
    using System;
    using System.Globalization;

    /// <summary>
    /// Common string extension methods.
    /// </summary>
    internal static class StringExtensions
    {
        /// <summary>
        /// Converts to T.
        /// </summary>
        /// <typeparam name="T">The type to convert to.</typeparam>
        /// <param name="value">The value.</param>
        /// <returns>The converted type.</returns>
        internal static T ConvertTo<T>(this string value) where T : IConvertible
        {
            return (T)Convert.ChangeType(value, typeof(T), CultureInfo.CurrentCulture);
        }

        /// <summary>
        /// Converts a string to base64.
        /// </summary>
        /// <param name="plainText">The plain text.</param>
        /// <returns>
        /// The string in base 64.
        /// </returns>
        internal static string ToBase64(this string plainText)
        {
            return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(plainText));
        }

        /// <summary>
        /// Extracts the account identifier.
        /// </summary>
        /// <param name="href">The HREF.</param>
        /// <returns>A cleaned account id.</returns>
        internal static string ExtractAccountId(this string href)
        {
            return href.Replace("https://api.stormpath.com/v1/accounts/", string.Empty);
        }

        /// <summary>
        /// Determines whether the specified string is blank.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <returns>Flag indicating if string is empty or not.</returns>
        internal static bool IsBlank(this string value)
        {
            return string.IsNullOrWhiteSpace(value);
        }
    }
}
