using System.ComponentModel;
using System.Drawing.Design;
using Telerik.WinControls;

//using System.Linq;
using Telerik.WinControls.Data;

namespace Pilz.UI.Telerik.Controls.RadValidationProvider;

/// <summary>
/// RadCompositeValidationRule evaluates two or more RadValidationRules or RadValidationRuleWithTargetControl
/// and combines their with AND or OR operator.
/// </summary>
public class RadCompositeValidationRuleEx : CompositeFilterDescriptor, IRadValidationRuleEx
{
    private bool caseSensitive = false;
    private string toolTipText = "";
    private string toolTipTitle = "Validation Failed";
    private bool autoToolTip = true;

    /// <summary>
    /// Gets or Sets a collection with RadControl descendants that belongs this Rule.
    /// </summary>
    [Editor(DesignerConsts.RadValidationRuleAssociatedControlsEditorString, typeof(UITypeEditor))]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public List<Control> Controls
    {
        get
        {
            List<Control> controls = [];
            foreach (IRadValidationRuleEx validationRule in ValidationRules)
            {
                foreach (Control control in validationRule.Controls)
                {
                    if (!controls.Contains(control))
                    {
                        controls.Add(control);
                    }
                }
            }

            return controls;
        }
        set
        {
            foreach (RadControl control in value)
            {
                AddControl(control);
            }
        }
    }

    /// <summary>
    ///   <para>Associates this rule and all controls in ValidationRules collection with the specified RadControl descendant.</para>
    /// </summary>
    /// <param name="control">A RadControl descendant that represents the editor.</param>     

    public virtual void AddControl(RadControl control)
    {
        if (control == null)
        {
            return;
        }

        foreach (IRadValidationRuleEx validationRule in ValidationRules)
        {
            if (!validationRule.Controls.Contains(control))
            {
                validationRule.AddControl(control);
            }
        }
    }

    /// <summary>
    ///   <para>Removes the specified RadControl descendant from this rule and from all controls in ValidationRules collection .</para>
    /// </summary>
    /// <param name="control">A RadControl descendant that represents the editor.</param>   
    public void RemoveControl(RadControl control)
    {
        if (control == null)
        {
            return;
        }

        foreach (IRadValidationRuleEx validationRule in ValidationRules)
        {
            while (validationRule.Controls.Contains(control))
            {
                validationRule.RemoveControl(control);
            }
        }
    }


    /// <summary>
    /// Inherit property. Not used in RadCompositeValidationRule.
    /// </summary>
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    [Browsable(false)]
    public override FilterDescriptorCollection FilterDescriptors { get { return base.FilterDescriptors; } }

    /// <summary>
    /// Gets or Sets the collection of ValidationRules that belongs to this RadValidationProvider.
    /// </summary>
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
    Editor(DesignerConsts.RadValidationProviderCompositeConditionsCollectionEditorString, typeof(UITypeEditor)),
    Category(RadDesignCategory.DataCategory),
    Description("Gets a collection representing the Rules in this CompositeValidationRule.")]
    public FilterDescriptorCollection ValidationRules
    {
        get
        {
            return base.FilterDescriptors;
        }
    }

    /// <summary>
    /// Gets the Rule expression.
    /// </summary>
    /// <value>The Rule expression.</value>
    public override string Expression { get { return base.Expression; } }

    /// <summary>
    /// Gets or Sets the ToolTip Text.
    /// </summary>
    [DefaultValue("")]
    public string ToolTipText { get { return toolTipText; } set { toolTipText = value; } }

    /// <summary>
    /// Gets or Sets the ToolTip Title Text. This text will be shown as ToolTip Title text when rule validation fails.
    /// </summary>
    [DefaultValue("Validation Failed")]
    public string ToolTipTitle { get { return toolTipTitle; } set { toolTipTitle = value; } }

    /// <summary>
    /// Inherited property. Not used in the Rule.
    /// </summary>
    [EditorBrowsable(EditorBrowsableState.Never)]
    [Browsable(false)]
    public override bool IsFilterEditor
    {
        get { return base.IsFilterEditor; }
        set { base.IsFilterEditor = value; }
    }

    /// <summary>
    /// Enable or Disable the ToolTip when validation fails.
    /// </summary>
    [DefaultValue(true)]
    public bool AutoToolTip
    {
        get { return autoToolTip; }
        set { autoToolTip = value; }
    }

    /// <summary>
    /// Inherited property. Not used in the Rule.
    /// </summary>
    [Browsable(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override object Value { get { return base.Value; } set { base.Value = value; } }

    /// <summary>
    /// Enable or Disable the case sensitive Rule's Like operator.
    /// </summary>
    [DefaultValue(false)]
    public bool CaseSensitive
    {
        get
        {
            return caseSensitive;
        }
        set
        {
            caseSensitive = value;
        }
    }
}
