#region Copyright and GPL
/*************************************************************************
 * dataMagus - The open source database learning software
 * Copyright (C) 2007 by all authors
 * Authors: Monika Schwitter (monsy@gmx.ch)
 *          Jürg Zgraggen (j.zgraggen@ejaz.net)
 *          HSLU (Hochschule Luzern - Technik+Architektur)
 * 
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     This program 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 General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *************************************************************************/
#endregion
#region Using
using System;
using System.Globalization;
using System.Drawing;
using System.Windows.Forms;
#endregion

namespace dataMagus.Forms
{
	/// <summary>
	/// Description of MessageForm.
	/// </summary>
	public partial class MessageForm : Form
	{
		#region Enumerations
		/// <summary>
		/// Type of the message.
		/// </summary>
		/// <remarks>Influences the image shown and must correspond with the index on the list.</remarks>
		public enum MessageType
		{
			/// <summary>
			/// Information
			/// </summary>
			Information = 0,

			/// <summary>
			/// Succeeded
			/// </summary>
			Succeeded = 1,
			
			/// <summary>
			/// Warning
			/// </summary>
			Warning = 2,
			
			/// <summary>
			/// Error
			/// </summary>
			Error = 3
		}
		#endregion
		
		#region Constructors / Destructors
		/// <summary>
		/// Constructor.
		/// </summary>
		/// <param name="message">Message to be shown.</param>
		/// <param name="type">Type of the message.</param>
		/// <param name="exception">Exception to be shown.</param>
		/// <param name="data">Extended data to be shown.</param>
		protected MessageForm(string message, MessageType type, Exception exception, string data)
		{
			InitializeComponent();
			
			this.messageTextBox.Text = message;
			this.typePictureBox.Image = this.typeImageList.Images[(int)type];
			
			// remove unneeded tabs
			if (String.IsNullOrEmpty(data))
			{
				this.tabControl.TabPages.Remove(this.dataTab);
			}
			else
			{
				this.dataTextBox.Text = data;
			}
			
			if (null == exception)
			{
				this.tabControl.TabPages.Remove(this.exceptionTab);
			}
			else
			{
				this.exceptionTextBox.Text = CreateExceptionText(exception);
			}
		}
		#endregion
		
		#region Methodes
		/// <summary>
		/// Create exception text with stack trace and inner exceptions.
		/// </summary>
		/// <param name="ex">Exception object to be parsed.</param>
		/// <returns>Exception text to be shown.</returns>
		private string CreateExceptionText(Exception ex)
		{
			string innerMsgs = String.Empty;
			Exception innerEx = ex;
			while (null != innerEx)
			{
				innerMsgs += String.Format("Message:\r\n{0}\r\nStackTrace:\r\n{1}\r\n---", innerEx.Message, innerEx.StackTrace);
				innerEx = innerEx.InnerException;
			}
			return String.Format("Message:\r\n{0}\r\n\r\nStackTrace:\r\n{1}\r\n\r\nInnerExceptions:\r\n{2}", ex.Message, ex.StackTrace, innerMsgs);
		}
		
		/// <summary>
		/// Show message dialog.
		/// </summary>
		/// <param name="message">Message to be shown.</param>
		/// <param name="type">Type of the message.</param>
		public static void ShowDialog(string message, MessageType type)
		{
			MessageForm frm = new MessageForm(message, type, null, null);
			frm.ShowDialog();
		}
		
		/// <summary>
		/// Show message dialog with data tab.
		/// </summary>
		/// <param name="message">Message to be shown.</param>
		/// <param name="type">Type of the message.</param>
		/// <param name="data">Extended data to be shown.</param>
		public static void ShowDialog(string message, MessageType type, string data)
		{
			MessageForm frm = new MessageForm(message, type, null, data);
			frm.ShowDialog();
		}
		
		/// <summary>
		/// Show message dialog with exception tab.
		/// </summary>
		/// <param name="ex">Exception to be shown.</param>
		public static void ShowDialog(Exception ex)
		{
			MessageForm frm = new MessageForm(ex.Message, MessageType.Error, ex, null);
			frm.ShowDialog();
		}

		/// <summary>
		/// Show message dialog with exception tab.
		/// </summary>
		/// <param name="message">Message to be shown.</param>
		/// <param name="ex">Exception to be shown.</param>
		public static void ShowDialog(string message, Exception ex)
		{
			MessageForm frm = new MessageForm(message, MessageType.Error, ex, null);
			frm.ShowDialog();
		}

		/// <summary>
		/// Show message dialog with exception tab and data tab.
		/// </summary>
		/// <param name="message">Message to be shown.</param>
		/// <param name="ex">Exception to be shown.</param>
		/// <param name="data">Extended data to be shown.</param>
		public static void ShowDialog(string message, Exception ex, string data)
		{
			MessageForm frm = new MessageForm(message, MessageType.Error, ex, data);
			frm.ShowDialog();
		}

		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">EventArgs</param>
		private void OkButtonClick(object sender, EventArgs e)
		{
			this.Close();
		}
		#endregion
	}
}
