using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp2.Pages
{
    /// <summary>
    /// Логика взаимодействия для ListPage.xaml
    /// </summary>
    public partial class ListPage : Page
    {
        public List<Book> cart = new List<Book>();
        public float sum = 0;
        public int count = 0;
        public ListPage()
        {
            InitializeComponent();
            list.ItemsSource = DB.data.Books.ToList();
            buySum.Text = sum.ToString();
            selectedCount.Text = count.ToString();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var book = (Book)((Button)sender).DataContext;
            if (book.countStock == 0 && book.countStrore == 0)
            {
                MessageBox.Show("Кнмиги нет в наличии!");
                return;
            }
            book.Count++;
            if (!cart.Any(a=>a.title == book.title))
            {
                cart.Add(book);
            }
            selectedCount.Text = cart.Sum(a=>a.Count).ToString();
            sum = (float)cart.Sum(a => a.price * a.Count);
            buySum.Text = sum.ToString();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            FrameClass.frame.Navigate(new CartPage(cart));
        }
    }
}
