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

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

        FrpdDirectoryItem ShowFrpdEditDialog(FrpdDirectoryItem seed, bool isNew, IReadOnlyList<FrpdDirectoryItem> existingItems, FrpdDirectoryService service);

        FrpdvdDirectoryItem ShowFrpdvdEditDialog(FrpdvdDirectoryItem seed, bool isNew, IReadOnlyList<FrpdvdDirectoryItem> existingItems, FrpdDirectoryService service);

        void ShowError(string message);

        void ShowInfo(string message);

        void ShowWarning(string message);
    }

    internal sealed class FrpdDirectoryDialogService : IFrpdDirectoryDialogService
    {
        private readonly Window _owner;

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

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

        public FrpdDirectoryItem ShowFrpdEditDialog(FrpdDirectoryItem seed, bool isNew, IReadOnlyList<FrpdDirectoryItem> existingItems, FrpdDirectoryService service)
        {
            var viewModel = new FrpdEditWindowViewModel(seed, isNew, existingItems, service);
            var window = new FrpdEditWindow(viewModel);
            window.Owner = _owner;

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

        public FrpdvdDirectoryItem ShowFrpdvdEditDialog(FrpdvdDirectoryItem seed, bool isNew, IReadOnlyList<FrpdvdDirectoryItem> existingItems, FrpdDirectoryService service)
        {
            var viewModel = new FrpdvdEditWindowViewModel(seed, isNew, existingItems, service);
            var window = new FrpdvdEditWindow(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);
        }
    }
}
