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 modulekz
{
    /// <summary>
    /// Логика взаимодействия для Cart.xaml
    /// </summary>
    public partial class Cart : Page
    {
        List<Book> bb = new List<Book>();
    

        public Cart(List<Book> books, double sale)
        {
            InitializeComponent();

            
            foreach (Book b in books) //пересчет скидки
            {
                if (sale > 0)
                {
                    b.sale = sale;
                    b.decor = "";
                }
                else
                { }
            }
            bb = books;
            ListBook.ItemsSource = books;
        }

        /// <summary>
        /// Покупка книг
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Buy_Click(object sender, RoutedEventArgs e)
        {
            int o = BaseConnect.BaseModel.BuyOrder.Max(x => x.id) + 1;
            int allBook = 0, sale = 0, thrfl = 0;
            double allSum = 0, ss = 0;
            int flag = 0, firstfl = 0, secfl = 0;
            foreach (Book b in bb)
            {

                BuyOrder order = new BuyOrder() { IdBook = b.Id, Count = b.inc, DateOrder = DateTime.Now };
                BaseConnect.BaseModel.BuyOrder.Add(order);
                allBook += b.inc;
                if (b.inc < b.Store)
                {
                    b.Store -= b.inc;
                    flag = 0;
                }
                else
                {
                    if (b.Store > 0)
                        flag = 1;
                    else
                        flag = 2;
                    b.inc -= b.Store;
                    b.Store = 0;
                    b.Stock -= b.inc;
                }

                if (flag == 0)
                {
                    firstfl = 1;
                }
                else if (flag == 1)
                {
                    secfl = 1;
                }
                else
                {
                    thrfl = 1;
                }



                allSum += Convert.ToDouble(b.inc) * Convert.ToDouble(b.Cost);
                sale = Convert.ToInt32(b.sale);
                if (sale > 0)

                    ss = Convert.ToDouble(sale) / 100;


                b.inc = 0;
            }


            allSum = Math.Floor(allSum - (allSum * ss));
            string mesto = "";
            if (firstfl == 1 && secfl == 0 && thrfl == 0)
                mesto = "на кассе";
            else if (secfl == 1 || (thrfl == 1 && firstfl == 1))
                mesto = "частично на кассе, вторую часть заказа можно получить через 72 часа (" + DateTime.Now.AddDays(3) + ")";
            else
                mesto = "через 72 часа (" + DateTime.Now.AddDays(3) + ")";



            BaseConnect.BaseModel.SaveChanges();
            bb.Clear();
            ListBook.Items.Refresh();
            MessageBox.Show("Заказ №" +
               o + " оформлен!\n" +
               "Забрать его можно " + mesto +
                "\nОбщее количество книг в заказе: " + allBook +
                "\nОбщая цена: " + allSum +
                "\nСкидка: " + sale + "%" +
                "\nЗаказ зарезервирован до: " + DateTime.Now.AddDays(7));
            LoadPage.MainFrame.Navigate(new ListBook(bb));

        }

        /// <summary>
        /// Очищение корзины
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            foreach (Book b in bb)
            {
                b.inc = 0;
            }
            bb.Clear();
            ListBook.Items.Refresh();
        }

        /// <summary>
        /// Переход назад
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Back_Click(object sender, RoutedEventArgs e)
        {
            LoadPage.MainFrame.Navigate(new ListBook(bb));
        }

        private void del_Click(object sender, RoutedEventArgs e)
        {


        }

        private void add_Click(object sender, RoutedEventArgs e)
        {


        }
        /// <summary>
        /// Удаление позиции
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Delet_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            int id = Convert.ToInt32(button.Uid);
            Book book = bb.FirstOrDefault(x => x.Id == id);
            book.inc = 0;
            bb.Remove(book);
            ListBook.Items.Refresh();
        }
    }
}
