using System;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Drawing;
using System.Web.Security;
using PatNorSoft.Mail;
using PatNorSoft.Mail.Smtp;
using PatNorSoft.Mail.Message;
using PatNorSoft.Mail.Message.Build;

namespace SK.MemberWeb
{
	/// <summary>
	/// Utils 的摘要说明。
	/// </summary>
	public class Utils
	{
		public Utils()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}
		static public void SendMail(string to,string toName,string subject,string body) 
		{
			EmailMessage myMessage = new EmailMessage();			
			RecipientCollection recCollection = new RecipientCollection();			
			recCollection.Add(to,toName,RecipientType.To);
			myMessage.From.Email = Main.GetValue("SmtpEmail").ToString();
			myMessage.From.Name = to;
			myMessage.Recipients = recCollection;
			myMessage.Subject = subject;
			myMessage.BodyParts.Add(body, BodyPartFormat.HTML);
    		
			SMTP mySmtp = new SMTP(Main.GetValue("SmtpServer").ToString());
			if(Convert.ToBoolean(Main.GetValue("SMTPServerAuthLogin")))
			{
				mySmtp.SMTPServers[0].Account  = Main.GetValue("SmtpAccount").ToString();
				mySmtp.SMTPServers[0].Password = Main.GetValue("SmtpPassword").ToString();
				mySmtp.SMTPServers[0].AuthMode = SMTPAuthMode.AuthLogin;			
			}
			else
				mySmtp.SMTPServers[0].AuthMode = SMTPAuthMode.None;
			mySmtp.Connect();
			mySmtp.Send(myMessage);
			mySmtp.Disconnect(); 
		}
		static public void SendMail(string to,string toName,string subject,string body,BodyPartFormat part) 
		{
			EmailMessage myMessage = new EmailMessage();			
			RecipientCollection recCollection = new RecipientCollection();			
			recCollection.Add(to,toName,RecipientType.To);
			myMessage.From.Email = Main.GetValue("SmtpEmail").ToString();
			myMessage.From.Name = to;
			myMessage.Recipients = recCollection;
			myMessage.Subject = subject;
			myMessage.BodyParts.Add(body, part);
    		
			SMTP mySmtp = new SMTP(Main.GetValue("SmtpServer").ToString());
			if(Convert.ToBoolean(Main.GetValue("SMTPServerAuthLogin")))
			{
				mySmtp.SMTPServers[0].Account  = Main.GetValue("SmtpAccount").ToString();
				mySmtp.SMTPServers[0].Password = Main.GetValue("SmtpPassword").ToString();
				mySmtp.SMTPServers[0].AuthMode = SMTPAuthMode.AuthLogin;			
			}
			else
				mySmtp.SMTPServers[0].AuthMode = SMTPAuthMode.None;
			mySmtp.Connect();
			mySmtp.Send(myMessage);
			mySmtp.Disconnect(); 
		}
		static public void SendMail(DataRowCollection rows,string subject,string body) 
		{
			EmailMessage myMessage = new EmailMessage();			
			RecipientCollection recCollection = new RecipientCollection();			
			foreach(DataRow dr in rows)
			{
				recCollection.Add(dr["EMAIL"].ToString(),dr["CNAME"].ToString(),RecipientType.To);
			}
			myMessage.From.Email = Main.GetValue("SmtpEmail").ToString();		
			myMessage.From.Name = "下载系统";
			myMessage.Recipients = recCollection;
			myMessage.Subject = subject;
			myMessage.BodyParts.Add(body, BodyPartFormat.HTML);
    		
			SMTP mySmtp = new SMTP(Main.GetValue("SmtpServer").ToString());
			if(Convert.ToBoolean(Main.GetValue("SMTPServerAuthLogin")))
			{
				mySmtp.SMTPServers[0].Account  = Main.GetValue("SmtpAccount").ToString();
				mySmtp.SMTPServers[0].Password = Main.GetValue("SmtpPassword").ToString();
				mySmtp.SMTPServers[0].AuthMode = SMTPAuthMode.AuthLogin;					
			}
			else
				mySmtp.SMTPServers[0].AuthMode = SMTPAuthMode.None;
			mySmtp.Connect();
			mySmtp.Send(myMessage);
			mySmtp.Disconnect(); 
		}
		static public bool IsValidEmail(string email)
		{
			return Regex.IsMatch(email,@"^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$");
		}
		static public bool IsValidURL(string url)
		{
			return Regex.IsMatch(url,@"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$");
		}
		static public bool IsValidInt(string val)
		{
			return Regex.IsMatch(val,@"^[1-9]\d*\.?[0]*$");
		}
		static public void SaveTemplate(string source,string targetfile)
		{
			using (StreamWriter sw = new StreamWriter(targetfile,false,Encoding.UTF8)) 
			{
				sw.Write(source);
				sw.Close();
			}

		}
		static public bool IsNum(string val)
		{
			try
			{
				Convert.ToDouble(val);
				return true;
			}
			catch
			{
				return false;
			}
		}
		static public Size ThumbSize(Size Initial,ref bool needResize,int ImgWidth,int ImgHeight)
		{
			Size size = new Size(Initial.Width, Initial.Height);
			Size standardsize=new Size(ImgWidth,ImgHeight);
			//return ScaleDimensions(Initial,size,ref needResize);
			if(standardsize.Width>=size.Width & standardsize.Height>=size.Height)
			{
				needResize=false;
			}
			else
			{
				if (Initial.Width > Initial.Height)
				{
					size.Width = standardsize.Width;
					size.Height = (int)((double)size.Width * ((double)Initial.Height / (double)Initial.Width));
					if(size.Height>standardsize.Height)
					{
						size.Height = standardsize.Height;
						size.Width = (int)((double)size.Height * ((double)Initial.Width / (double)Initial.Height));
					}

				}
				else
				{
					size.Height = standardsize.Height;
					size.Width = (int)((double)size.Height * ((double)Initial.Width / (double)Initial.Height));
					if(size.Width>standardsize.Width)
					{
						size.Width = standardsize.Width;
						size.Height = (int)((double)size.Width * ((double)Initial.Height / (double)Initial.Width));
					}
				}
				needResize=true;
			}
			return size;
		}
		static public System.Drawing.Imaging.ImageFormat GetImageType(object strContentType) 
		{ 
			System.Drawing.Imaging.ImageFormat fmt;
			switch(strContentType.ToString().ToLower())
			{
				case "image/pjpeg":
					fmt=System.Drawing.Imaging.ImageFormat.Jpeg;
					break;
				case "image/gif":
					fmt=System.Drawing.Imaging.ImageFormat.Gif;
					break;
				case "image/bmp":
					fmt=System.Drawing.Imaging.ImageFormat.Bmp;
					break;
				case "image/tiff":
					fmt=System.Drawing.Imaging.ImageFormat.Tiff;
					break;
				case "image/x-icon":
					fmt=System.Drawing.Imaging.ImageFormat.Icon;
					break;
				case "image/x-png":
					fmt=System.Drawing.Imaging.ImageFormat.Png;
					break;
				case "image/x-emf":
					fmt=System.Drawing.Imaging.ImageFormat.Emf;
					break;
				case "image/x-exif":
					fmt=System.Drawing.Imaging.ImageFormat.Exif;
					break;
				case "image/x-wmf":
					fmt=System.Drawing.Imaging.ImageFormat.Wmf;
					break;
				default:
					fmt=System.Drawing.Imaging.ImageFormat.MemoryBmp;
					break;
			}
			return fmt;
		
		}
		static public string CreateRndStr(int length) 
		{
			string valid = "0123456789";
			string res = "";
			Random rnd = new Random();
			while(0<length--)
				res += valid[rnd.Next(valid.Length)];
			return res;
		}
		static public void Logout()
		{
			FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
				"aaa",
				DateTime.Now,
				DateTime.Now.AddMinutes(-20),
				true,
				"User",
				FormsAuthentication.FormsCookiePath);
			string hash = FormsAuthentication.Encrypt(ticket);
			HttpCookie cookie = new HttpCookie(
				FormsAuthentication.FormsCookieName,
				hash); 

