
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ResultView
{
    /// <summary>
    /// generic method Extension
    /// </summary>
    public static class MethodExtensions
    {
        public static string ToString(this System.Drawing.Rectangle rect)
        {
            return string.Format("X={0},Y={1},W={2},H={3}", rect.X, rect.Y, rect.Width, rect.Height);
        }
        public static string ToString(this System.Drawing.RectangleF rect)
        {
            return string.Format("X={0},Y={1},W={2},H={3}", rect.X, rect.Y, rect.Width, rect.Height);
        }

        //public static void SetBGColor(this System.Windows.Forms.Label ctl,System.Drawing.Color color1)
        //{
        //    ctl.BackColor = System.Drawing.Color.Red;
        //}

        /// <summary>
        /// 0101이 반복되는 문자열 형태로 전환합니다.
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        public static string BitString(this System.Collections.BitArray arr)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = arr.Length; i > 0; i--)
                sb.Append(arr[i - 1] ? "1" : "0");
            return sb.ToString();
        }

        /// <summary>
        /// int 값으로 변환합니다.
        /// </summary>
        /// <param name="arr"></param>
        /// <returns></returns>
        public static int ValueI(this System.Collections.BitArray arr)
        {
            byte[] buf = new byte[4];
            arr.CopyTo(buf, 0);
            return BitConverter.ToInt32(buf, 0);
        }

        /// <summary>
        /// 숫자인지 검사합니다.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsNumeric(this string input)
        {
            double data;
            return double.TryParse(input, out data);
            //return Regex.IsMatch(input, @"^\d+$");
        }

        /// <summary>
        /// isnullorempty 를 수행합니다.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static Boolean isEmpty(this string input)
        {
            return string.IsNullOrEmpty(input);
        }

        /// <summary>
        /// default 인코딩을 사용하여 문자열로 반환합니다.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string GetString(this Byte[] input)
        {
            return System.Text.Encoding.Default.GetString(input);
        }

        /// <summary>
        /// 16진수 문자열 형태로 반환합니다.
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string GetHexString(this Byte[] input)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (byte b in input)
                sb.Append(" " + b.ToString("X2"));
            return sb.ToString();
        }

        public static string Base64Encode(this string src)
        {
            string base64enc = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(src));
            return base64enc;
        }
        public static string Base64Decode(this string src)
        {
            var base64dec = Convert.FromBase64String(src);
            return System.Text.Encoding.UTF8.GetString(base64dec);
        }
        
    }
}

