using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using InABox.Core;
using Syncfusion.Windows.Shared;

namespace InABox.DynamicGrid
{
    public class CurrencyEditorControl : DynamicEditorControl<double, CurrencyEditor>
    {
        
        static CurrencyEditorControl()
        {
            //DynamicEditorControlFactory.Register<CurrencyEditorControl, CurrencyEditor>();
        }
        
        private CurrencyTextBox Editor;

        public CurrencyEditorControl()
        {
            Width = 150;
        }
        public override void Configure()
        {
        }

        protected override FrameworkElement CreateEditor()
        {
            var DockPanel = new DockPanel
            {
                //Orientation = Orientation.Horizontal,
                HorizontalAlignment = HorizontalAlignment.Stretch
            };

            var buttons = CreateButtons(out var bDisableEditor);
            foreach(var button in buttons)
            {
                button.SetValue(DockPanel.DockProperty, Dock.Right);
                button.Click += (o, e) =>
                {
                    var b = (Button)o;
                    var eb = b.Tag as EditorButton;
                    eb.Click(this);
                };
                DockPanel.Children.Add(button);
            }

            Editor = new CurrencyTextBox
            {
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                CurrencyDecimalDigits = (EditorDefinition as CurrencyEditor).Digits
            };
            Editor.ValueChanged += (o, e) => { CheckChanged(); };
            Editor.SetValue(DockPanel.DockProperty, Dock.Left);

            if (bDisableEditor)
            {
                Editor.Background = new SolidColorBrush(Colors.Silver);
                Editor.IsEnabled = false;
            }

            DockPanel.Children.Add(Editor);


            return DockPanel;
        }

        public override int DesiredHeight()
        {
            return 25;
        }

        public override int DesiredWidth()
        {
            var result = 150;
            var btnEditor = EditorDefinition as IButtonEditor;
            if (btnEditor?.Buttons != null)
                foreach (var button in ((IButtonEditor)EditorDefinition).Buttons)
                    result += button.Width + 5;
            return result;
        }

        protected override double RetrieveValue()
        {
            return Convert.ToDouble(Editor.Value);
        }

        protected override void UpdateValue(double value)
        {
            Editor.Value = Convert.ToDecimal(value);
        }

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

        public override void SetColor(Color color)
        {
            Editor.Background = new SolidColorBrush(color);
        }
    }
}