using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.WizardFramework;
using Howest.SchemaDiscovery.UI.Controls;
using System.ComponentModel.Design;
using System.Configuration;
using SD = Howest.SchemaDiscovery.DbObjects;
using System.Collections;
using System.Windows.Forms.Design;

namespace Howest.Guidance.DataAccess.CustomWizardPages
{
	public partial class EntitiesTablesSelectorCP : CustomWizardPage
	{
		//private ObjectTree _tableTree;
		private string _conString;
		private ArrayList _tables;
		private ArrayList _views;

		public EntitiesTablesSelectorCP(WizardForm parent)
			: base(parent)
		{
			InitializeComponent();
		}
		

#region Arguments

		[RecipeArgument]
		public ArrayList Tables
		{
			get
			{
				return _tables;
			}
			set
			{
				_tables = value;
			}
		}

		[RecipeArgument]
		public ArrayList Views
		{
			get { return _views; }
			set { _views = value; }
		}
		
#endregion

		public override void OnActivated()
		{
			IDictionaryService dictservice = (IDictionaryService)GetService(typeof(IDictionaryService));
			ConnectionStringSettings connectionSettings = (ConnectionStringSettings)dictservice.GetValue("ConnectionSettings");

			//if (dictservice.GetValue("TablesTreeView") == null)
			if (dictservice.GetValue("Tables") == null)
			{
				//First Time
				try
				{
					tvwTableTree.Settings = connectionSettings;
					_conString = connectionSettings.ConnectionString;
				}
				catch (Exception ex)
				{
					IUIService service = (IUIService)GetService(typeof(IUIService));
					service.ShowError(ex);
				}
				
			}
			else
			{
				if (_conString != connectionSettings.ConnectionString)
				{
					try
					{
						//ConnectionString changed
						//RenderNodes();
						tvwTableTree.Settings = connectionSettings;
						_conString = connectionSettings.ConnectionString;
					}
					catch (Exception ex)
					{
						IUIService service = (IUIService)GetService(typeof(IUIService));
						service.ShowError(ex);
					}
					//dictservice.SetValue("TablesTreeView", null);
					dictservice.SetValue("Tables", null);
					dictservice.SetValue("Views", null);
					dictservice.SetValue("Entities", null);
				}
			}
			base.OnActivated();
		}

		public override bool IsDataValid
		{
			get
			{
				//bool flag = false;

				//if (_tableTree != null)
				//{
				//    flag = _tableTree.SelectedTables.Count > 0;
				//}
				//return flag;
				return tvwTableTree.SelectedTables.Count > 0 || tvwTableTree.SelectedViews.Count > 0;
			}
		}

		private void SaveArgument()
		{
			//_tableTree = tvwTableTree;
			IDictionaryService dictservice = (IDictionaryService)GetService(typeof(IDictionaryService));
			//dictservice.SetValue("TablesTreeView", _tableTree);
			List<SD.Table> tables = new List<SD.Table>(tvwTableTree.SelectedTables);
			List<SD.View> views = new List<SD.View>(tvwTableTree.SelectedViews);
			_tables = new ArrayList(tables);
			_views = new ArrayList(views);
			//dictservice.SetValue("Tables", _tableTree.SelectedTables);
			dictservice.SetValue("Tables", _tables);
			dictservice.SetValue("Views", _views);
		}

		private void tvwTableTree_Leave(object sender, EventArgs e)
		{
			SaveArgument();
		}

		private void tvwTableTree_MouseLeave(object sender, EventArgs e)
		{
			SaveArgument();
		}
	}
}
