using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AIStudio.Wpf.DiagramDesigner
{
    /// <summary>
    /// TextControl.xaml 的交互逻辑
    /// </summary>
    public partial class TextControl : UserControl
    {

        public TextControl()
        {
            InitializeComponent();

            this.Loaded += TextControl_Loaded;
            this.PART_ShowText.IsVisibleChanged += PART_ShowText_IsVisibleChanged;
        }     

        private void TextControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.Loaded -= TextControl_Loaded;

            //新建后处于编辑状态，暂时关闭
            //PART_ShowText.Visibility = Visibility.Visible;
            //PART_TextBlock.Visibility = Visibility.Collapsed;
            //PART_ShowText.Focus();
            //if (!string.IsNullOrEmpty(PART_ShowText.Text))
            //{
            //    PART_ShowText.SelectionStart = PART_ShowText.Text.Length;
            //}

            if (this.DataContext is ISelectable selectable)
            {
                selectable.PropertyChanged -= TextControl_PropertyChanged;
                selectable.PropertyChanged += TextControl_PropertyChanged;
            }
            TextControl_PropertyChanged(this.DataContext, new System.ComponentModel.PropertyChangedEventArgs("IsSelected"));
        }

        private void PART_ShowText_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext is ISelectable selectable)
            {
                selectable.IsEditing = PART_ShowText.IsVisible;
                PART_ShowText.Focusable = PART_ShowText.IsVisible;
            }
        }

        private void TextControl_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsSelected")
            {
                if (sender is ISelectable selectable)
                {
                    if (selectable.IsSelected == false)
                    {
                        PART_ShowText.Visibility = Visibility.Collapsed;
                        PART_TextBlock.Visibility = Visibility.Visible;
                        selectable.IsEditing = false;
                    }
                }
            }
            else if (e.PropertyName == "ShowText")
            {
                PART_ShowText.Visibility = Visibility.Visible;
                PART_TextBlock.Visibility = Visibility.Collapsed;
                PART_ShowText.Focus();
                if (!string.IsNullOrEmpty(PART_ShowText.Text))
                {
                    PART_ShowText.SelectionStart = PART_ShowText.Text.Length;
                }
            }
        }      
    }

    public class ControlAttachProperty
    {
        #region WatermarkProperty 水印
        /// <summary>
        /// 水印
        /// </summary>
        public static readonly DependencyProperty WatermarkProperty = DependencyProperty.RegisterAttached(
            "Watermark", typeof(string), typeof(ControlAttachProperty), new FrameworkPropertyMetadata(""));

        public static string GetWatermark(DependencyObject d)
        {
            return (string)d.GetValue(WatermarkProperty);
        }

        public static void SetWatermark(DependencyObject obj, string value)
        {
            obj.SetValue(WatermarkProperty, value);
        }
        #endregion
    }
}
