using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Nmo;
using Microsoft.SqlServer.Management.Smo;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.SqlServer.NotificationServices;

namespace RulesControls
{
	public class SubscriptionControl : RulesUserControl<SubscriptionConditionEventRule>
	{
		TableViewBase input;
		NSInstance nsInstance;
		NSApplication nsApplication;
		string subscriptionClassName;
		string ruleName;

		public SubscriptionControl()
			: base()
		{
			this.SuspendLayout();

			subscriberIdLabel = new Label();
			subscriberIdValue = new TextBox();
			fieldValues = new List<TextBox>();
			fieldLabels = new List<Label>();
			addSubscription = new Button();
			andCondition = new AndConditionControl();
			addSubscriber = new Button();

			// this
			this.Size = new Size(AndConditionControl.MinimumWidth, AndConditionControl.MinimumHeight + 400);
			this.MinimumSize = this.Size;

			// subscriberIdLabel
			subscriberIdLabel.Text = "SubscriberId";
			subscriberIdLabel.Location = new Point(0, 0);
			subscriberIdLabel.Size = new Size(this.Size.Width / 2 - 8, 16);
			subscriberIdLabel.TextAlign = ContentAlignment.MiddleRight;
			Controls.Add(subscriberIdLabel);

			// subscriberIdValue
			subscriberIdValue.Text = String.Empty;
			subscriberIdValue.Location = new Point(this.Size.Width / 2, 0);
			subscriberIdValue.Size = new Size(this.Size.Width / 2 - 208, 16);
			Controls.Add(subscriberIdValue);

			// addSubscriber
			addSubscriber.Text = "Add subscriber";
			addSubscriber.Location = new Point(this.Size.Width - 200, 0);
			addSubscriber.Size = new Size(200, 24);
			addSubscriber.Click += new EventHandler(addSubscriber_Click);
			Controls.Add(addSubscriber);

			// addSubscription
			addSubscription.Text = "Add Subscription";
			addSubscription.Size = new Size(200, 24);
			addSubscription.Click += new EventHandler(addSubscription_Click);
			Controls.Add(addSubscription);

			// andCondition
			Controls.Add(andCondition);

			this.LayoutFields();

			this.ResumeLayout();
		}

		void addSubscriber_Click(object sender, EventArgs e)
		{
			try
			{
				Subscriber subscriber = new Subscriber(nsInstance);
				subscriber.SubscriberId = this.subscriberIdValue.Text;
				subscriber.Add();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString(), "Error adding subscriber");
			}
		}

		void addSubscription_Click(object sender, EventArgs e)
		{
			try
			{
				Subscription subscription = new Subscription(nsApplication, subscriptionClassName);
				for (int i = 0; i < fieldLabels.Count; i++)
				{
					subscription.SetFieldValue(fieldLabels[i].Text, fieldValues[i].Text);
				}
				subscription.RuleName = ruleName;
				subscription.Condition = andCondition.Condition;
				subscription.SubscriberId = subscriberIdValue.Text;
				subscription.Add();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.ToString(), "Error adding subscription");
			}
		}

		private void LayoutFields()
		{
			this.SuspendLayout();
			// remove any existing controls
			foreach (TextBox v in fieldValues)
			{
				Controls.Remove(v);
			}
			foreach (Label l in fieldLabels)
			{
				Controls.Remove(l);
			}

			fieldValues.Clear();
			fieldLabels.Clear();

			int y = 32;

			if (null != ParentContext)
			{
				foreach (SubscriptionField f in ParentContext.Parent.SubscriptionFields)
				{
					TextBox fieldValue = new TextBox();
					Label fieldLabel = new Label();

					// fieldLabel
					fieldLabel.Text = f.Name;
					fieldLabel.Location = new Point(0, y);
					fieldLabel.Size = new Size(this.Size.Width / 2 - 8, 16);
					fieldLabel.TextAlign = ContentAlignment.MiddleRight;
					fieldLabels.Add(fieldLabel);
					Controls.Add(fieldLabel);

					// fieldValue
					fieldValue.Text = String.Empty;
					fieldValue.Location = new Point(this.Size.Width / 2, y);
					fieldValue.Size = new Size(this.Size.Width / 2, 16);
					fieldValues.Add(fieldValue);
					Controls.Add(fieldValue);

					y += 20;
				}
			}

			y += 8;
			addSubscription.Location = new Point(0, y);
			y += addSubscription.Size.Height + 8;
			andCondition.Location = new Point(0, y);
			
			this.ResumeLayout();
		}

		protected override void UpdateParentContext()
		{
			// Get the server
			input = null;
			try
			{
				if (null != this.ParentContext)
				{
					Server server = this.ParentContext.Parent.Parent.Parent.Parent.Parent;

					string databaseName = this.ParentContext.Parent.Parent.DatabaseName;
					nsInstance = new NSInstance(this.ParentContext.Parent.Parent.Parent.Name);
					nsApplication = new NSApplication(nsInstance, this.ParentContext.Parent.Parent.Name);
					subscriptionClassName = this.ParentContext.Parent.Name;
					ruleName = this.ParentContext.Name;

					string schemaName = this.ParentContext.InputTypeSchema;
					string tvbName = this.ParentContext.InputTypeName;

					input = server.Databases[databaseName].Tables[tvbName, schemaName];
					if (null == input)
					{
						input = server.Databases[databaseName].Views[tvbName, schemaName];
					}
				}

				LayoutFields();
			}
			catch (Exception e)
			{
				MessageBox.Show(e.ToString());
			}

			// Update the children
			andCondition.ParentContext = input;
		}


		Label subscriberIdLabel;
		TextBox subscriberIdValue;
		List<TextBox> fieldValues;
		List<Label> fieldLabels;
		Button addSubscription;
		AndConditionControl andCondition;
		Button addSubscriber;
	}
}
