using CalcS;
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 ExamBoldin
{
    /// <summary>
    /// Логика взаимодействия для pgListBooks.xaml
    /// </summary>
    public partial class pgListBooks : Page
    {
        int count = 0;
        decimal price = 0;
        List<BookShop> books;
        int Sale = 0;
        public pgListBooks()
        {
            InitializeComponent();
            BaseConnect.BaseModel = new Entities();
            lbBooks.ItemsSource = BaseConnect.BaseModel.BookShop.ToList();
            books = new List<BookShop>();
            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);
            BookShop book = BaseConnect.BaseModel.BookShop.FirstOrDefault(x => x.id == id);
            BookShop findBook = books.FirstOrDefault(x => x.id == id);
            int summ = BaseConnect.BaseModel.BookShop.Where(x => x.id == id).Sum(x => x.CountStock) + BaseConnect.BaseModel.BookShop.Where(x => x.id == id).Sum(x => x.CountStore);
            int summList = 0;
            if (book.CountStock > 0 || book.CountStore > 0)
            {
                if (findBook == null)
                {
                    findBook = new BookShop();
                    findBook.id = book.id;
                    findBook.Cover = book.Cover;
                    findBook.Name = book.Name;
                    findBook.Description = book.Description;
                    findBook.Author = book.Author;
                    findBook.Cost = book.Cost;
                    findBook.Genre = book.Genre;
                    books.Add(findBook);
                }
                if (book.CountStock + book.CountStore > findBook.AllCount)
                {

                    summList = books.Where(x => x.id == id).Sum(x => x.CountStock) + books.Where(x => x.id == id).Sum(x => x.CountStore);
                    findBook.AllCount++;

                    if (summList < summ)
                    {
                        count++;
                        tbCount.Text = count.ToString();
                        price += book.Cost;
                        tbPrice.Text = price.ToString();
                        if (tbPrice.Text != "" || tbSalePrice.Text != "")
                        {
                            tbRub.Visibility = Visibility.Visible;
                        }
                        lbBooks.Items.Refresh();
                        tbPrice.Text = tbPrice.Text.Replace(".", ",");
                        CalcSale calc = new CalcSale();
                        int countB = Convert.ToInt32(tbCount.Text);
                        decimal costB = Convert.ToDecimal(tbPrice.Text);
                        Sale = calc.Calc(countB, costB);
                        foreach (BookShop booke in books)
                        {
                            booke.Sale = Sale;
                        }
                    }
                    else
                        MessageBox.Show("Данный товар невозможно приобрести!");
                }
                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 pgBasket(books));
        }
    }
}
