//180917    chi makefilepath,MakeFTPPath 입력
//180705    chi GetHexStringNoSpace 다시 추가 ,UrlPathEncode 추가
//              getDateValue 추가                
//180625    chi GetHexStringNoSpace 삭제(이것은 util.cs로 이동)
//180614    chi Map 명령추가


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 FPJ0000
{

	public static class MethodExtensions
	{
		public static string MakeFilePath(this string value, params string[] param)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			sb.Append(value.Replace("/", "\\"));
			foreach (var item in param)
			{
				if (sb.Length > 0 && sb.ToString().EndsWith("\\") == false) sb.Append("\\");
				sb.Append(item.Replace("/", "\\"));
			}
			var retval = sb.ToString().Replace("/", "\\").Replace("\\\\", "\\");
			return retval.ToString();
		}


		public static string MakeFTPPath(this string value, params string[] param)
		{
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			sb.Append(value.Replace("\\", "/"));
			foreach (var item in param)
			{
				if (sb.Length > 0 && sb.ToString().EndsWith("/") == false) sb.Append("/");
				sb.Append(item.Replace("\\", "/"));
			}
			var retval = sb.ToString().Replace("//", "/");
			return retval.ToString();
		}


		public static double map(this double x, int in_min, int in_max, int out_min, int out_max)
		{
			return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
		}

		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);
		}


		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);
		}


		public static bool IsNumeric(this string input)
		{
			double data;
			input = input.Replace(",", "");
			return double.TryParse(input, out data);
			//return Regex.IsMatch(input, @"^\d+$");
		}
		public static int ToInt(this string input)
		{
			input = input.Replace(",", "");
			if (input.IsNumeric() == false) return 0;
			return int.Parse(input);
		}
		public static decimal ToDecimal(this string input)
		{
			input = input.Replace(",", "");
			if (input.IsNumeric() == false) return 0;
			return decimal.Parse(input);
		}
		public static double ToDouble(this string input)
		{
			input = input.Replace(",", "");
			if (input.IsNumeric() == false) return 0;
			return double.Parse(input);
		}


		public static Boolean isEmpty(this string input)
		{
			return string.IsNullOrEmpty(input);
		}

		public static Boolean isDate(this string input)
		{
			DateTime v;
			return DateTime.TryParse(input, out v);
		}

		/// <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();
		}
	}

}