using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using FasdDesktopUi.Basics.Models;
using FasdDesktopUi.Basics.UserControls.AdaptableIcon;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Pages.DetailsPage.UserControls
{
    public partial class DetailsPageStateContainerCollection : UserControl
    {
        #region Properties and Fields

        private List<UIElement> editControls = new List<UIElement>();

        public cContainerCollectionData CollectionData
        {
            get { return (cContainerCollectionData)GetValue(CollectionDataProperty); }
            set { SetValue(CollectionDataProperty, value); }
        }

        public static readonly DependencyProperty CollectionDataProperty =
            DependencyProperty.Register("CollectionData", typeof(cContainerCollectionData), typeof(DetailsPageStateContainerCollection), new PropertyMetadata(null, new PropertyChangedCallback(CollectionDataChanged)));

        private static void CollectionDataChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!(d is DetailsPageStateContainerCollection _me))
                return;

            _me.editControls.Clear();
            _me.DrawStateContainers();
        }

        #endregion

        public DetailsPageStateContainerCollection()
        {
            InitializeComponent();
        }

        private void DrawStateContainers()
        {
            try
            {
                MainStack.Children.Clear();

                foreach (var containerData in CollectionData)
                {
                    var containerToAdd = new DetailsPageStateContainer() { ContainerData = containerData, Margin = new Thickness(0, 0, 0, 10) };
                    MainStack.Children.Add(containerToAdd);
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        public void SetEditMode(bool isActive)
        {
            try
            {
                editControls.ForEach(x => x.Visibility = isActive ? Visibility.Visible : Visibility.Collapsed);

                foreach (var child in MainStack.Children)
                {
                    if (!(child is DetailsPageStateContainer stateContainer))
                        continue;

                    stateContainer.SetEditMode(isActive);
                }
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }
    }
}
