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

namespace InABox.DynamicGrid
{
    /// <summary>
    ///     Interaction logic for ProductIssuesWindow.xaml
    /// </summary>
    public partial class DynamicIssuesEditor : ThemableWindow
    {
        private readonly IIssues[] _items;

        private readonly List<string> notes = new();

        public DynamicIssuesEditor(IIssues[] issues, bool allowclear = false)
        {
            _items = issues;
            InitializeComponent();
            ReloadHistory();
            ClearIssues.Visibility = allowclear ? Visibility.Visible : Visibility.Collapsed;
        }

        private void ReloadHistory()
        {
            var issues = _items.Select(x => x.Issues).Where(x => !string.IsNullOrWhiteSpace(x)).Distinct().ToArray();

            var history = new List<string>();
            if (issues.Length == 1)
                history.Add(issues[0]);
            else if (issues.Length > 1)
                history.Add("(Multiple Issues Noted)");

            if (issues.Length > 0 && notes.Any())
                history.Add("=========================================");
            if (notes.Any())
                history.AddRange(notes);

            History.Text = string.Join("\n", history);
        }

        private void ClearIssues_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Are you sure you wish to clear these notes?", "Confirm Clear", MessageBoxButton.YesNo) != MessageBoxResult.Yes)
                return;
            foreach (var item in _items)
                item.Issues = "";
            DialogResult = true;
        }

        private void OK_Click(object sender, RoutedEventArgs e)
        {
            if (!notes.Any())
            {
                DialogResult = false;
            }
            else
            {
                foreach (var item in _items)
                    item.Issues = (string.IsNullOrWhiteSpace(item.Issues) ? "" : item.Issues + "\n") + string.Format(
                        "{0:dd MMM yy hh\\:mm\\:ss} {1} {2}",
                        DateTime.Now, ClientFactory.UserID, string.Join("\n", notes));
                DialogResult = true;
            }
        }

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

        private void AddNote_Click(object sender, RoutedEventArgs e)
        {
            DoAddNote();
        }

        private void DoAddNote()
        {
            notes.Add(NewNote.Text);
            NewNote.Text = "";
            NewNote.Focus();
            ReloadHistory();
        }

        private void NewNote_TextChanged(object sender, TextChangedEventArgs e)
        {
            AddNote.IsEnabled = !string.IsNullOrWhiteSpace(NewNote.Text);
        }

        private void AddNote_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter || e.Key == Key.Return)
                DoAddNote();
        }
    }
}