using DatabaseEditor.BL.Interfaces;
using DatabaseEditor.BL.ItemsProviders;
using DatabaseEditor.BL.Models;
using DatabaseEditor.ViewModels;
using GalaSoft.MvvmLight.Ioc;
using Microsoft.Practices.ServiceLocation;

namespace DatabaseEditor
{
    public class ViewModelLocator
    {
        static ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
            var connectionString = ConnectionStringReader.GetConnectionString();

            // fixes design-time errors
            SimpleIoc.Default.Unregister<AuthorsProvider>();
            SimpleIoc.Default.Unregister<BooksProvider>();
            SimpleIoc.Default.Unregister<MySQLItemsProvider<AuthorListModel, AuthorDetialModel>>();
            SimpleIoc.Default.Unregister<MySQLItemsProvider<BookListModel, BookDetailModel>>();
            SimpleIoc.Default.Unregister<IItemsProvider<AuthorListModel>>();
            SimpleIoc.Default.Unregister<IItemsProvider<BookListModel>>();
            SimpleIoc.Default.Unregister<IDetailProvider<AuthorDetialModel>>();
            SimpleIoc.Default.Unregister<IDetailProvider<BookDetailModel>>();

            SimpleIoc.Default.Register<AuthorsProvider>(() => new AuthorsProvider(connectionString));
            SimpleIoc.Default.Register<BooksProvider>(() => new BooksProvider(connectionString));
            SimpleIoc.Default.Register<MySQLItemsProvider<AuthorListModel, AuthorDetialModel>>(() => SimpleIoc.Default.GetInstance<AuthorsProvider>());
            SimpleIoc.Default.Register<MySQLItemsProvider<BookListModel, BookDetailModel>>(() => SimpleIoc.Default.GetInstance<BooksProvider>());
            SimpleIoc.Default.Register<IItemsProvider<AuthorListModel>>(() => SimpleIoc.Default.GetInstance<AuthorsProvider>());
            SimpleIoc.Default.Register<IItemsProvider<BookListModel>>(() => SimpleIoc.Default.GetInstance<BooksProvider>());
            SimpleIoc.Default.Register<IDetailProvider<AuthorDetialModel>>(() => SimpleIoc.Default.GetInstance<AuthorsProvider>());
            SimpleIoc.Default.Register<IDetailProvider<BookDetailModel>>(() => SimpleIoc.Default.GetInstance<BooksProvider>());
        }

        public MainViewModel MainViewModel => CreateMainWindowViewModel();
        public CreateDatabaseViewModel CreateDatabaseViewModel => new CreateDatabaseViewModel();
        public PageViewModel<BookListModel, BookDetailModel> BooksPageViewModel => new PageViewModel<BookListModel, BookDetailModel>();
        public PageViewModel<AuthorListModel, AuthorDetialModel> AuthorsPageViewModel => new PageViewModel<AuthorListModel, AuthorDetialModel>();
        public ItemDetailViewModel<AuthorListModel, AuthorDetialModel> AuthorDetailViewModel => new ItemDetailViewModel<AuthorListModel, AuthorDetialModel>();
        public ItemDetailViewModel<BookListModel, BookDetailModel> BookDetailViewModel => new ItemDetailViewModel<BookListModel, BookDetailModel>();
        public FilterViewModel<AuthorListModel> AuthorsFilterViewModel => new FilterViewModel<AuthorListModel>();
        public FilterViewModel<BookListModel> BooksFilterViewModel => new FilterViewModel<BookListModel>();
        public EditItemViewModel<AuthorDetialModel, BookListModel> EditAuthorViewModel => new EditAuthorViewModel();
        public EditItemViewModel<BookDetailModel, AuthorListModel> EditBookViewModel => new EditBookViewModel();
        public SelectItemViewModel<BookListModel> SelectBookViewModel => new SelectItemViewModel<BookListModel>();
        public SelectItemViewModel<AuthorListModel> SelectAuthorViewModel => new SelectItemViewModel<AuthorListModel>();
        
        private MainViewModel CreateMainWindowViewModel()
        {
            return new MainViewModel();
        }
    }
}
