using FasdDesktopUi.Basics.CustomEvents;
using FasdDesktopUi.Basics.Helper;
using FasdDesktopUi.Basics.Models;
using System;
using System.Windows;
using System.Windows.Controls;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Basics.UserControls
{
    public partial class CustomDialog : UserControl, IFocusInvoker
    {

        #region Proeprties and Fields

        public int? ParentIndex { get; set; }
        public UIElement ParentElement { get; set; }

        #region HasYesNoButtons

        public bool HasYesNoButtons
        {
            get { return (bool)GetValue(HasYesNoButtonsProperty); }
            set { SetValue(HasYesNoButtonsProperty, value); }
        }

        public static readonly DependencyProperty HasYesNoButtonsProperty =
            DependencyProperty.Register("HasYesNoButtons", typeof(bool), typeof(CustomDialog), new PropertyMetadata(true));

        #endregion


        #region HasYesNoText

        public bool HasYesNoText
        {
            get { return (bool)GetValue(HasYesNoTextProperty); }
            set { SetValue(HasYesNoTextProperty, value); }
        }

        public static readonly DependencyProperty HasYesNoTextProperty =
            DependencyProperty.Register("HasYesNoText", typeof(bool), typeof(CustomDialog), new PropertyMetadata(true));

        #endregion

        #region ContainerData

        public cContainerData ContainerData
        {
            get { return (cContainerData)GetValue(ContainerDataProperty); }
            set { SetValue(ContainerDataProperty, value); }
        }

        public static readonly DependencyProperty ContainerDataProperty =
            DependencyProperty.Register("ContainerData", typeof(cContainerData), typeof(CustomDialog), new PropertyMetadata(new cContainerData(), new PropertyChangedCallback(ContainerDataHasChanged)));

        private static void ContainerDataHasChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                if (!(d is CustomDialog _me))
                    return;

                _me.UpdateDialogContent();
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        #endregion

        #endregion

        public CustomDialog()
        {
            InitializeComponent();
        }

        private void UpdateDialogContent()
        {
            try
            {
                if (ContainerData is null)
                    return;

                MainStack.Children.Clear();

                foreach (var dialogComponentData in ContainerData)
                {
                    if (cUiElementHelper.DrawCustomizableContainerComponent(dialogComponentData, this, out var createdControl))
                    {
                        createdControl.Margin = new Thickness(0, 5, 0, 5);
                        MainStack.Children.Add(createdControl);
                    }
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        private void YesNoButton_ButtonHasBeenClicked(object sender, BooleanEventArgs e)
        {

        }
    }
}
