using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using InABox.Clients;
using InABox.Core;
using InABox.Wpf;

namespace InABox.DynamicGrid
{
    /// <summary>
    ///     Interaction logic for MultiSelectWindow.xaml
    /// </summary>
    public partial class MultiSelectWindow : ThemableWindow
    {
        public MultiSelectWindow(Type type, Guid[] exclusions)
        {
            InitializeComponent();
            Setup(type, exclusions);
        }

        //added for when wanting to prepopulate - only been tested on Manufacturing Packet Trolley selection so far
        public MultiSelectWindow(Type type, Guid[] exclusions, List<string> prepopulateValues) 
        {
            InitializeComponent();
            Setup(type, exclusions);
            try
            {
                if (prepopulateValues.Count > 0)
                {
                    foreach (var item in Items.Items)
                    {
                        string stringvalue = (item as KeyValuePair<Entity, string>?).Value.ToString();
                        var pairstring = stringvalue.Split(',');
                        string pairvalue = pairstring[1];
                        if (pairvalue.Contains('/'))
                        {
                            var substring = pairvalue.Split('/');
                            string s = substring[0].Trim();
                            if (prepopulateValues.Contains(s) && !Items.SelectedItems.Contains(item))
                            {
                                Items.SelectedItems.Add(item);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to Prepopulate", ex.Message);
            }
        }

        private void Setup(Type type, Guid[] exclusions)
        {
            Values = new Entity[] { };

            var source = new Dictionary<Entity, string>();
            var client = ClientFactory.CreateClient(type);
            //var columns = Entity.DefaultLookupColumns(type) as IColumns;
            var columns = LookupFactory.DefineColumns(type);

            var filter = LookupFactory.DefineFilter(type);

            //var sort = BaseObject.DefaultSortOrder(type);
            var sort = LookupFactory.DefineSort(type);

            var items = client.Load(filter, sort);
            foreach (var item in items)
                if (!exclusions.Contains(item.ID))
                {
                    var display = new List<object>();
                    foreach (var column in columns.ColumnNames().Where(x => !x.Equals("ID")))
                        display.Add(CoreUtils.GetPropertyValue(item, column));
                    source[item] = string.Join(" / ", display.Where(x => x != null && !string.IsNullOrWhiteSpace(x.ToString())));
                }

            Items.ItemsSource = source;
        }

        public Entity[] Values { get; private set; }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            var result = new List<Entity>();
            foreach (KeyValuePair<Entity, string> item in Items.SelectedItems)
                result.Add(item.Key);
            Values = result.ToArray();

            DialogResult = true;
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        public static bool Execute(Type type, ref Entity[] items, Guid[] exclusions)
        {
            var window = new MultiSelectWindow(type, exclusions);
            if (window.ShowDialog() == true)
            {
                items = window.Values;
                return true;
            }

            return false;
        }

        private void SelectAll_Click(object sender, RoutedEventArgs e)
        {
        }

        private void SelectAll_Click_1(object sender, RoutedEventArgs e)
        {
            Items.SelectedItems.Clear();
            if (SelectAll.Content.ToString() == "Select All")
            {
                foreach (var item in Items.Items)
                    Items.SelectedItems.Add(item);
                SelectAll.Content = "Clear All";
            }
            else
            {
                SelectAll.Content = "Select All";
            }
        }
    }
}