using System;
using System.Collections;

namespace Municipalidad.Utils
{
	/// <summary>
	/// 
	/// </summary>
	public sealed class UtilsTools
	{
		private UtilsTools()
		{			
		}

		public static void EnabledPermisos(System.Web.UI.Page currentPage, CustomPrincipal principal, string findClase )
		{
			ArrayList permisos = new ArrayList();
			permisos = principal.FindObjetos(findClase);
			foreach (string permiso in permisos)
			{
				if (permiso != null)
				{
					string tempPermiso = permiso.Substring(permiso.IndexOf(".") + 1, permiso.Length - permiso.IndexOf(".") - 1);
					foreach (System.Web.UI.Control container in currentPage.Controls)
					{
						foreach (System.Web.UI.Control c in container.Controls)
						{						
							if (c is System.Web.UI.WebControls.WebControl)
							{
								if (((System.Web.UI.WebControls.WebControl)c).ID == tempPermiso )
									((System.Web.UI.WebControls.WebControl)c).Enabled = true;
							}
						}
					}
				}
			}
		}

		/// <summary>
		/// Limpia los textbox de un formulario
		/// </summary>
		/// <param name="rut">Rut sin digito verificador y sin formato</param>
		/// <returns>Digito verificador</returns>
		public static void ClearTextBox(System.Web.UI.Page pageClear)
		{
			foreach (System.Web.UI.Control container in pageClear.Controls)
			{
				foreach (System.Web.UI.Control c in container.Controls)
				{
					if (c is System.Web.UI.WebControls.TextBox)
					{
						((System.Web.UI.WebControls.TextBox)c).Text="";
					}
				}
			}
		}

		/// <summary>
		/// Valida el rut
		/// </summary>
		/// <param name="rut">Rut sin digito verificador y sin formato</param>
		/// <returns>Digito verificador</returns>
		public static string ValidateRut(int rut)
		{
			int i;
			int suma = 0;
			byte digito;
			byte factor = 2;			

			for(i = (rut.ToString().Length - 1); i >= 0; i--)
			{
				if (factor > 7) factor = 2;
				
				suma += byte.Parse(rut.ToString().Substring(i, 1)) * factor;
				factor++;			
			}

			digito = (byte)(11 - (suma % 11));
			
			switch (digito)
			{
				case 10:
					return "K";
				case 11:
					return "0";
				default:
					return digito.ToString();
			}
		}

		/// <summary>
		/// Valida el rut
		/// </summary>
		/// <param name="rut">Rut con digito verificador y sin formato</param>
		/// <returns>Boolean que indica si el rut es conrrecto o no</returns>		
		public static bool ValidateRut(string rut)
		{
			int i;
			int suma = 0;
			byte digito;
			byte factor = 2;
			
			string rutNumeric = rut.Substring(0, rut.Length - 1);
			string rutDigito = rut.Substring(rut.Length - 1);

			for(i = (rutNumeric.Length - 1); i >= 0; i--)
			{
				if (Convert.ToChar(rutNumeric[i]) < 48 || Convert.ToChar(rutNumeric[i]) > 57)
					return false;

				if (factor > 7) factor = 2;			

				suma += byte.Parse(rutNumeric[i].ToString()) * factor;
				factor++;			
			}

			digito = (byte)(11 - (suma % 11));
			
			if (digito >=1 && digito <= 9)
			{
				if (rutDigito == digito.ToString())
					return true;
				else
					return false;
			}

			if (digito == 10)
			{
				if (rutDigito.ToUpper() == "K")
					return true;
				else
					return false;
			}

			if (digito == 11)
			{
				if (rutDigito == "0")
					return true;
				else
					return false;
			}

			return false;			
		}



