// -----------------------------------------------------------------------
//
//   Copyright (C) 2003-2006 Angel Marin
// 
//   This file is part of SharpMimeTools
//
//   SharpMimeTools is free software; you can redistribute it and/or
//   modify it under the terms of the GNU Lesser General Public
//   License as published by the Free Software Foundation; either
//   version 2.1 of the License, or (at your option) any later version.
//
//   SharpMimeTools is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//   Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public
//   License along with SharpMimeTools; if not, write to the Free Software
//   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// -----------------------------------------------------------------------
using System;
namespace anmar.SharpMimeTools
{
	/// 
	/// rfc 2822 header of a rfc 2045 entity
	/// 
	public class SharpMimeHeader : System.Collections.IEnumerable {
#if LOG
		private static log4net.ILog log  = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endif
		private static System.Text.Encoding default_encoding = System.Text.Encoding.ASCII;
		private anmar.SharpMimeTools.SharpMimeMessageStream message;
		private System.Collections.Specialized.HybridDictionary headers;
		private System.String _cached_headers = null;
		private long startpoint;
		private long endpoint;
		private long startbody;
		private struct HeaderInfo {
			public System.Collections.Specialized.StringDictionary contenttype;
			public System.Collections.Specialized.StringDictionary contentdisposition;
			public System.Collections.Specialized.StringDictionary contentlocation;
			public anmar.SharpMimeTools.MimeTopLevelMediaType TopLevelMediaType;
			public System.Text.Encoding enc;
			public System.String subtype;
			public HeaderInfo ( System.Collections.Specialized.HybridDictionary headers ) {
				this.TopLevelMediaType = new anmar.SharpMimeTools.MimeTopLevelMediaType();
				this.enc = null;
				try {
					this.contenttype = anmar.SharpMimeTools.SharpMimeTools.parseHeaderFieldBody ( "Content-Type", headers["Content-Type"].ToString() );
					this.TopLevelMediaType = (anmar.SharpMimeTools.MimeTopLevelMediaType)System.Enum.Parse(TopLevelMediaType.GetType(), this.contenttype["Content-Type"].Split('/')[0], true);
					this.subtype = this.contenttype["Content-Type"].Split('/')[1];
					this.enc = anmar.SharpMimeTools.SharpMimeTools.parseCharSet ( this.contenttype["charset"] );
				} catch (System.Exception) {
					this.enc = anmar.SharpMimeTools.SharpMimeHeader.default_encoding;
					this.contenttype = anmar.SharpMimeTools.SharpMimeTools.parseHeaderFieldBody ( "Content-Type", System.String.Concat("text/plain; charset=", this.enc.BodyName) );
					this.TopLevelMediaType = anmar.SharpMimeTools.MimeTopLevelMediaType.text;
					this.subtype = "plain";
				}
				if ( this.enc==null ) {
					this.enc = anmar.SharpMimeTools.SharpMimeHeader.default_encoding;
				}
				// TODO: rework this
				try {
					this.contentdisposition = anmar.SharpMimeTools.SharpMimeTools.parseHeaderFieldBody ( "Content-Disposition", headers["Content-Disposition"].ToString() );
				} catch ( System.Exception ) {
					this.contentdisposition = new System.Collections.Specialized.StringDictionary();
				}
				try {
					this.contentlocation = anmar.SharpMimeTools.SharpMimeTools.parseHeaderFieldBody ( "Content-Location", headers["Content-Location"].ToString() );
				} catch ( System.Exception ) {
					this.contentlocation = new System.Collections.Specialized.StringDictionary();
				}
			}
		}
		private HeaderInfo mt;
		internal SharpMimeHeader( anmar.SharpMimeTools.SharpMimeMessageStream message ) : this ( message, 0 ){}
		internal SharpMimeHeader(anmar.SharpMimeTools.SharpMimeMessageStream message, long startpoint) {
			this.startpoint = startpoint;
			this.message = message;
			if ( this.startpoint==0 ) {
				System.String line = this.message.ReadLine();
				// Perhaps there is part of the POP3 response
				if ( line!=null && line.Length>3 && line[0]=='+' && line[1]=='O' && line[2]=='K' ) {
#if LOG
					if ( log.IsDebugEnabled ) log.Debug ("+OK present at top of the message");
#endif
					this.startpoint = this.message.Position;
				} else this.message.ReadLine_Undo(line);
			}
			this.headers = new System.Collections.Specialized.HybridDictionary(2, true);
			this.parse();
		}
		/// 
		/// Initializes a new instance of the  class from a 
		/// 
		///  to read headers from
		public SharpMimeHeader( System.IO.Stream message ) : this( new anmar.SharpMimeTools.SharpMimeMessageStream (message), 0 ) {
		}
		/// 
		/// 
		/// 
		/// 
		public SharpMimeHeader( System.Byte[] message ) : this( new anmar.SharpMimeTools.SharpMimeMessageStream (message), 0 ) {
		}
		/// 
		/// Initializes a new instance of the  class from a  starting from the specified point
		/// 
		/// the  to read headers from
		/// initial point of the  where the headers start
		public SharpMimeHeader( System.IO.Stream message, long startpoint ) : this( new anmar.SharpMimeTools.SharpMimeMessageStream (message), startpoint ) {
		}
		/// 
		/// Gets header fields
		/// 
		/// field name
		/// Field names is case insentitive
		public System.String this[ System.Object name ] {
			get {
				return this.getProperty( name.ToString() );
			}
		}
		/// 
		/// 
		/// 
		/// 
		public void Close(){
			this._cached_headers = this.message.ReadLines( this.startpoint, this.endpoint );
			this.message.Close();
		}
		/// 
		/// 
		/// 
		/// 
		/// 
		public bool Contains ( System.String name ) {
			if ( this.headers==null )
				this.parse();
			return this.headers.Contains(name);
		}
		/// 
		/// Returns an enumerator that can iterate through the header fields
		/// 
		/// A  for the header fields
		public System.Collections.IEnumerator GetEnumerator() {
			return headers.GetEnumerator();
		}
		/// 
		/// Returns the requested header field body.
		/// 
		/// Header field name
		/// Value to return when the requested field is not present
		/// true to uncomment using ; false to return the value unchanged.
		/// true to decode ; false to return the value unchanged.
		/// Header field body
		public System.String GetHeaderField ( System.String name, System.String defaultvalue, bool uncomment, bool rfc2047decode ) {
			System.String tmp = this.getProperty(name);
			if ( tmp==null )
				tmp = defaultvalue;
			else {
				if ( uncomment )
					tmp = anmar.SharpMimeTools.SharpMimeTools.uncommentString(tmp);
				if ( rfc2047decode )
					tmp = anmar.SharpMimeTools.SharpMimeTools.rfc2047decode(tmp);
			}
			return tmp;
		}
		private System.String getProperty (  System.String name ) {
			System.String Value=null;
			name = name.ToLower();
			this.parse();
			if ( this.headers!=null && this.headers.Count > 0 && name!=null && name.Length>0 && this.headers.Contains(name) )
				Value = this.headers[name].ToString();
			return Value;
		}
		private bool parse () {
			bool error = false;
			if ( this.headers.Count>0 ) {
				return !error;
			}
			System.String line = System.String.Empty;
			this.message.SeekPoint ( this.startpoint );
			this.message.Encoding = anmar.SharpMimeTools.SharpMimeHeader.default_encoding;
			for ( line=this.message.ReadUnfoldedLine(); line!=null ; line=this.message.ReadUnfoldedLine() ) {
				if ( line.Length == 0 ) {
					this.endpoint = this.message.Position_preRead;
					this.startbody = this.message.Position;
					this.message.ReadLine_Undo(line);
					break;
				} else {
					String [] headerline = line.Split ( new Char[] {':'}, 2);
					if ( headerline.Length == 2 ) {
						headerline[1] = headerline[1].TrimStart(new Char[] {' '});
						if ( this.headers.Contains ( headerline[0]) ) {
							this.headers[headerline[0]] = System.String.Concat(this.headers[headerline[0]], "\r\n", headerline[1]);
						} else {
							this.headers.Add (headerline[0].ToLower(), headerline[1]);
						}
					}
				}
			}
			this.mt = new HeaderInfo ( this.headers );
			return !error;
		}
		/// 
		/// Gets the point where the headers end
		/// 
		/// Point where the headers end
		public long BodyPosition {
			get {
				return this.startbody;
			}
		}
		/// 
		/// Gets CC header field
		/// 
		/// CC header body
		public System.String Cc {
			get { return this.GetHeaderField("Cc", System.String.Empty, true, false); }
		}
		/// 
		/// Gets the number of header fields found
		/// 
		public int Count {
			get {
				return this.headers.Count;
			}
		}
		/// 
		/// Gets Content-Disposition header field
		/// 
		/// Content-Disposition header body
		public System.String ContentDisposition {
			get { return this.GetHeaderField("Content-Disposition", System.String.Empty, true, false); }
		}
		/// 
		/// Gets the elements found in the Content-Disposition header body
		/// 
		///  with the elements found in the header body
		public System.Collections.Specialized.StringDictionary ContentDispositionParameters {
			get {
				return this.mt.contentdisposition;
			}
		}
		/// 
		/// Gets Content-ID header field
		/// 
		/// Content-ID header body
		public System.String ContentID {
			get { return this.GetHeaderField("Content-ID", System.String.Empty, true, false); }
		}
		/// 
		/// Gets Content-Location header field
		/// 
		/// Content-Location header body
		public System.String ContentLocation {
			get { return this.GetHeaderField("Content-Location", System.String.Empty, true, false); }
		}
		/// 
		/// Gets the elements found in the Content-Location header body
		/// 
		///  with the elements found in the header body
		public System.Collections.Specialized.StringDictionary ContentLocationParameters {
			get {
				return this.mt.contentlocation;
			}
		}
		/// 
		/// Gets Content-Transfer-Encoding header field
		/// 
		/// Content-Transfer-Encoding header body
		public System.String ContentTransferEncoding {
			get {
				System.String tmp = this.GetHeaderField("Content-Transfer-Encoding", null, false, false);
				if ( tmp!=null ) {
					tmp = tmp.ToLower();
				}
				return tmp;
			}
		}
		/// 
		/// Gets Content-Type header field
		/// 
		/// Content-Type header body
		public System.String ContentType {
			get { return this.GetHeaderField("Content-Type", System.String.Concat("text/plain; charset=", this.mt.enc.BodyName), false, false); }
		}
		/// 
		/// Gets the elements found in the Content-Type header body
		/// 
		///  with the elements found in the header body
		public System.Collections.Specialized.StringDictionary ContentTypeParameters {
			get {
				return this.mt.contenttype;
			}
		}
		/// 
		/// Gets Date header field
		/// 
		/// Date header body
		public System.String Date {
			get { return this.GetHeaderField("Date", System.String.Empty, true, false); }
		}
		/// 
		/// Gets  found on the headers and applies to the body
		/// 
		///  for the body
		public System.Text.Encoding Encoding {
			get {
				this.parse();
				return this.mt.enc;
			}
		}
		/// 
		/// Gets or sets the default  used when it isn't defined otherwise.
		/// 
		/// The  used when it isn't defined otherwise
		/// The default value is  as defined in RFC 2045 section 5.2.
		/// If you change this value you'll be violating this rfc section.
		public static System.Text.Encoding EncodingDefault {
			get {return default_encoding; }
			set {
				if ( value!=null && !value.BodyName.Equals(System.String.Empty) )
					default_encoding=value;
				else
					default_encoding=System.Text.Encoding.ASCII;
			}
		}
		/// 
		/// Gets From header field
		/// 
		/// From header body
		public System.String From {
			get { return this.GetHeaderField("From", System.String.Empty, true, false); }
		}
		/// 
		/// Gets Raw headers
		/// 
		/// From header body
		public System.String RawHeaders {
			get {
				if ( this._cached_headers!=null )
					return this._cached_headers;
				else
					return this.message.ReadLines( this.startpoint, this.endpoint );
			}
		}
		/// 
		/// Gets Message-ID header field
		/// 
		/// Message-ID header body
		public System.String MessageID {
			get { return this.GetHeaderField("Message-ID", System.String.Empty, true, false); }
		}
		/// 
		/// Gets reply address as defined by rfc 2822
		/// 
		/// Reply address
		public System.String Reply {
			get {
				if ( !this.ReplyTo.Equals(System.String.Empty) )
					return this.ReplyTo;
				else
					return this.From;
			}
		}
		/// 
		/// Gets Reply-To header field
		/// 
		/// Reply-To header body
		public System.String ReplyTo {
			get { return this.GetHeaderField("Reply-To", System.String.Empty, true, false); }
		}
		/// 
		/// Gets Return-Path header field
		/// 
		/// Return-Path header body
		public System.String ReturnPath {
			get { return this.GetHeaderField("Return-Path", System.String.Empty, true, false); }
		}
		/// 
		/// Gets Sender header field
		/// 
		/// Sender header body
		public System.String Sender {
			get { return this.GetHeaderField("Sender", System.String.Empty, true, false); }
		}
		/// 
		/// Gets Subject header field
		/// 
		/// Subject header body
		public System.String Subject {
			get { return this.GetHeaderField("Subject", System.String.Empty, false, false); }
		}
		/// 
		/// Gets SubType from Content-Type header field
		/// 
		/// SubType from Content-Type header field
		public System.String SubType {
			get {
				this.parse();
				return this.mt.subtype;
			}
		}
		/// 
		/// Gets To header field
		/// 
		/// To header body
		public System.String To {
			get { return this.GetHeaderField("To", System.String.Empty, true, false); }
		}
		/// 
		/// Gets top-level media type from Content-Type header field
		/// 
		/// Top-level media type from Content-Type header field
		public anmar.SharpMimeTools.MimeTopLevelMediaType TopLevelMediaType {
			get {
				this.parse();
				return this.mt.TopLevelMediaType;
			}
		}
		/// 
		/// Gets Version header field
		/// 
		/// Version header body
		public System.String Version {
			get { return this.GetHeaderField("Version", "1.0", true, false); }
		}
	}
	/// 
	/// RFC 2046 Initial top-level media types
	/// 
	[Flags]
	public enum MimeTopLevelMediaType {
		/// 
		/// RFC 2046 section 4.1
		/// 
		text = 1,
		/// 
		/// RFC 2046 section 4.2
		/// 
		image = 2,
		/// 
		/// RFC 2046 section 4.3
		/// 
		audio = 4,
		/// 
		/// RFC 2046 section 4.4
		/// 
		video = 8,
		/// 
		/// RFC 2046 section 4.5
		/// 
		application = 16,
		/// 
		/// RFC 2046 section 5.1
		/// 
		multipart = 32,
		/// 
		/// RFC 2046 section 5.2
		/// 
		message = 64
	}
}