using MathSale;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace TestDemo
{
    /// <summary>
    /// Логика взаимодействия для PgBooks.xaml
    /// </summary>
    public partial class PgBooks : Page, INotifyPropertyChanged
    {

        ViewModel ViewModel = new ViewModel();
        public int CountTakedBooks { get; set; }
        public decimal ItogPriceNonSale { get; set; } = 0;
        public decimal PriceSale { get; set; } = 0;
        public decimal Sale { get; set; } = 0;
        public string Visible { get; set; }

        public string TextDecor { get; set; }

        Sale sale = new Sale();
        List<books> takedBooks = new List<books>();

        public event PropertyChangedEventHandler PropertyChanged;

        public PgBooks()
        {
            try
            {
                InitializeComponent();
                MailList.ItemsSource = ViewModel.BuildBooks();
            }
            catch
            {

            }

            }

        private void BtnAddCart_Click(object sender, RoutedEventArgs e)
        {
            try
            {


                Button button = (Button)sender;
                int id = Convert.ToInt32(button.Uid);
                books book = BaseModel.BaseConnect.books.FirstOrDefault(x => x.id_book == id);

                if ((book.count_in_stock <= 0) && (book.count_in_shop <= 0))
                {
                    throw new Exception("Данный товар закончился");
                }
                if (book.CountInCart+1 > (book.count_in_shop + book.count_in_stock))
                {
                    throw new Exception("Превышен лимит товара");
                }
                book.CountInCart++;
                
                takedBooks.Add(book);



                CountTakedBooks = takedBooks.Count;
                PropertyChanged(this, new PropertyChangedEventArgs("CountTakedBooks"));
                decimal price = 0;

                foreach (books booki in takedBooks.Distinct().ToList())
                {
                    price += (decimal)booki.price * booki.CountInCart;
                }
                ItogPriceNonSale = price;
                PropertyChanged(this, new PropertyChangedEventArgs("ItogPriceNonSale"));
                Sale sale = new Sale();
                Sale = (int)sale.sale(takedBooks.Count, ItogPriceNonSale);
                PropertyChanged(this, new PropertyChangedEventArgs("Sale"));
                if (Sale != 0)
                {
                    PriceSale = ItogPriceNonSale - (ItogPriceNonSale * Sale / 100);
                    TextDecor = "Strikethrough";
                    TxtYesSale.Visibility = Visibility.Visible;
                    PropertyChanged(this, new PropertyChangedEventArgs("TextDecor"));
                }
                PropertyChanged(this, new PropertyChangedEventArgs("PriceSale"));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

        private void BtnGoCart_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                new WinCart(takedBooks.Distinct().ToList(), Sale).ShowDialog();
            }
            catch
            {

            }
        }
    }
}
