using System;
using DatabaseEditor.Messages;
using GalaSoft.MvvmLight.CommandWpf;

namespace DatabaseEditor.ViewModels
{
    public class CreateDatabaseViewModel : ViewModelBase
    {
        public RelayCommand ConfirmCommand => new RelayCommand(CreateDb, () => ValuesAreIntegers);

        public string AuthorsCount { get; set; }
        public string BooksCount { get; set; }
        public string MinAuthorsBooks { get; set; }
        public string MaxAuthorsBooks { get; set; }

        private void CreateDb()
        {
            MessengerInstance.Send(new CreateDatabaseMessage(
                true,
                Int32.Parse(AuthorsCount),
                Int32.Parse(BooksCount),
                Int32.Parse(MinAuthorsBooks),
                Int32.Parse(MaxAuthorsBooks)));
        }

        public RelayCommand CancelCommand => new RelayCommand(Cancel);

        private void Cancel()
        {
            MessengerInstance.Send(new CreateDatabaseMessage(false, 0, 0, 0, 0));
        }

        private bool ValuesAreIntegers
        {
            get
            {
                int temp;
                if (Int32.TryParse(AuthorsCount, out temp))
                    if (Int32.TryParse(BooksCount, out temp))
                        if (Int32.TryParse(MinAuthorsBooks, out temp))
                            if (Int32.TryParse(MaxAuthorsBooks, out temp))
                                return true;
                return false;
            }
        }
    }
}