using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Valg.EVA.Skanning.Resources.Language;

namespace Valg.EVA.Skanning.UserInterfaceElements.DatabaseVerifier
{
    public class SplashWindowViewModel : INotifyPropertyChanged
    {

        public string DescriptionText { get; set; } = LanguageString.SplashScreen_FirstTimeSetup;
        public string SubDescriptionText { get; set; } = LanguageString.SplashScreen_PleaseWaitText;
        public bool ErrorHasOccured { get; set; }
        public bool ShowProgressIndicator => !ErrorHasOccured;
        public bool ShowExitButton => ErrorHasOccured;
        public bool ShowErrorDetails => ErrorHasOccured;
        public string ErrorDetailsText { get; set; }
        public string ExitApplicationText => LanguageString.SplashScreen_ExitApplication;
        public event PropertyChangedEventHandler PropertyChanged;

        public void ErrorOccured(string[] errorDetails)
        {
            DescriptionText = LanguageString.SplashScreen_UnexpectedError;
            SubDescriptionText = LanguageString.SplashScreen_ContactSystemadministrator;

            ErrorDetailsText = FormatErrors(errorDetails);
            ErrorHasOccured = true;
            UpdateView();
        }

        private static string FormatErrors(string[] errorDetails)
        {
            return string.Join(Environment.NewLine, errorDetails);
        }

        private void UpdateView()
        {
            OnPropertyChanged(nameof(ErrorDetailsText));
            OnPropertyChanged(nameof(ShowErrorDetails));
            OnPropertyChanged(nameof(ShowExitButton));
            OnPropertyChanged(nameof(ShowProgressIndicator));
            OnPropertyChanged(nameof(ErrorHasOccured));
            OnPropertyChanged(nameof(DescriptionText));
            OnPropertyChanged(nameof(SubDescriptionText));
        }

        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

