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 ClassLibrary_CountingSale;

namespace modulekz
{
    /// <summary>
    /// Логика взаимодействия для ListBook.xaml
    /// </summary>
    public partial class ListBook : Page
    {

        VModel VM = new VModel();
        double sale = 0;
        int kol_vo = 0;
        List<Book> tableBooks = new List<Book>();

        public ListBook(List<Book> books)
        {
            InitializeComponent();
            sale = 0;
            tableBooks = books;
            ListBooks.ItemsSource = VM.books;
            int ii = 0;
            double c = 0;
            foreach (Book b in books)
            {
                ii += b.inc;
                sale = b.sale;
                c += Math.Floor(Convert.ToDouble(b.Cost) * b.inc);
            }
            kol_vo = ii;
            RKol_v.Text = kol_vo.ToString();
            Rsale.Text = sale.ToString();
            FirstCost.Text = c.ToString();
            if (sale > 0)
            {
                FirstCost.TextDecorations = TextDecorations.Strikethrough;
                SecCost.Text = Math.Floor(c - c * (sale / 100)).ToString();
            }
        }

        /// <summary>
        /// Переход в корзину
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Gocart_Click(object sender, RoutedEventArgs e)
        {
            LoadPage.MainFrame.Navigate(new Cart(tableBooks, sale));
        }

        /// <summary>
        /// Добавление позиции в корзину
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddCart_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Controls.Button btn = sender as System.Windows.Controls.Button;
            int uid = Convert.ToInt32(btn.Uid);

            AddCart(uid);
        }

        /// <summary>
        /// Модуль пересчета стоимости, скидки, количества позиции
        /// </summary>
        /// <param name="i"></param>
        public void AddCart(int i)
        {
            List<Book> bl;
            try
            {
                bl = BaseConnect.BaseModel.Book.ToList();
                Book book = BaseConnect.BaseModel.Book.FirstOrDefault(x => x.Id == i);
                if ((book.Stock < 0) && (book.Store < 0))
                {
                    MessageBox.Show("Данного товара на данный момент нет!");
                    throw new Exception();
                }
                else if (book.inc == (book.Stock + book.Store))
                {
                    MessageBox.Show("Превышен лимит");
                    throw new Exception();
                }
                book.inc += 1;

                kol_vo += 1;

                RKol_v.Text = kol_vo.ToString();


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

                    c += Math.Floor(Convert.ToDouble(b.Cost) * b.inc);
                }
                FirstCost.Text = c.ToString();



                sale = SaleCount.saleCount(kol_vo, c);
                Rsale.Text = sale.ToString();

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


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


        }
    }
}
