using System;
using System.Collections.Generic;
using System.ComponentModel;
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 WpfApp1
{
    /// <summary>
    /// Логика взаимодействия для korzinka.xaml
    /// </summary>
    public partial class YourBasket : Page, INotifyPropertyChanged
    {
        List<Books> changeBooksList = new List<Books>();
        public event PropertyChangedEventHandler PropertyChanged;
        int Sale;
        public YourBasket(List<Books> books, int sale)
        {
            InitializeComponent();
            ListBasket.ItemsSource = books;
            ListBasket.Items.Refresh();
            Sale = sale;
            changeBooksList = books;
            foreach (Books book in books)
            {
                if (book.CountInBasket == 1)
                {
                    book.Visible = "Collapsed";
                }
                if (sale == 0)
                {
                    book.Visible2 = "Collapsed";
                }
                book.TotalPriseOrder = (book.Cost * book.CountInBasket);
                book.TotalPriseOrderWithSale = book.TotalPriseOrder - (book.TotalPriseOrder * sale / 100);
                book.newDecoration = "Strikethrough";
            }
        }
        int bufCount = 0, buffCost = 0, buffCostNotSale = 0, itogCount = 0, itogCost = 0, itogCostNotSale = 0;
        DateTime dateOr;
        private void Order_Click(object sender, RoutedEventArgs e)
        {
            foreach (Books books1 in changeBooksList)
            {
                if (books1.CountInStore > books1.CountInBasket)
                {
                    books1.CountInStore -= books1.CountInBasket;
                }
                else
                {
                    int minus = books1.CountInStore - books1.CountInBasket;
                    books1.CountInStore -= books1.CountInBasket;
                    if (minus < 0)
                    {
                        books1.CountInStore = 0;
                        books1.CountInStock -= minus * -1;
                    }
                }
            }
            DataBAse.BaseModel.SaveChanges();
            DateTime today = DateTime.Now;
            foreach (Books books in changeBooksList)
            {
                bufCount = books.CountInBasket;
                buffCost = Convert.ToInt32(books.TotalPriseOrderWithSale);
                buffCostNotSale = Convert.ToInt32(books.TotalPriseOrder);
                itogCount += bufCount;
                itogCost += buffCost;
                itogCostNotSale += buffCostNotSale;
                if (books.CountInStore >= books.CountInBasket)
                {
                    dateOr = today;
                }
                else
                {
                    dateOr = today.AddHours(72);
                }
            }
            Order order = new Order()
            {
                date = dateOr,
                count = itogCount,
                totalcost = itogCost,
                sale = Sale,
                reserve = today.AddDays(7),
            };
            DataBAse.BaseModel.Order.Add(order);
            DataBAse.BaseModel.SaveChanges();
            DataBAse.BaseModel = new Entities();
            ListBasket.ItemsSource = null;
            ListBasket.Items.Refresh();
            MessageBox.Show("Данные о заказе добавлены в БД");
            ListBasket.Visibility = Visibility.Collapsed;
            Info.Visibility = Visibility.Visible;
            number.Text = "Номер заказа " + order.ID.ToString();
            dateOrder.Text = "Можно забирать " + dateOr.ToString();
            DateRezerv.Text = "Конец срока резервирования продукции " + today.AddDays(7).ToString();
            totalCostOrder.Text = "Цена заказа " + itogCostNotSale.ToString();
            totalCostSaleOrder.Text = "Процент скидки " + Sale.ToString();
            totalCostWithSaleOrder.Text = "Цена заказа со скидкой" + itogCost.ToString();
            totalCount.Text = "Итоговое количество " + itogCount.ToString();
        }

        private void Plus_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            int id = Convert.ToInt32(button.Uid);
            Books prov = DataBAse.BaseModel.Books.FirstOrDefault(x => x.IdBook == id);
                prov.CountInBasket += 1;
                if (prov.CountInBasket == 1)
                {
                prov.Visible = "Collapsed";
                }
                if (Sale == 0)
                {
                prov.Visible2 = "Collapsed";
                }
            prov.TotalPriseOrder = (prov.Cost * prov.CountInBasket);
            prov.TotalPriseOrderWithSale = prov.TotalPriseOrder - (prov.TotalPriseOrder * Sale / 100);
            prov.newDecoration = "Strikethrough";
            ListBasket.Items.Refresh();
        }

        private void Minus_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Button button = (Button)sender;
                int id = Convert.ToInt32(button.Uid);
                Books minus = DataBAse.BaseModel.Books.FirstOrDefault(x => x.IdBook == id);
                Books prov = new Books();
                minus.CountInBasket -= 1;
                    if ((minus.CountInBasket > 0) && (minus.CountInBasket < (minus.CountInStock + minus.CountInStore)))
                    {
                        if (minus.CountInBasket == 1)
                        {
                        minus.Visible = "Collapsed";
                        }
                        if (Sale == 0)
                        {
                        minus.Visible2 = "Collapsed";
                        }
                    minus.TotalPriseOrder = (minus.Cost * minus.CountInBasket);
                    minus.TotalPriseOrderWithSale = minus.TotalPriseOrder - (minus.TotalPriseOrder * Sale / 100);
                    minus.newDecoration = "Strikethrough";
                        ListBasket.Items.Refresh();
                    }
                    else if (minus.CountInBasket == 0)
                    {
                        prov = minus;
                    }
                    else
                    {
                        throw new Exception("Количество не может быть");
                    }
                changeBooksList.Remove(prov);
                ListBasket.Items.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void MainPage_Click(object sender, RoutedEventArgs e)
        {
            ChangePages.changed.Navigate(new MainPage());
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            ListBasket.ItemsSource = null;
            ListBasket.Items.Refresh();
        }
        private void Delit_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            int id = Convert.ToInt32(button.Uid);
            Books prov = DataBAse.BaseModel.Books.FirstOrDefault(x => x.IdBook == id);
            changeBooksList.Remove(prov);
            ListBasket.Items.Refresh();
        }
    }
}
