using System;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace KendoDateSample
{
    static public class StringExtensions
    {
        public static string FormatAsPhoneNumber(this string value)
        {
            if (string.IsNullOrEmpty(value))
                return value;

            long phone = 0;
            if (!long.TryParse(value, out phone))
                return value;

            string phoneraw = phone.ToString();
            string phoneFormatted = string.Empty;

            switch (phoneraw.Length)
            {
                case 10:
                    phoneFormatted = String.Format("{0:(###) ###-####}", phone);
                    break;
                case 11:
                    phoneFormatted = String.Format("{0:(###) ###-#### x#}", phone);
                    break;
                case 12:
                    phoneFormatted = String.Format("{0:(###) ###-#### x##}", phone);
                    break;
                case 13:
                    phoneFormatted = String.Format("{0:(###) ###-#### x###}", phone);
                    break;
                case 14:
                    phoneFormatted = String.Format("{0:(###) ###-#### x####}", phone);
                    break;
            }

            return phoneFormatted;
        }

        public static string EncryptPassword(this string value)
        {
            if (string.IsNullOrEmpty(value))
                return value;

            var encryption = new Encryption();
            return encryption.EncryptToString(value);
        }

        public static string EncryptToMD5(this string value)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            return BitConverter.ToString(md5.ComputeHash(Encoding.Default.GetBytes(value)));
        }

        public static string GetLast(this string value, int tail_length)
        {
            if (tail_length >= value.Length)
                return value;
            return value.Substring(value.Length - tail_length);
        }

        static public bool IsValidPassword(this string value)
        {
            if (string.IsNullOrEmpty(value))
                return false;

            string pattern = @"^.*(?=.{8,})(?=.*[\d]).*$";
            return Regex.IsMatch(value, pattern);
        }

        static public bool IsValidEmail(this string value)
        {
            if (string.IsNullOrEmpty(value))
                return false;

            string pattern = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
            bool isValid = Regex.IsMatch(value, pattern);

            if (isValid)
            {
                isValid = value.IsValidEmailExtension();
            }

            return isValid;
        }

        static public bool IsValidEmailExtension(this string value)
        {
            if (string.IsNullOrEmpty(value))
                return false;

            string[] validExtensions = new string[] 
            { 
                ".com",
                ".info",
                ".de",
                ".me",
                ".bz",
                ".net",
                ".biz",
                ".us",
                ".name",
                ".cc",
                ".co",
                ".tel",
                ".us.com",
                ".am",
                ".gs",
                ".org",
                ".eu",
                ".pro",
                ".uk.com",
                ".asia",
                ".mobi",
                ".co.uk",
                ".eu.com",
                ".de.com",
                ".xxx"
            };

            bool isValid = false;

            foreach (string validExtension in validExtensions)
            {
                string ext = value.GetLast(validExtension.Length);

                if (ext == validExtension)
                {
                    isValid = true;
                    break;
                }
            }

            return isValid;
        }
    }
}
