using System.Windows;
using System.Windows.Controls;
using HandyControl.Data;
using HandyControl.Tools.Extension;

namespace HandyControl.Controls
{
    public class Col : ContentControl
    {
        public static readonly DependencyProperty LayoutProperty = DependencyProperty.Register(
            "Layout", typeof(ColLayout), typeof(Col), new PropertyMetadata(default(ColLayout)));

        public ColLayout Layout
        {
            get => (ColLayout)GetValue(LayoutProperty);
            set => SetValue(LayoutProperty, value);
        }

        public static readonly DependencyProperty OffsetProperty = DependencyProperty.Register(
            "Offset", typeof(int), typeof(Col), new PropertyMetadata(ValueBoxes.Int0Box));

        public int Offset
        {
            get => (int)GetValue(OffsetProperty);
            set => SetValue(OffsetProperty, value);
        }

        public static readonly DependencyProperty SpanProperty = DependencyProperty.Register(
            "Span", typeof(int), typeof(Col), new PropertyMetadata(24), OnSpanValidate);

        private static bool OnSpanValidate(object value)
        {
            var v = (int)value;
            return v >= 1 || v <= 24;
        }

        public int Span
        {
            get => (int) GetValue(SpanProperty);
            set => SetValue(SpanProperty, value);
        }

        internal int GetLayoutCellCount(ColLayoutStatus status)
        {
            var result = 0;

            if (Layout != null)
            {
                switch (status)
                {
                    case ColLayoutStatus.Xs:
                        result = Layout.Xs;
                        break;
                    case ColLayoutStatus.Sm:
                        result = Layout.Sm;
                        break;
                    case ColLayoutStatus.Md:
                        result = Layout.Md;
                        break;
                    case ColLayoutStatus.Lg:
                        result = Layout.Lg;
                        break;
                    case ColLayoutStatus.Xl:
                        result = Layout.Xl;
                        break;
                    case ColLayoutStatus.Xxl:
                        result = Layout.Xxl;
                        break;
                }
            }
            else
            {
                result = Span;
            }

            return result;
        }
    }
}
