using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace UP_Session1_Belov.Models
{
    internal class ProductListModel : INotifyPropertyChanged
    {
        List<ProductModel> products = new List<ProductModel>();

        private List<FilterModel> filters = new List<FilterModel>();

        private int idFilterModel;

        private int allCount;

        public ProductListModel()
        {
            using(UP_41P_BelovRAEntities db = new UP_41P_BelovRAEntities())
            {
                List<Product> productsFromBase = db.Product.Include(x => x.Manufacturer).ToList();
                foreach(var product in productsFromBase)
                {
                    products.Add(new ProductModel(product.ProductArticleNumber,
                                                  product.ProductName,
                                                  product.ProductDescription,
                                                  product.Manufacturer.ManufacturerName,
                                                  Convert.ToDouble(product.ProductCost),
                                                  Convert.ToDouble(product.ProductDiscountAmount),
                                                  product.ProductPhoto));
                }

                filters.Add(new FilterModel(0, 0, 0, "Все"));
                filters.Add(new FilterModel(1, 0, 9.99, "0-9.99%"));
                filters.Add(new FilterModel(2, 10, 14.99, "10-14.99%"));
                filters.Add(new FilterModel(3, 15, 100, "15% и более"));

                idFilterModel = 0;
                Sort();

                allCount = products.Count;
            }
        }

        public List<ProductModel> Products
        {
            get => products; set
            {
                products = value;
                OnPropertyChanged("Products");
            }
        }

        public void Sort()
        {
            List<ProductModel> baseList = new List<ProductModel>();
            using (UP_41P_BelovRAEntities db = new UP_41P_BelovRAEntities())
            {
                List<Product> productsBase = db.Product.ToList();

                foreach (var product in productsBase)
                {
                    baseList.Add(
                        new ProductModel(product.ProductArticleNumber, product.ProductName,
                        product.ProductDescription, product.Manufacturer.ManufacturerName, Convert.ToDouble(product.ProductCost),
                        Convert.ToDouble(product.ProductDiscountAmount), product.ProductPhoto)
                        );
                }
                switch (IdFilterModel)
                {
                    case 0: break;
                    case 1:
                        baseList = baseList.Where(x => x.Discount >= filters[1].FirstRange && x.Discount <= filters[1].SecondRange).ToList();
                        break;
                    case 2:
                        baseList = baseList.Where(x => x.Discount >= filters[2].FirstRange && x.Discount <= filters[2].SecondRange).ToList();
                        break;
                    case 3:
                        baseList = baseList.Where(x => x.Discount >= filters[3].FirstRange && x.Discount <= filters[3].SecondRange).ToList();
                        break;
                    default:
                        break;
                }
            }
            Products = baseList;
        }

        public List<FilterModel> Filters { get => filters; set => filters = value; }
        public int IdFilterModel
        {
            get => idFilterModel;
            set
            {
                idFilterModel = value;
                OnPropertyChanged("IdFilterModel");
                Sort();
            }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
