using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Collections;

namespace FormulaEditor
{
	/// <summary>
	/// The simple symbol of math formula 
	/// </summary>
    [Serializable()]
	public class SimpleSymbol : MathSymbol
	{

		#region Fields

		/// <summary>
		/// Auxiliary variable
		/// </summary>
		protected string sb = "";


		/// <summary>
		/// Italic flag
		/// </summary>
		protected bool italic = true;

		/// <summary>
		/// Bold flag
		/// </summary>
		protected bool bold = true;

		#endregion

		#region Constructors

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="c">the symbol</param>
		public SimpleSymbol(char c) : base(c, 1)
		{
			sb += c;
			type = (byte)1;
			s = sb.Clone() as string;
		}

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="c">the symbol</param>
		/// <param name="type">the type</param>
		/// <param name="italic">the italic flag</param>
		public SimpleSymbol(char c, byte type, bool italic) : base(c, 1)
		{
		    this.type = type;
			this.italic = italic;
		}

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="c">The symbol</param>
		/// <param name="type">The type</param>
		/// <param name="italic">The italic flag</param>
		/// <param name="bold">The bold flag</param>
		public SimpleSymbol(char c, byte type, bool italic, bool bold) : this(c, type, italic)
		{
			this.bold = bold;
			s = c + "";
		}

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="c">the symbol</param>
		/// <param name="type">the type</param>
		/// <param name="italic">the italic flag</param>
		/// <param name="s">string representation</param>
		public SimpleSymbol(char c, byte type, bool italic, string s) : this(c, type, italic)
		{
			this.s = s;
		}

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="c"></param>
		/// <param name="type"></param>
		/// <param name="italic"></param>
		/// <param name="s"></param>
		public SimpleSymbol(char c, byte type, bool italic, bool bold, string s) : this(c, type, italic, s)
		{
			this.s = s;
			this.bold = bold;
		}



		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="c">the symbol</param>
		/// <param name="italic">the italic flag</param>
		/// <param name="type">the type</param>
		public SimpleSymbol(char c,  bool italic, byte type) : this(c, type, italic)
		{
			String str = "";
			str += c;
			string s = null;
			if (MathFormula.Resources != null)
			{
				s = MathFormula.Resources[str] as string;
			}
			if (s == null)
			{
				this.s = str;
			}
			else if (s.Length > 0)
			{
				this.s = s;
			}
			else
			{
				this.s = str;
			}
		}

        public SimpleSymbol(SimpleSymbol s)
            : this(s.symbol, s.type, s.italic, s.bold, s.s)
        {
        }

        public SimpleSymbol(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            sb = info.GetValue("Sb", typeof(string)) + "";
            italic = (bool)info.GetValue("Italic", typeof(bool));
            bold = (bool)info.GetValue("Bold", typeof(bool));

        }

		#endregion

		#region Overriden Members

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("Sb", sb, typeof(string));
            info.AddValue("Italic", italic, typeof(bool));
            info.AddValue("Bold", bold, typeof(bool));
        }

		/// <summary>
		/// Sets this object to formula
		/// </summary>
		/// <param name="formula">the formula to set</param>
		public override void SetToFormula(MathFormula formula)
		{
			base.SetToFormula(formula);
			if (italic)
			{
				//font = fontsItalic[level];
			}
			else
			{
				//font = fonts[level];
			}
			//widthInsert = (int)graphics.MeasureString("y", font, 100).Width;
			if (this is BracketsSymbol | this is BinarySymbol | this is BinaryFunctionSymbol)
			{
				return;
			}
			if (level < (sizes.Length - 1))
			{
				MathFormula child = new MathFormula((byte)(level + 1), sizes);
				children.Add(child);
				//childPositions = new Point[]{new Point()};
				return;
			}
			children = null;
		}

		/// <summary>
		/// ICloneable interface implementation
		/// </summary>
		/// <returns></returns>
		public override object Clone()
		{
			SimpleSymbol ss = new SimpleSymbol(symbol, type, italic, bold, s);
			ss.doubleValue = doubleValue;
			ss.ulongValue = ulongValue;
			return ss;
		}

		/// <summary>
		/// Checks whether the symbol is same
		/// </summary>
		/// <param name="sym">Symbol</param>
		/// <returns>True if same and false otherwise</returns>
		public override bool IsSame(MathSymbol sym)
		{
			if (!(sym is SimpleSymbol))
			{
				return false;
			}
			SimpleSymbol ss = sym as SimpleSymbol;
			return (bold == ss.bold) & (type == ss.type) & (ss.s.Equals(s));
		}


		#endregion

		#region Specific Members


		/// <summary>
		/// Italic flag
		/// </summary>
		public bool Italic
		{
			get
			{
				return italic;
			}
		}

		/// <summary>
		/// Bold flag
		/// </summary>
		public bool Bold
		{
			get
			{
				return bold;
			}
		}

		#endregion

	}
}
