//-----------------------------------------------------------------------
//  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 System.Windows.Forms;
using System.Drawing;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.NotificationServices.Rules;

namespace RulesControls
{
    // Control representing a condition component within the "AndConditionControl".
    public class ConditionPlaceholder : RulesUserControl<TableViewBase>
    {
        // Default constructor
        public ConditionPlaceholder()
            : base()
        {
            this.SuspendLayout();

            this.addRemoveButton = new Button();
            this.conditionControl = new SimpleLeafConditionControl();

            // this
            this.Size = new Size(MinimumWidth, FixedHeight);
            this.MinimumSize = this.Size;
            this.MaximumSize = new Size(Int16.MaxValue, FixedHeight);
            this.Controls.Add(this.addRemoveButton);
            this.Controls.Add(this.conditionControl);

            // addRemoveButton
            this.addRemoveButton.Location = new Point(0, ComponentMargin);
            this.addRemoveButton.Size = new Size(AddRemoveButtonWidth, ComponentHeight);
            this.addRemoveButton.Anchor = AnchorStyles.Left | AnchorStyles.Top;
            this.addRemoveButton.Click += new EventHandler(addRemoveButton_Click);

            // conditionControl
            this.conditionControl.Location = new Point(ComponentMargin + AddRemoveButtonWidth, 0);
            this.conditionControl.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
            this.Adding = false;

            this.ResumeLayout();
        }

        // Gets the condition built with this control.
        public Condition Condition
        {
            get
            {
                return this.conditionControl.Condition;
            }
        }

        // Event raised when this placeholder is activated.
        public event EventHandler Add;

        // Event raised when this placeholder is deactivated.
        public event EventHandler Delete;

        // Gets or sets the state of the placeholder. Value is true when the placeholder
        // is active.
        internal bool Adding
        {
            get { return this.adding; }
            set
            {
                this.adding = value;
                if (this.Adding)
                {
                    this.addRemoveButton.Text = "Delete";
                    this.conditionControl.Visible = true;
                    if (null != this.Add)
                    {
                        this.Add(this, new EventArgs());
                    }
                }
                else
                {
                    this.addRemoveButton.Text = "Add criteria";
                    this.conditionControl.Visible = false;
                    if (null != this.Delete)
                    {
                        this.Delete(this, new EventArgs());
                    }
                }
            }
        }

        protected override void UpdateParentContext()
        {
            this.conditionControl.ParentContext = this.ParentContext;
        }

        void addRemoveButton_Click(object sender, EventArgs e)
        {
            this.Adding = !this.Adding;
        }

        private SimpleLeafConditionControl conditionControl;
        private Button addRemoveButton;
        private bool adding;

        internal const int AddRemoveButtonWidth = 80;
        internal const int ComponentHeight = SimpleLeafConditionControl.ComponentHeight;
        internal const int ComponentMargin = SimpleLeafConditionControl.ComponentMargin;
        internal const int FixedHeight = SimpleLeafConditionControl.FixedHeight;
        internal const int MinimumWidth = SimpleLeafConditionControl.MinimumWidth + ComponentMargin + AddRemoveButtonWidth;
    }
}
