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>
    /// Логика взаимодействия для pgTrash.xaml
    /// </summary>
    public partial class pgTrash : Page
    {
        List<Books> bookBuy;
        bool CheckBack = true;
        public pgTrash(List<Books> bookBuy)
        {
            InitializeComponent();
            this.bookBuy = bookBuy;
            lbBooksBuy.ItemsSource = bookBuy;
        }
        private void btnDeleteBook_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)sender;
            int id = Convert.ToInt32(btn.Uid);
            Books delBook = bookBuy.FirstOrDefault(x => x.id == id);
            bookBuy.Remove(delBook);
            lbBooksBuy.Items.Refresh();
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                TextBox tb = (TextBox)sender;
                int id = Convert.ToInt32(tb.Uid);
                Books book = bookBuy.FirstOrDefault(x => x.id == id);
                int tmpValue = book.FullCount;
                try
                {
                    Books checkCountBook = BaseConnect.BaseModel.Books.FirstOrDefault(x => x.id == id);
                    int Base = checkCountBook.StoreCount + checkCountBook.StockCount;
                    if (Base >= Convert.ToInt32(tb.Text))
                    {
                        Calculator cs = new Calculator();
                        book.FullCount = Convert.ToInt32(tb.Text);
                        int count = 0;
                        decimal price = 0;
                        foreach (Books bok in bookBuy)
                        {
                            price += bok.Price * bok.FullCount;
                            count += bok.FullCount;

                        }
                        foreach (Books booke in bookBuy)
                        {
                            booke.Sale = cs.Calc(price, count);
                        }
                        lbBooksBuy.ItemsSource = bookBuy;
                        lbBooksBuy.Items.Refresh();

                    }
                    else
                    {
                        MessageBox.Show("Такого кол-ва нет!");
                        tb.Text = tmpValue.ToString();
                    }

                }
                catch { }
            }
            catch 
            {

            }
           
        }
        private void btnBuy_Click(object sender, RoutedEventArgs e)
        {

            int nOrder, count = 0, sale = 0;
            bool flagMessageOrder = false;
            if (BaseConnect.BaseModel.Ordering.Count() != 0)
                nOrder = BaseConnect.BaseModel.Ordering.Max(x => x.SerialNumber) + 1;
            else
                nOrder = 1;
           
            decimal CountCost = 0;
            foreach (Books book in bookBuy)
            {

                Books bk = BaseConnect.BaseModel.Books.FirstOrDefault(x => x.id == book.id);
                if (bk.StoreCount >= book.FullCount)
                {
                    flagMessageOrder = true;
                    bk.StoreCount -= book.FullCount;
                    BaseConnect.BaseModel.SaveChanges();
                }
                else
                {
                    int res = book.FullCount - bk.StoreCount;
                    bk.StoreCount = 0;
                    bk.StockCount -= res;
                    BaseConnect.BaseModel.SaveChanges();
                }
                Ordering or = new Ordering();
                or.IdBooks = book.id;
                or.SerialNumber = nOrder;
                or.Count = book.FullCount;
                count += book.FullCount;
                sale = book.Sale;
                CountCost += book.Price * book.FullCount;
                or.DateOrdering = DateTime.Now;
                BaseConnect.BaseModel.Ordering.Add(or);
            }
            if (flagMessageOrder)
                MessageBox.Show("Номер заказа: " + nOrder + "\nЧасть заказа будет готова к выдаче: " + DateTime.Now.ToShortDateString() + "\nДругая часть заказа готова к выдаче: " + DateTime.Now.AddDays(3).ToShortDateString() + "\nЗаказ зарезервирован до: " + DateTime.Now.AddDays(7).ToShortDateString() + "\n Итоговое количество книг: " + count + "\nИтоговая цена:" + Math.Floor(CountCost - (CountCost * sale / 100)) + "\nСкидка:" + sale);
            else
            {
                MessageBox.Show("Номер заказа: " + nOrder + "\nВыдача готова: " + DateTime.Now.ToShortDateString() + "\nЗаказ зарезервирован до: " + DateTime.Now.AddDays(7).ToShortDateString() + "\n Итоговое количество книг: " + count + "\nИтоговая цена:" + Math.Floor(CountCost - (CountCost * sale / 100)) + "\nСкидка:" + sale);
            }
            bookBuy.Clear();
            lbBooksBuy.Items.Refresh();
            CheckBack = false;
        }

        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            bookBuy.Clear();
            lbBooksBuy.Items.Refresh();
            CheckBack = false;
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            if (!CheckBack)
                LoadPages.goPage.Navigate(new pgBooksCatalog());
            else
                LoadPages.goPage.GoBack();
        }
    }
}
