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;
using DllSale;

namespace VorobiovExam01
{
    /// <summary>
    /// Логика взаимодействия для BookListPage.xaml
    /// </summary>
    public partial class BookListPage : Page
    {
        ViewModel VM = new ViewModel();
        List<Books> books = new List<Books>();

        double sale = 0;
        int count = 0;
        public BookListPage(List<Books> book)
        {
            InitializeComponent();
            load();
        }

        public void load()
        {
            VM = new ViewModel();
            BookList.ItemsSource = VM.books;
            DataContext = VM;
        }
        private void BtnClick_AddToCart(object sender, RoutedEventArgs e)
        {

            System.Windows.Controls.Button btn = sender as System.Windows.Controls.Button;
            int uid = Convert.ToInt32(btn.Uid);

            cart(uid);
        }

        public void cart(int i)
        {
            List<Books> bl;
            try
            {
                bl = BaseConnect.BaseModel.Books.ToList();
                Books book = BaseConnect.BaseModel.Books.FirstOrDefault(x => x.ID == i);
                if ((book.CountInStock < 0) && (book.CountInStore < 0))
                {
                    MessageBox.Show("Данного товара на данный момент нет!");
                    throw new Exception();
                }
                else if (book.InCost == (book.CountInStock + book.CountInStore))
                {
                    MessageBox.Show("Превышен лимит");
                    throw new Exception();
                }
                book.InCost += 1;

                count += 1;

                RKol_v.Text = count.ToString();


                double c = 0;
                foreach (Books b in bl)
                {

                    c += Convert.ToDouble(b.Cost) * b.InCost;
                }
                FirstCost.Text = c.ToString();
                CalcSale Sale = new CalcSale();

                sale = Sale.sale(count, c);
                Rsale.Text = sale.ToString();

                if (sale > 0)
                {
                    FirstCost.TextDecorations = TextDecorations.Strikethrough;
                    SecCost.Text = (c - c * (sale / 100)).ToString();
                }
                book.sale = sale;
                books.Remove(book);
                books.Add(book);

            }
            catch { }
            bl = BaseConnect.BaseModel.Books.ToList();

        }

        private void BtnClick_Cart(object sender, RoutedEventArgs e)
        {
            LoadPages.MainFrame.Navigate(new CartPage(books, count));
        }
    }
}
