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

namespace Exxxam
{
    public partial class ProductList : Page
    {
        public ProductList()
        {
            InitializeComponent();
            productList.ItemsSource = DataBaseConnection.dataBaseEntities.Products.ToList();
        }

        private void oldPrice_Loaded(object sender, RoutedEventArgs e)
        {
            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)
        {
            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 currentDiscount_Loaded(object sender, RoutedEventArgs e)
        {
            TextBlock currentDiscount = (TextBlock)sender;

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

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

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

        private List<Products> _shoppingCartProduct = new List<Products>();

        private void addToShoppingCart_Click(object sender, RoutedEventArgs e)
        {
            Products selectedProduct = productList.SelectedItem as Products;

            bool checkSameProduct = false;

            foreach (Products product in _shoppingCartProduct)
            {
                if (selectedProduct == product)
                {
                    checkSameProduct = true;
                    product.Count++;
                    break;
                }
            }

            if (checkSameProduct)
            {
                checkSameProduct = false;
            }
            else
            {
                selectedProduct.Count = 1;
                _shoppingCartProduct.Add(selectedProduct);
            }

            if (_shoppingCartProduct.Count > 0)
            {
                openShoppingCart.Visibility = Visibility.Visible;
            }
        }
    }
}
