using System;
using System.Windows;
using System.Windows.Input;
using MaxwellFramework.Core.Common.Command;
using Stylet;

namespace MainShell.Common.ViewModel
{
    public class MwMessageBoxWindowViewModel : PropertyChangedBase
    {
        private bool? _dialogResult;
        private MessageBoxResult _result;

        public MwMessageBoxWindowViewModel(MwMessageBoxState state)
        {
            State = state ?? throw new ArgumentNullException(nameof(state));
            SelectButtonCommand = new DelegateCommand(OnSelectButton);
            WindowClosingCommand = new DelegateCommand(OnWindowClosing);
        }

        public MwMessageBoxState State { get; }

        public bool? DialogResult
        {
            get => _dialogResult;
            set => SetAndNotify(ref _dialogResult, value);
        }

        public MessageBoxResult Result
        {
            get => _result;
            private set => SetAndNotify(ref _result, value);
        }

        public ICommand SelectButtonCommand { get; }

        public ICommand WindowClosingCommand { get; }

        private void OnSelectButton(object parameter)
        {
            var definition = parameter as MwMessageBoxButtonDefinition;
            if (definition == null)
            {
                return;
            }

            Result = definition.Result;
            DialogResult = true;
        }

        private void OnWindowClosing(object parameter)
        {
            if (Result != MessageBoxResult.None)
            {
                return;
            }

            Result = MwMessageBox.ResolveFallbackResult(State);
        }
    }
}