using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ModuleA;
using ModuleA.Models;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Mvvm;
using Microsoft.Practices.Prism.Commands;

namespace ModuleC
{
    class ModuleCViewViewModel : BindableBase, INavigationAware
    {
       
        private decimal suma;
        private ObservableCollection<Product> products;
        private ObservableCollection<Product> cart;
        public DelegateCommand<Product> AddToCartCommand { get; set; }
        public DelegateCommand<Product> RemoveFromCartCommand { get; set; }
        public DelegateCommand SumCommand { get; set; }

         public decimal Suma
        {
            get 
            {
                return suma;
            }
            set
            {
                SetProperty<decimal>(ref suma, value);
            }
        }
        public ObservableCollection<Product> ProductsInChart
        {
            get
            {
                return cart;
            }
            protected set
            {
                SetProperty<ObservableCollection<Product>>(ref cart, value);
            }
        }

        public ObservableCollection<Product> Products

		{
			get
			{
				return products;
			}
			protected set
			{
				SetProperty<ObservableCollection<Product>>(ref products, value);
			}
		}

        protected IProductRepository productsRepository;

        public ModuleCViewViewModel(IProductRepository productsRepository)
        {
            this.productsRepository = productsRepository;
            AddToCartCommand = new DelegateCommand<Product>(AddToCart);
            RemoveFromCartCommand = new DelegateCommand<Product>(RemoveFromCart);
            SumCommand = new DelegateCommand(Sum);
            this.Products = new ObservableCollection<Product>(this.productsRepository.GetAll());
            cart = new ObservableCollection<Product>();
            
        }

		public bool IsNavigationTarget(NavigationContext navigationContext)
		{
			return true;
		}

		public void OnNavigatedFrom(NavigationContext navigationContext)
		{
		}

		public void OnNavigatedTo(NavigationContext navigationContext)
		{
			this.Products = new ObservableCollection<Product>(this.productsRepository.GetAll());
		}

        private void AddToCart(Product product)
        {
            ProductsInChart.Add(product);
     
        }
        private void RemoveFromCart(Product product)
        {
            ProductsInChart.Remove(product);
     
        }

        private void Sum()
        {
            Suma = ProductsInChart.Sum(x => x.Price);
        }
        
           
        }
	}


