//-----------------------------------------------------------------------
//  This file is part of the Microsoft Code Samples.
// 
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// 
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation.  See these other
//materials for detailed information regarding Microsoft code samples.
// 
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------

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

namespace RulesControls
{
    // A control supporting the interactive construction of an "AndCondition".
    public class AndConditionControl : RulesUserControl<TableViewBase>
    {
        // Default constructor.
        public AndConditionControl()
            : base()
        {
            this.SuspendLayout();

            this.Size = new Size(MinimumWidth, MinimumHeight);
            this.MinimumSize = this.Size;

            this.AddCondition();
			this.conditions[0].Adding = true;

            this.ResumeLayout();

        }

        // Gets the condition built with this control.
        public Condition Condition
        {
            get
            {
                AndCondition condition = new AndCondition();

                foreach (ConditionPlaceholder c in this.conditions)
                {
                    // Only add conditions for placeholders that are active.
                    if (c.Adding)
                    {
                        condition.Children.Add(c.Condition);
                    }
                }

                // If the AndCondition has no children, add an "always true" condition
                if (0 == condition.Children.Count)
                {
                    condition.Children.Add(new BooleanLeafCondition(true));
                }

                return condition;
            }
        }

        // Add a new ConditionPlaceholder to the control.
        private void AddCondition()
        {
            ConditionPlaceholder condition = new ConditionPlaceholder();
            conditions.Add(condition);

            this.Controls.Add(condition);
            condition.Add += new EventHandler(OnAdd);
            condition.Delete += new EventHandler(OnDelete);
            condition.ParentContext = this.ParentContext;
            this.LayoutConditions();
        }

        // Handles activation of a component placeholder.
        private void OnAdd(object sender, EventArgs e)
        {
            this.SuspendLayout();

            this.AddCondition();

            this.ResumeLayout();
        }

        // Handles deactivation of a component placeholder.
        private void OnDelete(object sender, EventArgs e)
        {
            this.SuspendLayout();

            ConditionPlaceholder deleted = sender as ConditionPlaceholder;
            if (null != deleted)
            {
                conditions.Remove(deleted);
                this.Controls.Remove(deleted);
                this.LayoutConditions();
            }

            this.ResumeLayout();
        }

        // Performs layout of the components.
        private void LayoutConditions()
        {
            int y = 0;
            foreach (ConditionPlaceholder c in conditions)
            {
                c.Location = new Point(0, y);
                c.Width = this.Width;
                y += c.Height;
            }
            this.Height = y;
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            this.LayoutConditions();
        }

        protected override void UpdateParentContext()
        {
            foreach (ConditionPlaceholder c in this.conditions)
            {
                c.ParentContext = this.ParentContext;
            }
        }

        private List<ConditionPlaceholder> conditions = new List<ConditionPlaceholder>();
        internal const int MinimumHeight = ConditionPlaceholder.FixedHeight;
        internal const int MinimumWidth = ConditionPlaceholder.MinimumWidth;
    }
}
