using System.Windows.Input;
using DatabaseEditor.BL.Models;
using DatabaseEditor.DAL;
using DatabaseEditor.Messages;
using GalaSoft.MvvmLight.CommandWpf;
using MySql.Data.MySqlClient;

namespace DatabaseEditor.ViewModels
{
    public class MainViewModel : ViewModelBase
    {
        private readonly string _connectionString;

        public MainViewModel()
        {
            _connectionString = ConnectionStringReader.GetConnectionString();

            MessengerInstance.Register<ShowItemMessage>(this, ShowItem);
            MessengerInstance.Register<ConnectionNotExistsMessage>(this, ShowCreateDbDialog);
            MessengerInstance.Register<CreateDatabaseMessage>(this, CreateDatabase);
        }

        private void CreateDatabase(CreateDatabaseMessage message)
        {
            CreateDatabaseDialogVisible = false;
            if (message.CreateDb)
            {
                CreateDatabase(message.AuthorsCount, message.BooksCount, message.MinAuthorsBooks,
                    message.MaxAuthorsBooks);
                Connect();
            }
            else
            {
                IsConnected = false;
            }
        }

        private void ShowCreateDbDialog(ConnectionNotExistsMessage message)
        {
            CreateDatabaseDialogVisible = true;
        }

        public ICommand ConnectCommand => new RelayCommand(Connect, () => !IsConnected);

        public ICommand DisconnectCommand => new RelayCommand(Disconnect, () => IsConnected);

        public int TabSelectionIndex { get; set; }

        private void ShowItem(ShowItemMessage message)
        {
            if (message.Item is BookListModel)
            {
                TabSelectionIndex = 0; // switch to Books TabPage
                MessengerInstance.Send(new ShowItemMessageGeneric<BookListModel>((BookListModel) message.Item));
            }
            else if (message.Item is AuthorListModel)
            {
                TabSelectionIndex = 1; // switch to Authors TabPage
                MessengerInstance.Send(new ShowItemMessageGeneric<AuthorListModel>((AuthorListModel) message.Item));
            }
        }

        private void Connect()
        {
            if (!IsConnected)
            {
                if (DatabaseExists())
                {
                    MessengerInstance.Send(new ConnectMessage(true));
                    IsConnected = true;
                }
                else
                {
                    CreateDatabaseDialogVisible = true;
                }
            }
        }

        private bool DatabaseExists()
        {
            using (var connection = new MySqlConnection(_connectionString))
            {
                using (var dbContext = new LibraryDbContext(connection, false))
                {
                    return dbContext.Database.Exists();
                }
            }
        }

        private bool CreateDatabase(int authorsCount, int booksCount, int minAuthorsBooks, int maxAuthorsBooks)
        {
            using (var dbContext =
                new LibraryDbContext(_connectionString, authorsCount, booksCount, minAuthorsBooks, maxAuthorsBooks))
            {
                return dbContext.Database.Exists();
            }
        }

        private void Disconnect()
        {
            if (IsConnected)
            {
                IsConnected = false;
                MessengerInstance.Send(new ConnectMessage(false));
            }
        }

        public bool IsConnected { get; set; }

        public RelayCommand RefreshCommand => new RelayCommand(Refresh, () => IsConnected);

        public bool CreateDatabaseDialogVisible { get; private set; }

        private void Refresh()
        {
            MessengerInstance.Send(new RefreshCommand());
        }
    }
}