using BPA.Helper;
using BPA.Model;
using BPA.SingleDevice.View;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace BPA.SingleDevice.ViewModel
{
    public class MainViewModel : NotifyBase
    {
        public MainViewModel()
        {
            ViewItems = new ObservableCollection<ViewItem>
            {
                new ViewItem("原料管理", new RawMaterialManagementView(), IconType.ReservedFill),
                new ViewItem("配方管理",  new RecipeManagementView(), IconType.NewspaperFill),
                new ViewItem("点单主页",  new OrderMainView(), IconType.Home3Fill),
            };

            SelecteCommand = new BPARelayCommand<object>(DoNavChanged);
        }

        public ObservableCollection<ViewItem> ViewItems { get; set; } = new ObservableCollection<ViewItem>();

        public BPARelayCommand<object> SelecteCommand { get; set; }


        public ViewItem CurrentViewItem
        {
            get { return _mCurrentViewItem; }
            set
            {
                _mCurrentViewItem = value;
                OnPropertyChanged();
                var res = ViewItems.FirstOrDefault(p => p.Name == value.Name);
                res.Content = Activator.CreateInstance(res.Content.GetType());
            }
        }
        private ViewItem _mCurrentViewItem = new ViewItem();


        private void DoNavChanged(object obj)
        {
            if (obj != null)
            {
                Type type = Type.GetType($"BPA.SingleDevice.View.{obj.ToString()}");
                ConstructorInfo cti = type?.GetConstructor(Type.EmptyTypes);
                MainContent = (FrameworkElement)cti?.Invoke(null);
            }
        }

        public FrameworkElement MainContent { get { return _mMainContent; } set { _mMainContent = value; OnPropertyChanged(); } }
        private FrameworkElement _mMainContent;

    }
}
