using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
using System.Globalization;
using Platform.Common.BasicFunctions;
using Platform.Controls.ControlsHelper;

namespace Platform.Controls.AdornerContainer
{
   [DesignTimeVisible(false)]
   public class PlaceholderAdorner:Adorner
   {
       private VisualCollection _visCollec;
       private TextBlock _tb;
       private TextBox _txt;

       public PlaceholderAdorner(UIElement ele, string placeholder)
           : base(ele)
       {
           _txt = ele as TextBox;
           if (_txt == null) return;

           Binding bd = new Binding("IsVisible");
           bd.Source = _txt;
           bd.Mode = BindingMode.OneWay;
           bd.Converter = new BoolToVisibilityConverter();
           this.SetBinding(TextBox.VisibilityProperty, bd);
           _visCollec = new VisualCollection(this);
           _tb = new TextBlock();
           _tb.Style = null;
           _tb.FontSize = _txt.FontSize;
           _tb.FontFamily = _txt.FontFamily;
           _tb.FontWeight = _txt.FontWeight;
           _tb.FontStretch = _txt.FontStretch;
           _tb.FontStyle = FontStyles.Italic;
           _tb.Foreground = Brushes.Gray;
           _tb.Text = placeholder;
           _tb.IsHitTestVisible = false;
           _visCollec.Add(_tb);
       }

       protected override int VisualChildrenCount
       {
           get
           {
               return _visCollec.Count;
           }
       }

       protected override Size ArrangeOverride(Size finalSize)
       {
           Point point = TextBoxHelper.GetPlaceholderOffset(this.AdornedElement);
           _tb.Arrange(new Rect(point, finalSize));
           return finalSize;
       }

       protected override Visual GetVisualChild(int index)
       {
           return _visCollec[index];
       }

       /// <summary>
       /// 重写方法以添加进一步图形元素到呈现的元素，如效果或装饰器。
       /// </summary>
       /// <param name="drawingContext">DrawingContext 对象作为参数传递，可以为绘制形状、文本、图像或视频提供方法</param>
       //protected override void OnRender(DrawingContext drawingContext)
       //{
       //    TextBox txt = this.AdornedElement as TextBox;
       //    if (txt == null || !txt.IsVisible) return;

       //    this.IsHitTestVisible = false;

       //    drawingContext.DrawText(
       //        new FormattedText
       //        (
       //            _placeholder, 
       //            CultureInfo.CurrentCulture,
       //            txt.FlowDirection,
       //            new Typeface(txt.FontFamily, FontStyles.Italic, txt.FontWeight, txt.FontStretch), 
       //            txt.FontSize,
       //            Brushes.Gray
       //        ),
       //        new Point(4, 2));
       //}
   }
}
