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

namespace XLAB
{
    internal interface IDialogService
    {
        DocumentEditorResult ShowCreateDocumentDialog(DocumentEditorResult seed);

        IReadOnlyList<AvailableInstrumentItem> ShowInstrumentPickerDialog(string customerName, IReadOnlyList<AvailableInstrumentItem> instruments);

        InstrumentTypeSelectionResult ShowInstrumentTypeDialog(string customerName, IReadOnlyList<AvailableInstrumentItem> instrumentTypes);

        IReadOnlyList<string> ShowCloneVerificationDialog(CloneVerificationSeed seed);

        SpoiDirectoryItem ShowSpoiEditDialog(SpoiDirectoryItem seed, bool isNew, IReadOnlyList<SpoiDirectoryItem> existingItems);

        SpnmtpDirectoryItem ShowSpnmtpEditDialog(SpnmtpDirectoryItem seed, bool isNew, IReadOnlyList<SpnmtpDirectoryItem> existingItems);

        VerificationEditResult ShowVerificationDialog(
            VerificationEditSeed seed,
            IReadOnlyList<PersonReference> verifiers,
            IReadOnlyList<DocumentFormReference> documentForms);

        bool Confirm(string message);

        void ShowError(string message);

        void ShowInfo(string message);

        void ShowWarning(string message);
    }

    internal sealed class DialogService : IDialogService
    {
        private readonly Window _owner;

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

        public DocumentEditorResult ShowCreateDocumentDialog(DocumentEditorResult seed)
        {
            var viewModel = new CreateDocumentWindowViewModel(seed);
            var window = new CreateDocumentWindow(viewModel);
            window.Owner = _owner;

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

        public IReadOnlyList<AvailableInstrumentItem> ShowInstrumentPickerDialog(string customerName, IReadOnlyList<AvailableInstrumentItem> instruments)
        {
            var viewModel = new SelectInstrumentsWindowViewModel(customerName, instruments);
            var window = new SelectInstrumentsWindow(viewModel);
            window.Owner = _owner;

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

        public InstrumentTypeSelectionResult ShowInstrumentTypeDialog(string customerName, IReadOnlyList<AvailableInstrumentItem> instrumentTypes)
        {
            var viewModel = new SelectInstrumentTypeWindowViewModel(customerName, instrumentTypes);
            var window = new SelectInstrumentTypeWindow(viewModel);
            window.Owner = _owner;

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

        public IReadOnlyList<string> ShowCloneVerificationDialog(CloneVerificationSeed seed)
        {
            var viewModel = new CloneVerificationWindowViewModel(seed);
            var window = new CloneVerificationWindow(viewModel);
            window.Owner = _owner;

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

        public SpoiDirectoryItem ShowSpoiEditDialog(SpoiDirectoryItem seed, bool isNew, IReadOnlyList<SpoiDirectoryItem> existingItems)
        {
            var viewModel = new SpoiEditWindowViewModel(seed, isNew, existingItems);
            var window = new SpoiEditWindow(viewModel);
            window.Owner = _owner;

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

        public SpnmtpDirectoryItem ShowSpnmtpEditDialog(SpnmtpDirectoryItem seed, bool isNew, IReadOnlyList<SpnmtpDirectoryItem> existingItems)
        {
            var viewModel = new SpnmtpEditWindowViewModel(seed, isNew, existingItems);
            var window = new SpnmtpEditWindow(viewModel);
            window.Owner = _owner;

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

        public VerificationEditResult ShowVerificationDialog(
            VerificationEditSeed seed,
            IReadOnlyList<PersonReference> verifiers,
            IReadOnlyList<DocumentFormReference> documentForms)
        {
            var viewModel = new VerificationEditWindowViewModel(seed, verifiers, documentForms);
            var window = new VerificationEditWindow(viewModel);
            window.Owner = _owner;

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

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