using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using WpfApp1.model;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private string searchFilter = "";

        private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
        {
            searchFilter = SearchFilterTextBox.Text;
            Invalidate();
        }
        private bool sortAsc = true;

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

        public event PropertyChangedEventHandler PropertyChanged;
        private void Invalidate()
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("CarList"));
        }
        public string selectedname = "Все автомобили";
        public Carsprice? selectedprice = null;

        private IEnumerable<cars> _CarList;
        public IEnumerable<cars> CarList
        {
            get
            {
                var res = _CarList;

                res = res
                    .Where(c => (c.name == selectedname || selectedname == "Все автомобили"))
                    .Where(c => (selectedprice == null || (c.price >= selectedprice.priceFrom && c.price < selectedprice.priceTo)));

                if (searchFilter != "")
                    res = res.Where(c => c.name.IndexOf(
                        searchFilter,
                        StringComparison.OrdinalIgnoreCase) >= 0);
                if (sortAsc) res = res.OrderBy(c => c.year);
                else res = res.OrderByDescending(c => c.year);

                return res;

            }
            set
            {
                _CarList = value;
            }
        }

        public List<cars> Cars { get; set; }
        public List<Namecar> CarinfoList { get; set; }
        public List<Carsprice> CarpriceList { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            Style buttonStyle = new Style();
            buttonStyle.Setters.Add(
                new Setter
                {
                    Property = Control.FontFamilyProperty,
                    Value = new FontFamily("Verdana")
                });
            buttonStyle.Setters.Add(
                new Setter
                {
                    Property = Control.MarginProperty,
                    Value = new Thickness(10)
                });
            buttonStyle.Setters.Add(
                new Setter
                {
                    Property = Control.BackgroundProperty,
                    Value = new SolidColorBrush(Colors.Black)
                });
            buttonStyle.Setters.Add(
                new Setter
                {
                    Property = Control.ForegroundProperty,
                    Value = new SolidColorBrush(Colors.White)
                });
            buttonStyle.Setters.Add(
                new EventSetter
                {
                    Event = Button.ClickEvent,
                    Handler = new RoutedEventHandler(Button_Click)
                });

            ExitButton.Style = buttonStyle;

            DataContext = this;
            Globals.dataProvider = new JSONDataProvider();
            CarList = Globals.dataProvider.getCars();
            CarinfoList = Globals.dataProvider.getName().ToList();
            CarinfoList.Insert(0, new Namecar { title = "Все автомобили" });
            CarpriceList = Globals.dataProvider.getPrice().ToList();
        }

        private string currentStyle = "StackStyle";

        private void ToggleView_Click(object sender, RoutedEventArgs e)
        {
            currentStyle = currentStyle == "StackStyle" ? "WrapStyle" : "StackStyle";
            var newStyle = (Style)TryFindResource(currentStyle);
            if (newStyle != null)
                carListBox.Style = newStyle;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button clickedButton = (Button)sender;
            MessageBox.Show(clickedButton.Content.ToString());
        }
        private void ExitButton_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();

        }

        private void carnameFilterComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            selectedname = (carnameFilterComboBox.SelectedItem as Namecar).title;
            Invalidate();
        }
        private void carpriceFilterComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            selectedprice = carpriceFilterComboBox.SelectedItem as Carsprice;
            Invalidate();
        }
    }
    interface IDataProvider
    {
        IEnumerable<cars> getCars();
        IEnumerable<Namecar> getName();
        IEnumerable<Carsprice> getPrice();
    }
    public class LocalDataProvider : IDataProvider
    {
        public IEnumerable<Namecar> getName()
        {
            return new Namecar[]
            {
                new Namecar()
                {
                    title="Toyota Trueno AE86"

                },
                new Namecar()
                {
                    title="Toyota Supra A80"
                },
                new Namecar()
                {
                    title="Nissal Skyline R34"
                },
                new Namecar()
                {
                    title="Nissan Silvia S15"

                },
                new Namecar()
                {
                    title="Toyota Camry 3.5"
                },
                new Namecar()
                {
                    title="Audi RS 6"

                },
                new Namecar()
                {
                    title="Трактор LOVOL TE354 HT"
                },
                new Namecar()
                {
                    title="BMW M5 F90"
                },
                new Namecar()
                {
                    title="BMW E36"
                },
                new Namecar()
                {
                    title="Daewoo Matiz"
                }
            };
        }
        public IEnumerable<Carsprice> getPrice()
        {
            return new Carsprice[]
            {
                new Carsprice()
                {
                    title="Все цены",
                    priceFrom=0,
                    priceTo=70000
                },
                new Carsprice()
                {
                    title="Малая цена",
                    priceFrom=0,
                    priceTo=15001

                },
                new Carsprice()
                {
                    title="Средняя цена",
                    priceFrom=20000,
                    priceTo=35000

                },
                new Carsprice()
                {
                    title="Высокая цена",
                    priceFrom=35001,
                    priceTo=70000

                }
            };
        }
        public IEnumerable<cars> getCars()
        {
            return new cars[] {
                new cars {
                price = 15000,
                name = "Toyota Trueno AE86",
                year = 1995,
                },
                new cars {
                price = 30000,
                name = "Toyota Supra A80",
                year = 1996,
                },
                new cars {
                price = 25000,
                name = "Nissal Skyline R34",
                year = 1996
                },
                new cars {
                price = 20000,
                name = "Nissan Silvia S15",
                year = 1999,
                },
                new cars {
                price = 20000,
                name = "Toyota Camry 3.5",
                year = 2020,
                },
                new cars {
                price = 30000,
                name = "Audi RS 6",
                year = 2016,
                },
                new cars {
                price = 66666,
                name = "Трактор LOVOL TE354 HT",
                year = 2024,
                },
                new cars {
                price = 30000,
                name = "BMW M5 F90",
                year = 2019,
                },
                new cars {
                price = 35000,
                name = "BMW E36",
                year = 2006,
                },
                new cars {
                price = 20000,
                name = "Daewoo Matiz",
                year = 2010,
                },
            };
        }
    }
}