using System.Collections.Generic;
using System.Text.RegularExpressions;
using InABox.Core;
using System.Windows;
using System.Windows.Media;

namespace InABox.DynamicGrid
{
    public class RichTextEditorControl : DynamicEditorControl<string, InABox.Core.RichTextEditor>
    {
        
        static RichTextEditorControl()
        {
            //DynamicEditorControlFactory.Register<RichTextEditorControl, InABox.Core.RichTextEditor>();
        }
        
        private RichTextEditor Editor;

        public override int DesiredHeight()
        {
            return int.MaxValue;
        }

        public override int DesiredWidth()
        {
            return int.MaxValue;
        }

        public override void SetColor(Color color)
        {
            Editor.SetColor(IsEnabled ? color : Colors.WhiteSmoke);
        }

        public override void SetEnabled(bool enabled)
        {
            Editor.Editor.IsReadOnly = !enabled;
            Editor.ZoomFactor = 100;
            SetColor(enabled ? Color : Colors.WhiteSmoke);
        }

        public override void SetFocus()
        {
            Editor.Focus();
        }
        public override void Configure()
        {
        }

        protected override FrameworkElement CreateEditor()
        {
            using var profiler = new Profiler(true);
            MinHeight = 250;
            
            Editor = new RichTextEditor
            {
                VerticalAlignment = VerticalAlignment.Stretch,
                //VerticalContentAlignment = VerticalAlignment.Top,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                //VerticalScrollBarVisibility = ScrollBarVisibility.Auto,
                //TextWrapping = TextWrapping.Wrap,
                //AcceptsReturn = true,
                VisibleButtons = EditorDefinition?.VisibleButtons ?? RichTextEditorButtons.Simple
            };
            Editor.OnChanged += o => { CheckChanged(); };
            return Editor;
        }
        
        protected override string RetrieveValue()
        {
            return Editor.Text;
        }

        protected override void UpdateValue(string value)
        {
            Editor.Text = value;
        }
    }
}