using Calculation;
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 Exam19Apr
{
    /// <summary>
    /// Логика взаимодействия для pgBooksCatalog.xaml
    /// </summary>
    public partial class pgBooksCatalog : Page
    {
        int count = 0;
        decimal price = 0;
        List<Books> books;
        int Sale = 0;
        public pgBooksCatalog()
        {
            InitializeComponent();
            BaseConnect.BaseModel = new Entities();
            lbBooks.ItemsSource = BaseConnect.BaseModel.Books.ToList();
            books = new List<Books>();
            tbPer.Visibility = Visibility.Collapsed;
            tbRub.Visibility = Visibility.Collapsed;
            tbSalePrice.Visibility = Visibility.Collapsed;
        }
        private void btnAddBok_Click(object sender, RoutedEventArgs e)
        {

            Button btn = (Button)sender;
            int id = Convert.ToInt32(btn.Uid);
            Books book = BaseConnect.BaseModel.Books.FirstOrDefault(x => x.id == id);
            Books checkBook = books.FirstOrDefault(x => x.id == id);
            int summ = BaseConnect.BaseModel.Books.Where(x => x.id == id).Sum(x => x.StockCount) + BaseConnect.BaseModel.Books.Where(x => x.id == id).Sum(x => x.StoreCount);
            int summList = 0;
            if (book.StockCount > 0 || book.StoreCount > 0)
            {
                if (checkBook == null)
                {
                    checkBook = book;
                    checkBook.FullCount = 0;
                    books.Add(checkBook);
                }
                if (book.StockCount + book.StoreCount > checkBook.FullCount)
                {

                    summList = books.Where(x => x.id == id).Sum(x => x.StockCount) + books.Where(x => x.id == id).Sum(x => x.StoreCount);
                    checkBook.FullCount++;
                        count++;
                        tbCount.Text = count.ToString();
                        price += book.Price;
                        tbPrice.Text = price.ToString();
                        if (tbPrice.Text != "" || tbSalePrice.Text != "")
                        {
                            tbRub.Visibility = Visibility.Visible;
                        }
                        lbBooks.Items.Refresh();
                        tbPrice.Text = tbPrice.Text.Replace(".", ",");
                        Calculator calc = new Calculator();
                        int countTb = Convert.ToInt32(tbCount.Text);
                        decimal priceTb = Convert.ToDecimal(tbPrice.Text);
                        Sale = calc.Calc(priceTb, countTb);
                        foreach (Books booke in books)
                        {
                            booke.Sale = Sale;
                        }
                }
                else
                    MessageBox.Show("Товар нельзя купить!");
            }
            else
                MessageBox.Show("Товар нельзя купить!");




            if (Sale > 0 && Sale <= 100)
            {
                tbPrice.TextDecorations = TextDecorations.Strikethrough;
                tbPer.Visibility = Visibility.Visible;
                tbSale.Visibility = Visibility.Visible;
                tbSalePrice.Visibility = Visibility.Visible;
                tbPer.Visibility = Visibility.Visible;
                decimal salePrice;
                salePrice = Convert.ToDecimal(tbPrice.Text) - Convert.ToDecimal(tbPrice.Text) * (Convert.ToDecimal(Sale) / 100);
                tbSalePrice.Text = salePrice.ToString();
                tbSale.Text = Sale.ToString();
            }

        }

        private void btnGoCart_Click(object sender, RoutedEventArgs e)
        {
            LoadPages.goPage.Navigate(new pgTrash(books));
        }
    }
}

