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 FasdDesktopUi.Basics.Models;
using static C4IT.Logging.cLogManager;

namespace FasdDesktopUi.Pages.DetailsPage.UserControls
{
    public partial class CustomizableSection : UserControl
    {

        #region Properties

        #region ContainerCollections

        public List<cContainerCollectionData> ContainerCollections
        {
            get { return (List<cContainerCollectionData>)GetValue(ContainerCollectionsProperty); }
            set { SetValue(ContainerCollectionsProperty, value); }
        }

        public static readonly DependencyProperty ContainerCollectionsProperty =
            DependencyProperty.Register("ContainerCollections", typeof(List<cContainerCollectionData>), typeof(CustomizableSection), new PropertyMetadata(new List<cContainerCollectionData>(), new PropertyChangedCallback(ContainerCollectionsChanged)));

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

            _me.DrawContainerCollections();
        }

        #endregion

        #region IsDataIncomplete DependencyProperty

        public static readonly DependencyProperty IsDataIncompleteProperty =
            DependencyProperty.Register("IsDataIncomplete", typeof(bool), typeof(CustomizableSection), new PropertyMetadata(true));

        public bool IsDataIncomplete
        {
            get { return (bool)GetValue(IsDataIncompleteProperty); }
            set { SetValue(IsDataIncompleteProperty, value); }
        }

        #endregion

        #endregion

        public CustomizableSection()
        {
            InitializeComponent();
        }

        public void UpdateContainerCollection(List<cContainerCollectionData> containerCollectionsData)
        {
            ContainerCollections = containerCollectionsData;
        }

        private void DrawContainerCollections()
        {
            MainGrid.Children.Clear();
            foreach (var containerCollection in ContainerCollections)
            {
                var containerCollectionToAdd = new DetailsPageStateContainerCollection() { CollectionData = containerCollection };
                Grid.SetColumnSpan(containerCollectionToAdd, containerCollection.ColumnSpan);
                SetColumnIndex(containerCollectionToAdd, containerCollection.ColumnSpan);
                MainGrid.Children.Add(containerCollectionToAdd);
            }
        }

        private void SetColumnIndex(UIElement element, int columnSpan)
        {
            try
            {
                int columnCount = MainGrid.ColumnDefinitions.Count;

                if (MainGrid.Children.Count <= 0)
                {
                    Grid.SetColumn(element, 0);
                    Grid.SetRow(element, 0);
                    return;
                }

                var lastChild = MainGrid.Children.OfType<FrameworkElement>().Last();

                int lastChildColumnIndex = Grid.GetColumn(lastChild);
                int lastChildColumnSpan = Grid.GetColumnSpan(lastChild);
                int lastChildRowIndex = Grid.GetRow(lastChild);

                if (columnCount < lastChildColumnIndex + lastChildColumnSpan + columnSpan)
                {
                    MainGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
                    Grid.SetRow(element, lastChildRowIndex + 1);
                    Grid.SetColumn(element, 0);
                    return;
                }

                Grid.SetRow(element, lastChildRowIndex);
                Grid.SetColumn(element, lastChildColumnIndex + lastChildColumnSpan);
            }
            catch (Exception E)
            {
                LogException(E);
            }
        }

        public void SetEditMode(bool isActive)
        {
            try
            {
                foreach (var child in MainGrid.Children)
                {
                    if (!(child is DetailsPageStateContainerCollection stateContainerCollection))
                        continue;

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