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

namespace dataMagus.Forms
{
	/// <summary>
	/// Generator form for creating source code.
	/// </summary>
	public partial class GeneratorForm : Form
	{
		#region Members
		/// <summary>
		/// Type (interface) of the generators to be loaded.
		/// </summary>
		private Type _generatorType = null;
		
		/// <summary>
		/// Current selected generator.
		/// </summary>
		private IGenerator _selectedGenerator = null;
		
		/// <summary>
		/// Current physical schema.
		/// </summary>
		private ISchema _schema = null;
		#endregion
		
		#region Constructors / Destructors
		/// <summary>
		/// Default constructor.
		/// </summary>
		public GeneratorForm()
		{
			InitializeComponent();
		}
		#endregion
		
		#region Properties
		/// <summary>
		/// Type (interface) of the generators to be loaded.
		/// </summary>
		public Type GeneratorType 
		{
			get { return _generatorType; }
			set { _generatorType = value; }
		}
		
		/// <summary>
		/// Current physical schema.
		/// </summary>
		public ISchema Schema 
		{
			get { return _schema; }
			set { _schema = value; }
		}
		#endregion
		
		#region Methodes
		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">EventArgs</param>
		void GenerateButtonClick(object sender, EventArgs e)
		{
            Cursor oldCursor = this.Cursor;
            try
            {
                this.Cursor = Cursors.WaitCursor;
                MemoryStream memStream = new MemoryStream();
                this._selectedGenerator.Schema = (IPhysicalSchema)((IPhysicalSchema)this._schema).Clone(); ;
                this._selectedGenerator.Generate(memStream);
                memStream.Seek(0, SeekOrigin.Begin);

                StreamReader reader = new StreamReader(memStream);
                this.sourceTextBox.Text = reader.ReadToEnd();
                this.tabControl.SelectedTab = this.sourceTab;
            }
            catch (Exception ex)
            {
                MessageForm.ShowDialog(ex);
            }
            finally
            {
                this.Cursor = oldCursor;
            }
		}
		
		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">EventArgs</param>
		void CloseButtonClick(object sender, EventArgs e)
		{
			try
			{
				this.Close();
			}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
		}
		
		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">EventArgs</param>
		void GeneratorComboSelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				if (this.generatorCombo.SelectedIndex != -1)
				{
					this._selectedGenerator = (IGenerator)this.generatorCombo.SelectedItem;
					
					// check if generator has options
					this.optionsTab.Controls.Clear();
					Control optionControl = this._selectedGenerator.GetGeneratorOptionDataControl();
					if (null != optionControl)
					{
						if (!this.tabControl.TabPages.Contains(this.optionsTab))
						{
							this.tabControl.TabPages.Add(this.optionsTab);
						}
						
						this.optionsTab.Controls.Add(optionControl);
						optionControl.Dock = DockStyle.Fill;
					}
					else
					{
						if (this.tabControl.TabPages.Contains(this.optionsTab))
						{
							this.tabControl.TabPages.Remove(this.optionsTab);
						}
					}
				}
			}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
		}
		
		/// <summary>
		/// Event handler.
		/// </summary>
		/// <param name="sender">Sender object.</param>
		/// <param name="e">EventArgs</param>
		void GeneratorFormLoad(object sender, EventArgs e)
		{
			try
			{
				if (null != this._generatorType)
				{
					List<IGenerator> generators = GeneratorManager.Instance.FindInterfaceType(this._generatorType);
					this.generatorCombo.DisplayMember = "Name";
					this.generatorCombo.DataSource = generators;
				}
				else
				{
					this.generatorCombo.DataSource = null;
				}
			}
			catch (Exception ex)
			{
				MessageForm.ShowDialog(ex);
			}
		}
		#endregion
	}
}
