using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace SergechevExam
{
    /// <summary>
    /// Логика взаимодействия для ShoppingCart.xaml
    /// </summary>
    public partial class ShoppingCart : Window
    {
        public ShoppingCart(List<Products> selectedProducts)
        {
            InitializeComponent();

            productList.ItemsSource = selectedProducts.ToList();
        }

        private void oldPrice_Loaded(object sender, RoutedEventArgs e) // Проверка на наличие скидки у товара. Если она есть, то отрисовывается перечеркнутый TB, в котором старая цена
        {
            TextBlock oldPrice = (TextBlock)sender;

            int indexProduct = Convert.ToInt32(oldPrice.Uid);

            Products currentProduct = DataBaseConnection.dataBaseEntities.Products.FirstOrDefault(x => x.Code_Product == indexProduct);

            if (currentProduct.Discount_Product != 0)
            {
                oldPrice.Visibility = Visibility.Visible;
            }
        }

        private void mainPrice_Loaded(object sender, RoutedEventArgs e) // Проверка на наличие скидки у товара. Если она есть, то рядом с перечеркнутым TB отрисовывается новая цена
        {
            TextBlock mainPrice = (TextBlock)sender;

            int indexProduct = Convert.ToInt32(mainPrice.Uid);

            Products currentProduct = DataBaseConnection.dataBaseEntities.Products.FirstOrDefault(x => x.Code_Product == indexProduct);

            if (currentProduct.Discount_Product > 0)
            {
                mainPrice.Text = $"{currentProduct.Price_Product - (currentProduct.Price_Product * currentProduct.Discount_Product)}";
            }
        }

        private void discount_Loaded(object sender, RoutedEventArgs e)
        {
            TextBlock discount = (TextBlock)sender;

            int indexProduct = Convert.ToInt32(discount.Uid);

            Products currentProduct = DataBaseConnection.dataBaseEntities.Products.FirstOrDefault(x => x.Code_Product == indexProduct);

            if (currentProduct.Discount_Product != 0)
            {
                discount.Visibility = Visibility.Visible;
                discount.Text = $"Скидка: {Convert.ToInt32(currentProduct.Discount_Product * 100)}%";
            }
        }

        private void addToCart_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
