using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WpfApp2;
using WpfApp3.Class;

namespace WpfApp3
{
    public partial class MainWindow : Window
    {
        private JSONDataProvider dataProvider;
        private InstrumentFilter filter;
        private string searchFilter = "";
        private bool sortAsc = true;

        public MainWindow()
        {
            InitializeComponent();

            dataProvider = new JSONDataProvider();
            filter = new InstrumentFilter();

            FilterComboBox.Items.Add("Все");
            FilterComboBox.Items.Add("Клавишные");
            FilterComboBox.Items.Add("Струнные");
            FilterComboBox.Items.Add("Ударные");
            FilterComboBox.SelectedIndex = 0;

            UpdateInstrumentList(dataProvider.GetInstruments());
        }
        private void FilterComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string selectedFilter = (string)FilterComboBox.SelectedItem;

            if (selectedFilter == "Все")
            {
                UpdateInstrumentList(filter.FilterAll(dataProvider.GetInstruments()));
            }
            else
            {
                UpdateInstrumentList(filter.FilterByType(dataProvider.GetInstruments(), selectedFilter));
            }
        }
        private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
        {
            searchFilter = SearchFilterTextBox.Text;
            UpdateInstrumentList(dataProvider.GetInstruments());
        }

        private void RadioButtonAsc_Checked(object sender, RoutedEventArgs e)
        {
            sortAsc = true;
            UpdateInstrumentList(dataProvider.GetInstruments());
        }
        private void RadioButtonDesc_Checked(object sender, RoutedEventArgs e)
        {
            sortAsc = false;
            UpdateInstrumentList(dataProvider.GetInstruments());
        }
        private void UpdateInstrumentList(List<MusicalInstrument> instrumentsToDisplay)
        {
            if (!string.IsNullOrEmpty(searchFilter))
            {
                instrumentsToDisplay = instrumentsToDisplay.Where(i => i.Name.IndexOf(searchFilter, StringComparison.OrdinalIgnoreCase) >= 0 ||
                i.Type.IndexOf(searchFilter, StringComparison.OrdinalIgnoreCase) >= 0 ||
                i.Brand.IndexOf(searchFilter, StringComparison.OrdinalIgnoreCase) >= 0).ToList();
            }

            if (sortAsc)
            {
                instrumentsToDisplay = instrumentsToDisplay.OrderBy(i => i.Price).ToList();
            }
            else
            {
                instrumentsToDisplay = instrumentsToDisplay.OrderByDescending(i => i.Price).ToList();
            }

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

    }
}