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

namespace XLAB
{
    internal interface ITypeSizeDialogService
    {
        TipsDirectoryItem ShowTipsEditDialog(TipsDirectoryItem seed, bool isNew, IReadOnlyList<TipsDirectoryItem> existingItems, TypeSizeDirectoryService service);

        TprzDirectoryItem ShowTprzEditDialog(TprzDirectoryItem seed, bool isNew, IReadOnlyList<TprzDirectoryItem> existingItems, TypeSizeDirectoryService service);

        TprmcpDirectoryItem ShowTprmcpEditDialog(TprmcpDirectoryItem seed, bool isNew, IReadOnlyList<TprmcpDirectoryItem> existingItems, TypeSizeDirectoryService service);

        TprmkDirectoryItem ShowTprmkEditDialog(TprmkDirectoryItem seed, bool isNew, IReadOnlyList<TprmkDirectoryItem> existingItems, TypeSizeDirectoryService service);

        TpvdklDirectoryItem ShowTpvdklEditDialog(TpvdklDirectoryItem seed, bool isNew, IReadOnlyList<TpvdklDirectoryItem> existingItems, TypeSizeDirectoryService service);

        void ShowTpvdklManagerDialog(TipsDirectoryItem tipsItem, TypeSizeDirectoryService service);

        bool Confirm(string message);

        void ShowError(string message);

        void ShowInfo(string message);

        void ShowWarning(string message);
    }

    internal sealed class TypeSizeDialogService : ITypeSizeDialogService
    {
        private readonly Window _owner;

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

        public TipsDirectoryItem ShowTipsEditDialog(TipsDirectoryItem seed, bool isNew, IReadOnlyList<TipsDirectoryItem> existingItems, TypeSizeDirectoryService service)
        {
            var viewModel = new TipsEditWindowViewModel(seed, isNew, existingItems, service);
            var window = new TipsEditWindow(viewModel);
            window.Owner = _owner;

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

        public TprzDirectoryItem ShowTprzEditDialog(TprzDirectoryItem seed, bool isNew, IReadOnlyList<TprzDirectoryItem> existingItems, TypeSizeDirectoryService service)
        {
            var viewModel = new TprzEditWindowViewModel(seed, isNew, existingItems, service);
            var window = new TprzEditWindow(viewModel);
            window.Owner = _owner;

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

        public TprmcpDirectoryItem ShowTprmcpEditDialog(TprmcpDirectoryItem seed, bool isNew, IReadOnlyList<TprmcpDirectoryItem> existingItems, TypeSizeDirectoryService service)
        {
            var viewModel = new TprmcpEditWindowViewModel(seed, isNew, existingItems, service);
            var window = new TprmcpEditWindow(viewModel);
            window.Owner = _owner;

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

        public TprmkDirectoryItem ShowTprmkEditDialog(TprmkDirectoryItem seed, bool isNew, IReadOnlyList<TprmkDirectoryItem> existingItems, TypeSizeDirectoryService service)
        {
            var viewModel = new TprmkEditWindowViewModel(seed, isNew, existingItems, service);
            var window = new TprmkEditWindow(viewModel);
            window.Owner = _owner;

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

        public TpvdklDirectoryItem ShowTpvdklEditDialog(TpvdklDirectoryItem seed, bool isNew, IReadOnlyList<TpvdklDirectoryItem> existingItems, TypeSizeDirectoryService service)
        {
            var viewModel = new TpvdklEditWindowViewModel(seed, isNew, existingItems, service);
            var window = new TpvdklEditWindow(viewModel);
            window.Owner = _owner;

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

        public void ShowTpvdklManagerDialog(TipsDirectoryItem tipsItem, TypeSizeDirectoryService service)
        {
            var window = new TpvdklWindow(tipsItem, service);
            window.Owner = _owner;
            window.ShowDialog();
        }

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

        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);
        }
    }
}
