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 VorobiovExam01
{
    /// <summary>
    /// Логика взаимодействия для CartPage.xaml
    /// </summary>
    public partial class CartPage : Page
    {
        List<Books> book = new List<Books>();
        public CartPage(List<Books> books, double sale)
        {
            InitializeComponent();
            foreach (Books b in books)
            {
                if (sale > 0)
                {
                    b.sale = sale;
                    b.decor = "";
                }
                else
                { }
            }
            book = books;
            CartList.ItemsSource = books;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            LoadPages.MainFrame.Navigate(new BookListPage(book));

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            foreach (Books b in book)
            {
                b.InCost = 0;
            }
            book.Clear();
            CartList.Items.Refresh();

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            int id = Convert.ToInt32(button.Uid);
            Books books = book.FirstOrDefault(x => x.ID == id);
            books.InCost = 0;
            book.Remove(books);
            CartList.Items.Refresh();
        }

        private void Buy_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int o = BaseConnect.BaseModel.Order.Max(x => x.NumberOrder);
                foreach (Books b in book)
                {

                    Order order = new Order() { NumberOrder = o + 1, BookID = b.ID, Count = b.InCost, DateOrder = DateTime.Now };
                    BaseConnect.BaseModel.Order.Add(order);

                    if (b.InCost < b.CountInStore)
                    {
                        b.CountInStore -= b.InCost;
                    }
                    else
                    {
                        b.InCost -= b.CountInStore;
                        b.CountInStore = 0;
                        b.CountInStock -= b.InCost;
                    }
                    b.InCost = 0;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Что-то пошло не так \n Попробуйте вернуться позже или обратится к специалисту");
                LoadPages.MainFrame.Navigate(new BookListPage(book));          
            }



            book.Clear();
            BaseConnect.BaseModel.SaveChanges();
            CartList.Items.Refresh();
            MessageBox.Show("Ваш заказ успешно оформлен. Ждем вас для его получения");
            LoadPages.MainFrame.Navigate(new BookListPage(book));
        }
    }
}
