namespace TeamViewerApi.ViewModels
{
    #region Using
    using System;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Windows.Controls;
    using Views;
    #endregion

    public class MainWindowViewModel : BasicViewModel
    {
        #region Properties
        private static MainWindowViewModel instance;
        private ObservableCollection<UserControl> views;
        private UserControl selectedView;
        #endregion

        #region Constructor
        private MainWindowViewModel()
        {
            this.views = new ObservableCollection<UserControl>();
            this.views.Add(new AccountView());
            this.views.Add(new UsersView());
            this.views.Add(new GroupsView());
            this.views.Add(new SessionsView());
            this.views.Add(new ContactsView());
            this.views.Add(new DevicesView());
            this.views.Add(new MeetingsView());
        }
        #endregion

        #region Public properties
        public static MainWindowViewModel Instance
        {
            get
            {
                if (MainWindowViewModel.instance == null)
                    MainWindowViewModel.instance = new MainWindowViewModel();

                return instance;
            }
        }
        
        /// <summary>
        /// Views
        /// </summary>
        public ObservableCollection<UserControl> Views { get { return this.views; } }

        /// <summary>
        /// Current view
        /// </summary>
        public UserControl SelectedView
        {
            get { return this.selectedView; }
            set
            {
                this.selectedView = value;

                this.RaisePropertyChanged(() => this.SelectedView);
            }
        }
        #endregion

        #region Public methods
        public void AddView(UserControl view)
        {
            if(!this.views.Contains(view))
                this.views.Add(view);

            this.SelectedView = view;
        }

        public void RemoveView(UserControl view)
        {
            if (this.views.Contains(view))
                this.views.Remove(view);
        }

        public void RemoveView(BasicViewModel viewModel)
        {
            UserControl view = this.views.FirstOrDefault(x => x.DataContext == viewModel);

            if (view != null)
                this.views.Remove(view);
        }
        #endregion
    }
}
