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

namespace Pilz.UI.Telerik.Controls.RadValidationProvider;


/// <summary>
/// RadValidationRuleWithTargetControl provides a validation logic which compares RadControl's Property with TargetControl's property.
/// </summary>
public class RadValidationRuleWithTargetControlEx : RadValidationRuleEx
{
    private string sourceControlPropertyName = "Text";

    public RadValidationRuleWithTargetControlEx()
    {
    }

    /// <summary>
    /// Gets or sets the Target Control. This control's property value will be used in the Rule evaluation.
    /// </summary>
    [DefaultValue(null)]
    public Control TargetControl { get; set; }


    /// <summary>
    /// The name of the property that will be used in the Rule evaluation.
    /// </summary>
    [DefaultValue("Text")]
    public string TargetControlPropertyName
    {
        get { return this.sourceControlPropertyName; }
        set { this.sourceControlPropertyName = value; }
    }

    /// <summary>
    /// Gets the Rule expression.
    /// </summary>
    /// <value>The Rule expression.</value>
    public override string Expression
    {
        get
        {
            if (this.TargetControl != null)
            {
                this.Value = this.CalculateValue();
            }

            return base.Expression;
        }
    }

    /// <summary>
    /// Gets or sets the Value of this rule. Controls in the rule will be evaluated against this value.
    /// </summary>
    [EditorBrowsable(EditorBrowsableState.Never)]
    [Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public override object Value
    {
        get
        {
            if (this.TargetControl != null && !string.IsNullOrEmpty(this.TargetControlPropertyName))
            {
                try
                {
                    base.Value = RadValidationProviderEx.GetSubPropertyValue(TargetControl, TargetControlPropertyName);//TargetControl.GetType().GetProperty(TargetControlPropertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).GetValue(TargetControl, null);
                }
                catch { }

            }
            return base.Value;
        }
        set
        {

            base.Value = value;
        }
    }

    protected virtual object CalculateValue()
    {
        if (this.TargetControl.Site != null)
        {
            return string.Format("{0}.{1}", this.TargetControl.Name, this.TargetControlPropertyName);
        }

        if (!string.IsNullOrEmpty(this.TargetControlPropertyName))
        {
            try
            {
                return RadValidationProviderEx.GetSubPropertyValue(TargetControl, TargetControlPropertyName);//this.TargetControl.GetType().GetProperty(this.TargetControlPropertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance).GetValue(this.TargetControl, null);
            }
            catch
            { }
        }

        return null;
    }
}
