using System.Collections.Generic;
using System.Windows;

namespace XLAB2
{
    internal interface IPrsnDirectoryDialogService
    {
        bool Confirm(string message);

        PrdspvDirectoryItem ShowPrdspvEditDialog(PrdspvDirectoryItem seed, bool isNew, IReadOnlyList<PrdspvDirectoryItem> existingItems, PrsnDirectoryService service);

        PrfrDirectoryItem ShowPrfrEditDialog(PrfrDirectoryItem seed, bool isNew, IReadOnlyList<PrfrDirectoryItem> existingItems, PrsnDirectoryService service);

        PrfrvdDirectoryItem ShowPrfrvdEditDialog(PrfrvdDirectoryItem seed, bool isNew, IReadOnlyList<PrfrvdDirectoryItem> existingItems, PrsnDirectoryService service);

        PrsnDirectoryItem ShowPrsnEditDialog(PrsnDirectoryItem seed, bool isNew, IReadOnlyList<PrsnDirectoryItem> existingItems);

        void ShowError(string message);

        void ShowInfo(string message);

        void ShowWarning(string message);
    }

    internal sealed class PrsnDirectoryDialogService : IPrsnDirectoryDialogService
    {
        private readonly Window _owner;

        public PrsnDirectoryDialogService(Window owner)
        {
            _owner = owner;
        }

        public bool Confirm(string message)
        {
            return MessageBox.Show(_owner, message, "ПСВ", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes;
        }

        public PrdspvDirectoryItem ShowPrdspvEditDialog(PrdspvDirectoryItem seed, bool isNew, IReadOnlyList<PrdspvDirectoryItem> existingItems, PrsnDirectoryService service)
        {
            var viewModel = new PrdspvEditWindowViewModel(seed, isNew, existingItems, service);
            var window = new PrdspvEditWindow(viewModel);
            window.Owner = _owner;

            var result = window.ShowDialog();
            return result.HasValue && result.Value ? viewModel.ToResult() : null;
        }

        public PrfrDirectoryItem ShowPrfrEditDialog(PrfrDirectoryItem seed, bool isNew, IReadOnlyList<PrfrDirectoryItem> existingItems, PrsnDirectoryService service)
        {
            var viewModel = new PrfrEditWindowViewModel(seed, isNew, existingItems, service);
            var window = new PrfrEditWindow(viewModel);
            window.Owner = _owner;

            var result = window.ShowDialog();
            return result.HasValue && result.Value ? viewModel.ToResult() : null;
        }

        public PrfrvdDirectoryItem ShowPrfrvdEditDialog(PrfrvdDirectoryItem seed, bool isNew, IReadOnlyList<PrfrvdDirectoryItem> existingItems, PrsnDirectoryService service)
        {
            var viewModel = new PrfrvdEditWindowViewModel(seed, isNew, existingItems, service);
            var window = new PrfrvdEditWindow(viewModel);
            window.Owner = _owner;

            var result = window.ShowDialog();
            return result.HasValue && result.Value ? viewModel.ToResult() : null;
        }

        public PrsnDirectoryItem ShowPrsnEditDialog(PrsnDirectoryItem seed, bool isNew, IReadOnlyList<PrsnDirectoryItem> existingItems)
        {
            var viewModel = new PrsnEditWindowViewModel(seed, isNew, existingItems);
            var window = new PrsnEditWindow(viewModel);
            window.Owner = _owner;

            var result = window.ShowDialog();
            return result.HasValue && result.Value ? viewModel.ToResult() : null;
        }

        public void ShowError(string message)
        {
            MessageBox.Show(_owner, message, "ПСВ", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        public void ShowInfo(string message)
        {
            MessageBox.Show(_owner, message, "ПСВ", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        public void ShowWarning(string message)
        {
            MessageBox.Show(_owner, message, "ПСВ", MessageBoxButton.OK, MessageBoxImage.Warning);
        }
    }
}
