using System;
using System.Windows.Input;
using Prism.Commands;
using Valg.EVA.Skanning.UserInterfaceElements.LightBox;

namespace Valg.EVA.Skanning.UserInterfaceElements.MessageBox
{
    public partial class MessageBox : IMessageBox
    {
        private readonly IMessageBoxViewModel viewModel;

        public MessageBox(IMessageBoxViewModel viewModel)
            : this()
        {
            this.viewModel = viewModel;
            DataContext = this.viewModel;
        }

        public MessageBox()
        {
            InitializeComponent();
        }

        public void Initialize(string messageBoxTitle, string messageBoxMessage, string possibleSolutionMessage, MessageBoxType messageBoxType, Exception[] errorException = null,
            DelegateCommand cancelCommand = null, ICommand[] solutionCommands = null)
        {
            viewModel.Initialize(messageBoxTitle, messageBoxMessage, possibleSolutionMessage, messageBoxType, errorException, GetCommand(cancelCommand), solutionCommands);
        }

        public void Initialize(string messageBoxTitle, string messageBoxMessage, string[] possibleSolutionMessages, MessageBoxType messageBoxType, Exception[] errorException = null, DelegateCommand cancelCommand = null)
        {
            Initialize(messageBoxTitle, messageBoxMessage, string.Join("#", possibleSolutionMessages), messageBoxType, errorException, cancelCommand);
        }

        public void Initialize(string messageBoxTitle, string messageBoxMessage, MessageBoxType messageBoxType, DelegateCommand cancelCommand = null)
        {
            Initialize(messageBoxTitle, messageBoxMessage, string.Empty, messageBoxType, null, cancelCommand);
        }

        public void Initialize(string messageBoxTitle, string messageBoxMessage, MessageBoxType messageBoxType, DelegateCommand confirmCommand, DelegateCommand cancelCommand)
        {
            viewModel.Initialize(messageBoxTitle, messageBoxMessage, messageBoxType, GetCommand(confirmCommand), GetCommand(cancelCommand));
        }

        public bool ShowWindowWhenNoParentWindow(string messageBoxTitle, string messageBoxMessage, string possibleSolutionMessage, MessageBoxType messageBoxType, Exception errorException = null, DelegateCommand cancelCommand = null)
        {
            this.EnsureArrowCursorForWindow();

            viewModel.Initialize(messageBoxTitle, messageBoxMessage, possibleSolutionMessage, messageBoxType, errorException != null ? new[] { errorException } : null, GetCommand(cancelCommand), null);
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
            var result = ShowDialog();
            return result.HasValue && result.Value;
        }

        public void CloseWindow()
        {
            Close();
        }

        private DelegateCommand GetCommand(DelegateCommand command)
        {
            return new DelegateCommand(
                delegate
                {
                    if (command != null)
                    {
                        command.Execute();
                    }
                    CloseWindow();
                });
        }
    }
}