			if (ticket.IsPersistent) cookie.Expires = DateTime.Now.AddMinutes(-20);

			System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
		}
		static public string Weekday2Upper(int i)
		{
			string str = "";
			switch(i)
			{
				case 0:
					str = "日";
					break;
				case 1:
					str = "一";
					break;
				case 2:
					str = "二";
					break;
				case 3:
					str = "三";
					break;
				case 4:
					str = "四";
					break;
				case 5:
					str = "五";
					break;
				case 6:
					str = "六";
					break;
				
			}
			return str;
		}
		static public string Int2Upper(int i)
		{
			string str = "";
			switch(i)
			{
				case 0:
					str = "零";
					break;
				case 1:
					str = "一";
					break;
				case 2:
					str = "二";
					break;
				case 3:
					str = "三";
					break;
				case 4:
					str = "四";
					break;
				case 5:
					str = "五";
					break;
				case 6:
					str = "六";
					break;
				case 7:
					str = "七";
					break;
				case 8:
					str = "八";
					break;
				case 9:
					str = "九";
					break;
				case 10:
					str = "十";
					break;
				default:
					str = i.ToString();
					break;
			}
			return str;
		}
		static public string  SubString(string Str,int Length)
		{
			//判断字符串长度是否大于截断长度
			if(System.Text.Encoding.Default.GetByteCount(Str) > Length)
			{
				//判断字串是否为空
				if(Str == null)
				{
					return "";
				}

				//初始化
				int i = 0, j = 0;

				//为汉字或全脚符号长度加2否则加1
				foreach (char Char in Str)
				{
					if ((int)Char > 127)
					{
						i += 2;
					}
					else
					{
						i ++;
					}
					if (i > Length)
					{
						Str = Str.Substring(0, j) + "...";
						break;
					}
					j ++;
				}
			}
			return Str;
		}
		static public bool IsValidDate(string val)
		{
			try
			{
				Convert.ToDateTime(val);
				return true;
			}
			catch
			{
				return false;
			}
		}
		static public float Multiply(object f1,object f2)
		{
			return (float)(Convert.ToDouble(f1)*Convert.ToDouble(f2));
		}
		
	}
}
