using InABox.Wpf;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace InABox.DynamicGrid
{
    /// <summary>
    ///     Interaction logic for FormDesignerLength.xaml
    /// </summary>
    public partial class DynamicFormDesignLength : ThemableWindow
    {
        private readonly Dictionary<GridUnitType, string> types = new()
        {
            { GridUnitType.Auto, "Auto" },
            { GridUnitType.Pixel, "Pixels" },
            { GridUnitType.Star, "Percentage" }
        };

        public DynamicFormDesignLength(GridLength length)
        {
            InitializeComponent();
            Type.ItemsSource = types;
            GridLength = length;
        }

        public GridLength GridLength
        {
            get => new(Value.Value.Value, (GridUnitType)Type.SelectedValue);
            set
            {
                Type.SelectedValue = value.GridUnitType;
                Value.Value = value.Value;
            }
        }

        private void OKClick(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

        private void CancelClick(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
        }

        private void Type_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var newvalue = e.AddedItems.Count > 0 ? ((KeyValuePair<GridUnitType, string>)e.AddedItems[0]).Key : GridUnitType.Auto;
            //var oldvalue = e.RemovedItems.Count > 0 ? ((KeyValuePair<GridUnitType, String>)e.RemovedItems[0]).Key : GridUnitType.Auto;
            if (newvalue == GridUnitType.Star)
            {
                Value.Value = 1.0F;
                Value.IsReadOnly = false;
            }
            else if (newvalue == GridUnitType.Auto)
            {
                Value.Value = 1.0F;
                Value.IsReadOnly = true;
            }
            else if (newvalue == GridUnitType.Pixel)
            {
                Value.Value = 50.0F;
                Value.IsReadOnly = false;
            }
        }
    }
}