using System.ComponentModel;
using System.Text;
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 wpf_app.Model.Windows;
using wpf_app.Model;

namespace wpf_app
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private IEnumerable<Product> _productList;
        public IEnumerable<Product> ProductList
        {
            get
            {
                var res = _productList;
                res = _productList
                    .Where(p =>(selectedCategory == "Все категории" || p.Category == selectedCategory))
                    .Where(p =>(selectedPrice == null || (p.Price>=selectedPrice.priceFrom && p.Price<selectedPrice.priceTo)))
                    .Where(p =>(selectedCompany == "Все компании" || p.Company == selectedCompany));

                if (searchFilter != "")
                    res = res.Where(c => c.Title.IndexOf(searchFilter, StringComparison.OrdinalIgnoreCase) >= 0);

                if (sortAsc) res = res.OrderBy(p => p.Price);
                else res = res.OrderByDescending(p => p.Price);

                return res;
            }
            set
            {
                _productList = value;
            }
        }
        public List <ProductCategory> CategoryList { get; set; }
        public List <ProductPrice> ProductPrices { get; set; }
        public List <ProductCompany> ProductCompanies { get; set; }
        string selectedCategory = "";
        string selectedCompany = "";
        private ProductPrice? selectedPrice = null;
        private string searchFilter = "";
        private bool sortAsc = true;
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            Globals.dataProvider = new JSONDataProvider(); 
            ProductList = Globals.dataProvider.GetProducts();
            ProductPrices = Globals.dataProvider.GetPrices().ToList();
            CategoryList = Globals.dataProvider.GetCategories().ToList();
            CategoryList.Insert(0, new ProductCategory { title = "Все категории" });
            ProductCompanies = Globals.dataProvider.GetCompanies().ToList();
            ProductCompanies.Insert(0, new ProductCompany { title = "Все компании" });
        }

        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void CategoryFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            selectedCategory = (CategoryFilterComboBox.SelectedItem as ProductCategory).title;
            Invalidate();
        }

        private void PriceFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            selectedPrice = PriceFilterComboBox.SelectedItem as ProductPrice;
            Invalidate();
        }

        private void CompanyFilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            selectedCompany = (CompanyFilterComboBox.SelectedItem as ProductCompany).title;
            Invalidate();
        }

        private void SearchFilterTextBox_KeyUp(object sender, KeyEventArgs e)
        {
            searchFilter = SearchFilterTextBox.Text;
            Invalidate();
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            sortAsc = (sender as RadioButton).Tag.ToString() == "1";
            Invalidate();
        }

        private void productsListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var productDetailsWindow = new ProductDetailsWindow(productsListBox.SelectedItem as Product);
            productDetailsWindow.Show();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void Invalidate()
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("ProductList"));
        }
    }

    class Globals
    {
        public static IDataProvider dataProvider;
    }
}