using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using InABox.Core;
using Syncfusion.XlsIO.Parser.Biff_Records.Charts;

namespace InABox.DynamicGrid
{
    public class ButtonEditorControl : DynamicEditorControl<string, ButtonEditor>
    {
                
        static ButtonEditorControl()
        {
            //DynamicEditorControlFactory.Register<ButtonEditorControl, ButtonEditor>();
        }
        
        private Button Editor;

        private string _data = "";

        public Action<object, ButtonEditorClickArgs> OnClick { get; set; }
        
        protected override FrameworkElement CreateEditor()
        {
            Editor = new Button
            {
                Content = EditorDefinition.Label,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
            };
            Editor.Click += Editor_Click;

            return Editor;
        }

        public override void Configure()
        {
            if (EditorDefinition is not ButtonEditor editor) return;

            OnClick += editor.OnClick;
        }

        private void Editor_Click(object sender, RoutedEventArgs e)
        {
            var args = new ButtonEditorClickArgs() { Cancel = false, Data = _data };
            OnClick?.Invoke(this, args);
            if (!args.Cancel)
                _data = args.Data;
        }

        public override int DesiredHeight()
        {
            return 25;
        }

        public override int DesiredWidth()
        {
            return 100;
        }

        protected override string RetrieveValue()
        {
            return _data;
        }

        protected override void UpdateValue(string value)
        {
            _data = value;
        }

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

        public override void SetColor(Color color)
        {
        }
    }
}