using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using System.Windows.Controls.Primitives;

namespace WGneu.Helpers {
    public static class Utils {
        public static void SetInputChanged(Control input) {
            var brush = Brushes.Orange;
            if (input is ComboBox cb) {
                var border = GetComboBoxBorder(cb);
                if (border != null)
                    border.BorderBrush = brush;
            } else {
                input.BorderBrush = brush;
            }
        }

        public static void SetInputInvalid(Control input) {
            var brush = Brushes.Red;
            if (input is ComboBox cb) {
                var border = GetComboBoxBorder(cb);
                if (border != null)
                    border.BorderBrush = brush;
            } else {
                input.BorderBrush = brush;
            }
        }

        public static void ClearInputState(Control input) {
            if (input is ComboBox cb) {
                GetComboBoxBorder(cb)?.ClearValue(Border.BorderBrushProperty);
            } else {
                input.ClearValue(Control.BorderBrushProperty);
            }
        }

        private static Border? GetComboBoxBorder(ComboBox cb) {
            var toggleButton = cb.Template.FindName("toggleButton", cb) as ToggleButton;
            return toggleButton?.Template.FindName("templateRoot", toggleButton) as Border;
        }

        public static IEnumerable<T> FindVisualChilds<T>(DependencyObject depObj) where T : DependencyObject {
            if (depObj == null)
                yield return (T)Enumerable.Empty<T>();
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) {
                DependencyObject ithChild = VisualTreeHelper.GetChild(depObj, i);
                if (ithChild == null)
                    continue;
                if (ithChild is T t)
                    yield return t;
                foreach (T childOfChild in FindVisualChilds<T>(ithChild))
                    yield return childOfChild;
            }
        }

        public static IEnumerable<T> FindVisualChilds<T>(DependencyObject depObj, IEnumerable<DependencyObject> exempt) where T : DependencyObject {
            return FindVisualChilds<T>(depObj).Where(c => !exempt.Contains(c));
        }

        public static int Modulo(string a, int b) {
            if (!a.All(char.IsDigit))
                throw new ArgumentException("First argument has to be a decimal string");
            return a.Select(ch => ch - '0').Aggregate((sum, n) => (sum * 10 + n) % b);
        }

        public static void RunBackground(string title, Func<Task> a) {
            Task.Run(async () => {
                try {
                    await a();
                } catch (Exception e) {
                    MessageBox.Show(e.ToString(), title, MessageBoxButton.OK, MessageBoxImage.Error);
                }
            });
        }

        public static void MailTo(string emailAddress) {
            Process.Start(new ProcessStartInfo() {
                FileName = $"mailto:{emailAddress}",
                UseShellExecute = true,
            });
        }

        public static bool MgNrExists(AppDbContext ctx, int mgnr) {
            return ctx.Members.Find(mgnr) != null;
        }

        public static int NextMgNr(AppDbContext ctx) {
            int c = ctx.Members.Select(m => m.MgNr).Min();
            ctx.Members.OrderBy(m => m.MgNr).Select(m => m.MgNr).ToList().ForEach(a => { if (a <= c + 100) c = a; });
            return c + 1;
        }
    }
}