using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using ClassLibrary2;


namespace WpfApp1
{
    /// <summary>
    /// Логика взаимодействия для books.xaml
    /// </summary>
    public partial class MainPage : Page, INotifyPropertyChanged
    {
        public int CountTakedBooks { get; set; }
        List<int> BasketBook = new List<int>();
        ViewModel viewModel = new ViewModel();
        int totalCost = 0;
        int totalSale = 0;
        public List<Books> bufferList = new List<Books>();
        public Books bufferBook;
        public event PropertyChangedEventHandler PropertyChanged;
        public string OldPrise { get; set; }
        public MainPage()
        {
            InitializeComponent();
            ListBoksAllBooks.ItemsSource = viewModel.books.ToList();
            ListBoksAllBooks.Items.Refresh();
            OldPrise = "None";
        }

        private void Add_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CountTakedBooks = 0;
                Button button = (Button)sender;
                int id = Convert.ToInt32(button.Uid);
                Books books = DataBAse.BaseModel.Books.FirstOrDefault(x => x.IdBook == id);
                int summ = books.CountInStock + books.CountInStore;
                if (books.CountInStock == 0 && books.CountInStore == 0)
                {
                    throw new Exception("Данной книги нет в наличии.");
                }
                books.CountInBasket++;
                if (books.CountInBasket > summ)
                {
                    books.CountInBasket--;
                    throw new Exception("Превышен лимит товара");
                }
                BasketBook.Add(id);
                CountTakedBooks = BasketBook.Count;
                totalCost += Convert.ToInt32(books.Cost);
                Class1 class1 = new Class1();
                totalSale = class1.sale(BasketBook.Count, totalCost);
                kolbook.Text = BasketBook.Count.ToString();
                price.Text = totalCost.ToString();
                sale.Text = totalSale.ToString();
                if(totalSale > 0)
                {
                    OldPrise = "Strikethrough";
                    PropertyChanged(this, new PropertyChangedEventArgs("OldPrise"));
                    newprice.Visibility = Visibility.Visible;
                }
                double ab = totalCost - (totalCost * totalSale / 100);
                newprice.Text = Math.Floor(ab).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Basket_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                foreach (int y in BasketBook.Distinct().ToList())
                {
                    bufferBook = DataBAse.BaseModel.Books.FirstOrDefault(x => x.IdBook == y);
                    bufferList.Add(bufferBook);
                }
                ChangePages.changed.Navigate(new YourBasket(bufferList, totalSale));
            }
            catch
            {
                MessageBox.Show("Ошибка");
            }
        }
    }
}
