using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows;

namespace SurfacePlot3D.Members
{
    public abstract class Text3DBase : ModelVisualBase
    {
        public DpiScale Dpi { get; set; }
        static readonly DependencyPropertyKey TextGeometryKey = DependencyProperty.RegisterReadOnly("TextGeometry", typeof(Geometry), typeof(Text3DBase), new PropertyMetadata(null));
        public static readonly DependencyProperty TextGeometryProperty = TextGeometryKey.DependencyProperty;

        public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(Text3DBase), new UIPropertyMetadata("", TextPropertyChanged, null, true));
        public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner(typeof(Text3DBase), new FrameworkPropertyMetadata(TextPropertyChanged));
        public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner(typeof(Text3DBase), new FrameworkPropertyMetadata(TextPropertyChanged));
        public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner(typeof(Text3DBase), new FrameworkPropertyMetadata(TextPropertyChanged));
        public static readonly DependencyProperty FontStretchProperty = TextElement.FontStretchProperty.AddOwner(typeof(Text3DBase), new FrameworkPropertyMetadata(TextPropertyChanged));
        public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(typeof(Text3DBase), new FrameworkPropertyMetadata(1.0, FrameworkPropertyMetadataOptions.None, TextPropertyChanged, null, true));
        public static readonly DependencyProperty OriginProperty = DependencyProperty.Register("Origin", typeof(Point), typeof(Text3DBase), new UIPropertyMetadata(new Point(), TextPropertyChanged, null, true));

        public String Text { set { SetValue(TextProperty, value); } get { return (String)GetValue(TextProperty); } }
        public FontFamily FontFamily { set { SetValue(FontFamilyProperty, value); } get { return (FontFamily)GetValue(FontFamilyProperty); } }
        public FontStyle FontStyle { set { SetValue(FontStyleProperty, value); } get { return (FontStyle)GetValue(FontStyleProperty); } }
        public FontWeight FontWeight { set { SetValue(FontWeightProperty, value); } get { return (FontWeight)GetValue(FontWeightProperty); } }
        public FontStretch FontStretch { set { SetValue(FontStretchProperty, value); } get { return (FontStretch)GetValue(FontStretchProperty); } }
        public Double FontSize { set { SetValue(FontSizeProperty, value); } get { return (double)GetValue(FontSizeProperty); } }
        public Point Origin { set { SetValue(OriginProperty, value); } get { return (Point)GetValue(OriginProperty); } }
        public Geometry TextGeometry { private set { SetValue(TextGeometryKey, value); } get { return (Geometry)GetValue(TextGeometryProperty); } }

        static void TextPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { (obj as Text3DBase).TextPropertyChanged(args); }
        protected virtual void TextPropertyChanged(DependencyPropertyChangedEventArgs args)
        {
            FormattedText formattedText = new FormattedText(Text, CultureInfo.GetCultureInfo("en-us"),
            FlowDirection.LeftToRight, new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize, Brushes.Black, 96); //Dpi.DpiScaleX

            TextGeometry = formattedText.BuildGeometry(Origin); PropertyChanged(args);
        }
    }
}