﻿using PaintTracker.Core.DependencyInterfaces;
using System.Threading.Tasks;
using System.Windows.Input;

namespace PaintTracker.Core.ViewModels
{
    public class ErrorVMArgs
    {
        public string Message { get; }
        public string Troubleshooting { get; }
        public bool CanRetry { get; }

        public ErrorVMArgs(string message, string troubleshooting, bool canRetry)
        {
            Message = message;
            CanRetry = canRetry;
            Troubleshooting = troubleshooting;
        }
    }

    public class ErrorVM : PageBaseVM, IHaveVMArgs<ErrorVMArgs>
    {
        private string _message;
        private bool _canRetry;
        private bool _isRetrying;
        private string _troubleshooting;

        public string Message
        {
            get { return _message; }
            set { SetProperty(ref _message, value); }
        }

        public string Troubleshooting
        {
            get { return _troubleshooting; }
            set { SetProperty(ref _troubleshooting, value); }
        }

        public bool CanRetry
        {
            get { return _canRetry; }
            set { SetProperty(ref _canRetry, value); }
        }

        public ICommand RetryCommand { get; }

        public bool IsRetrying
        {
            get { return _isRetrying; }
            set { SetProperty(ref _isRetrying, value); }
        }

        public ErrorVM(IExternalMessageSender messageSender) : base(messageSender, "Error")
        {
            RetryCommand = new RelayCommand(Retry);
        }

        private async void Retry(object obj)
        {
            IsRetrying = true;
            await (App.Current as App).LoadAppAsync();
            IsRetrying = false;
        }

        public Task LoadArgsAsync(ErrorVMArgs args)
        {
            Message = args.Message;
            CanRetry = args.CanRetry;
            Troubleshooting = args.Troubleshooting;

            return Extensions.CompletedTask;
        }
    }
}
