#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.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using dataMagus.Common.Physical;
#endregion

namespace dataMagus.Forms
{
	/// <summary>
	/// Form for having a user interaction during the generation process
	/// from a logical schema to a physical one.
	/// </summary>
	public partial class UserInteractionForm : Form, IUserInteraction
	{
		#region Members
		/// <summary>
		/// Answer the user has chosen.
		/// </summary>
		private object _answer = null;
		#endregion

		#region Constructors / Destructors
		/// <summary>
		/// Default constructor.
		/// </summary>
		public UserInteractionForm()
		{
			InitializeComponent();
		}
		#endregion

		#region Properties
		/// <summary>
		/// Answer the user has chosen.
		/// </summary>
		public object Answer 
		{
			get { return this._answer; }
		}
		
		/// <summary>
		/// Question the user will be asked.
		/// </summary>
		public string Question
		{
			get { return this.questionLabel.Text; }
			set { this.questionLabel.Text = value; }
		}

		/// <summary>
		/// Comment or hint for the user to solve the question.
		/// </summary>
		public string Comment
		{
			get { return this.commentLabel.Text; }
			set { this.commentLabel.Text = value; }
		}
		#endregion

		#region Methodes
		/// <summary>
		/// Add a new answer possability.
		/// </summary>
		/// <param name="answer">Answer text.</param>
		/// <param name="result">Result object when selecting this answer.</param>
		public void AddAnswer(string answer, object result)
		{
			Button answerButton = new Button();
			answerButton.Text = answer;
			answerButton.Tag = result;
			answerButton.ImageAlign = ContentAlignment.MiddleRight;
			answerButton.TextAlign = ContentAlignment.MiddleLeft;
			// set with a bit less because of possible scrollbar
			answerButton.Width = this.answerLayoutPanel.Width - 30;
			answerButton.Height = 40;
			answerButton.Click += new EventHandler(this.OnAnswerClick);
			this.answerLayoutPanel.Controls.Add(answerButton);
		}
		
		/// <summary>
		/// Clear all answers.
		/// </summary>
		public void ClearAnswers()
		{
			foreach (Button answerButton in this.answerLayoutPanel.Controls)
			{
				answerButton.Click -= new EventHandler(this.OnAnswerClick);
			}
			
			this.answerLayoutPanel.Controls.Clear();
		}
		
		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">EventArgs</param>
		private void OnAnswerClick(object sender, EventArgs e)
		{
			try
			{
				this._answer = ((Button)sender).Tag;
				this.Close();
			}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
		}
		
		/// <summary>
		/// Ask the question now by showing the dialog.
		/// </summary>
		/// <param name="title">Title of the dialog.</param>
		/// <param name="question">Question the user will be asked.</param>
		/// <param name="comment">Comment or hint for the user to solve the question.</param>
		/// <param name="allowedAnwers">Collection of allowed answers.</param>
		/// <returns>Answer the user has chosen.</returns>
		public int AskQuestion(string title, string question, string comment, Dictionary<int, string> allowedAnwers)
		{
			// kill previsous questions
			this.ClearAnswers();
			
			// set props
			this.Text = title;
			this.Question = question;
			this.Comment = comment;
			
			// set default answer and add answers
			this._answer = -1;
			Dictionary<int, string>.Enumerator answerItem = allowedAnwers.GetEnumerator();
			while (answerItem.MoveNext())
			{
				this.AddAnswer(answerItem.Current.Value, answerItem.Current.Key);
			}
			this.ShowDialog();
			
			return (int)this.Answer;
		}
		
		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">EventArgs</param>
		void UserInteractionFormLoad(object sender, EventArgs e)
		{
			try
			{
				if (string.Empty == this.Comment)
				{
					this.tabControl.TabPages.Remove(this.commentTab);
				}
			}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
		}
		#endregion		
	}
}