		#region numeros a palabras
		/// <summary>
		/// se recibe un numero para escribirlo en palabras
		/// </summary>
		/// <param name="Numero"></param>
		/// <returns></returns>
		public static string NumPalabra(double Numero )
		{
			string Numerofmt ;
			int centenas;
			int pos ;
			int cen ;
			int dec ;
			int uni ;
			string textuni ;
			string textdec ;
			string textcen ;
			string milestxt="" ;
			string monedatxt="" ;
			string txtPalabra ;

			Numerofmt = Numero.ToString ("0000000000000000.00");//Le da un formato fijo
			centenas = 1;
			pos = 1;
			txtPalabra = "";
			int I =1;
			for (I=1;I<= 5 ;I++) 
			{
				//	' extrae series de Centena, Decena, Unidad
				cen = int.Parse (Numerofmt.Substring(pos, 1));
				dec = int.Parse (Numerofmt.Substring(pos + 1, 1));
				uni = int.Parse (Numerofmt.Substring(pos + 2, 1));
					
				pos = pos + 3;
				textcen = Centena(uni, dec, cen);
				textdec = Decena(uni, dec);
				textuni = Unidad(uni, dec);
				//	' determina separador de miles/millones
				switch  (centenas)
				{
					case 1:
						if (cen + dec + uni == 1 )
						{
							milestxt = "Billon ";
						}
						else	if ( cen + dec + uni  > 1 )
						{ 
							milestxt = "Billones ";
						}
						break;
					case 2:
						string tempnumber = Numerofmt.Substring (7, 3);
						if ( cen + dec + uni >= 1 && int.Parse (tempnumber )== 0 ) 
						{
							milestxt = "Mil Millones ";
						}
						else if (cen + dec + uni >= 1) 
						{
							milestxt = "Mil ";
						}
						break;
					case 3:
						if (cen + dec == 0 && uni == 1) 
						{
							milestxt = "Millon ";
						}
						else if (cen > 0 || dec > 0 || uni > 1 )
						{
							milestxt = "Millones ";
						}
						break;	
					case 4:
						if ( cen + dec + uni >= 1)
						{
							milestxt = "Mil ";
						}
						break;
					case 5:
						if ( cen + dec + uni >= 1 )
						{
							milestxt = "";
						}
						break;
				}
				centenas++;
				//	'va formando el texto del importe en palabras
				txtPalabra = txtPalabra + textcen + textdec + textuni + milestxt;
				milestxt = "";
				textuni = "";
				textdec = "";
				textcen = "";

				if (txtPalabra.Length > 0 && txtPalabra.Length >6)
				{
						
					if (txtPalabra.Substring(0, 7) == "un Mil " )
					{
						txtPalabra = txtPalabra.Substring(2, txtPalabra.Length - 2);
					}
				}
			}

			txtPalabra = txtPalabra + monedatxt;
			return txtPalabra.ToUpper();
		}
			
		/// <summary>
		/// saca las centenas
		/// </summary>
		/// <param name="uni"></param>
		/// <param name="dec"></param>
		/// <param name="cen"></param>
		/// <returns></returns>
		private static string Centena(int uni , int dec ,int cen )
		{
			string cMontext="";
			switch (cen)
			{
				case 1:
					if (dec + uni == 0)
					{
						cMontext = "cien ";
					}
					else
					{
						cMontext = "ciento ";
					}
					break;
				case 2 : cMontext = "doscientos ";break;
				case 3 : cMontext = "trescientos ";break;
				case 4 : cMontext = "cuatrocientos ";break;
				case 5 : cMontext = "quinientos ";break;
				case 6 : cMontext = "seiscientos ";break;
				case 7 : cMontext = "setecientos ";break;
				case 8 : cMontext = "ochocientos ";break;
				case 9 : cMontext = "novecientos ";break;
				default : cMontext = "";
					cMontext = "";break;
			}
			return cMontext;
		}

		/// <summary>
		/// saca las decenas
		/// </summary>
		/// <param name="uni"></param>
		/// <param name="dec"></param>
		/// <returns></returns>
		private static  string Decena(int uni , int dec ) 
		{
			string cMontext="";
			switch (dec)
			{
				case 1:
				switch (uni)
				{
								
					case 0 : cMontext = "diez ";break;
					case 1 : cMontext = "once ";break;
					case 2 : cMontext = "doce ";break;
					case 3 : cMontext = "trece ";break;
					case 4 : cMontext = "catorce ";break;
					case 5 : cMontext = "quince ";break;
					case 6 : cMontext = "dieci";break;
					case 7 : cMontext = "dieci";break;
					case 8 : cMontext = "dieci";break;
					case 9 : cMontext = "dieci";break;
				}
					break;
				case 2:
					if( uni == 0) 
					{
						cMontext = "veinte ";
					}
					else if (uni > 0 )
					{
						cMontext = "veinti";
					}
					break;
				case 3 : cMontext = "treinta ";break;
				case 4 : cMontext = "cuarenta ";break;
				case 5 : cMontext = "cincuenta ";break;
				case 6 : cMontext = "sesenta ";break;
				case 7 : cMontext = "setenta ";break;
				case 8 : cMontext = "ochenta ";break;
				case 9 : cMontext = "noventa ";break;
				default: cMontext = "";break;
			}
			if (uni > 0 && dec > 2)
			{
				cMontext = cMontext + "y ";
			}
			return cMontext;
		}

		/// <summary>
		/// saca las unidades
		/// </summary>
		/// <param name="uni"></param>
		/// <param name="dec"></param>
		/// <returns></returns>
		private static  string Unidad(int uni , int dec )
		{
			string cMontext="";
			if (dec != 1)
			{
				switch (uni)
				{
					case 1 : cMontext = "un ";break;
					case 2 : cMontext = "dos ";break;
					case 3 : cMontext = "tres ";break;
					case 4 : cMontext = "cuatro ";break;
					case 5 : cMontext = "cinco ";break;
				}
			}
			switch (uni)
			{
				case 6 : cMontext = "seis ";break;
				case 7 : cMontext = "siete ";break;
				case 8 : cMontext = "ocho ";break;
				case 9 : cMontext = "nueve ";break;
			}
			return cMontext;
		}

		#endregion
	}
}
