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;
        
        private String _currencySymbol = "";

        public String CurrencySymbol
        {
            get => _currencySymbol;
            set
            {
                _currencySymbol = value;
                if (Editor != null) 
                    Editor.CurrencySymbol = String.IsNullOrEmpty(value)
                        ? "$" : 
                        $"{value} ";
            }
        }

        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);
                DockPanel.Children.Add(button);
            }

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

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

            Editor.CurrencySymbol = String.IsNullOrEmpty(EditorDefinition.CurrencySymbol)
                ? "$" : 
                $"{EditorDefinition.CurrencySymbol} ";

            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)
        {
            try
            {
                Editor.Value = Convert.ToDecimal(value);
            }
            catch (Exception e)
            {
                Editor.Value = 0m;
            }
            
        }

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

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