using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WpfApp2.model;
using System.IO;
using System.Runtime.Serialization.Json;
namespace WpfApp2
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void Invalidate()
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("SparesList"));
}
public string selectedDetail = "Деталь";
public SparesPrice? selectedPrice = null;
public string selectedType = "Тип ТС";
private IEnumerable _SparesList;
public IEnumerable SparesList
{
get
{
var res = _SparesList;
res = res
.Where(c => (c.detail == selectedDetail || selectedDetail == "Деталь"))
.Where(c => (selectedPrice == null || (c.price >= selectedPrice.PriceFrom && c.price < selectedPrice.PriceTo)))
.Where(c => (c.type == selectedType || selectedType == "Тип ТС"));
if (searchFilter != "")
res = res.Where(c => c.name.IndexOf(
searchFilter,
StringComparison.OrdinalIgnoreCase) >= 0);
if (sortAsc) res = res.OrderBy(c => c.price);
else res = res.OrderByDescending(c => c.price);
return res;
}
set
{
_SparesList = value;
}
}
public List SparesDetailList { get; set; }
public List SparesPriceList { get; set; }
public List SparesTypeList { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
Globals.dataProvider = new JSONDataProvider();
SparesList = Globals.dataProvider.getSpares();
SparesDetailList = Globals.dataProvider.getDetail().ToList();
SparesDetailList.Insert(0, new SparesDetail { title = "Деталь" });
SparesPriceList = Globals.dataProvider.getPrice().ToList();
SparesTypeList = Globals.dataProvider.getType().ToList();
SparesTypeList.Insert(0, new SparesType { title = "Тип ТС" });
}
public class JSONDataProvider : LocalDataProvider, IDataProvider
{
private List _SparesList;
public JSONDataProvider()
{
var serializer = new DataContractJsonSerializer(typeof(Spares[]));
using (var sr = new StreamReader("./data.json"))
{
_SparesList = ((Spares[])serializer.ReadObject(sr.BaseStream)).ToList();
}
}
public IEnumerable getSpares()
{
return _SparesList;
}
}
private bool sortAsc = true;
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
sortAsc = (sender as RadioButton).Tag.ToString() == "1";
Invalidate();
}
private void ExitButton_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
private void DetailFilterComboBox_SelectionChanged_1(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
selectedDetail = (DetailFilterComboBox.SelectedItem as SparesDetail).title;
Invalidate();
}
private void PriceFilterComboBox_SelectionChanged_2(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
selectedPrice = PriceFilterComboBox.SelectedItem as SparesPrice;
Invalidate();
}
private void TypeFilterComboBox_SelectionChanged_3(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
selectedType = (TypeFilterComboBox.SelectedItem as SparesType).title;
Invalidate();
}
private string searchFilter = "";
private void SearchFilter_KeyUp(object sender, KeyEventArgs e)
{
searchFilter = SearchFilterTextBox.Text;
Invalidate();
}
}
interface IDataProvider
{
IEnumerable getSpares();
IEnumerable getDetail();
IEnumerable getPrice();
IEnumerable getType();
}
public class LocalDataProvider : IDataProvider
{
public IEnumerable getDetail()
{
return new SparesDetail[]
{
new SparesDetail()
{
title="Двигатель"
},
new SparesDetail()
{
title="КПП"
},
new SparesDetail()
{
title="Подвеска"
},
new SparesDetail()
{
title="Стекла"
},
new SparesDetail()
{
title="Колеса"
},
};
}
public IEnumerable getPrice()
{
return new SparesPrice[]
{
new SparesPrice()
{
title="Все цены",
PriceFrom=0,
PriceTo=99999999
},
new SparesPrice()
{
title="БУ",
PriceFrom=0,
PriceTo=10000
},
new SparesPrice()
{
title="Дешевая",
PriceFrom=10000,
PriceTo=20000
},
new SparesPrice()
{
title="Средняя",
PriceFrom=20000,
PriceTo=50000
},
new SparesPrice()
{
title="Элитная",
PriceFrom=50000,
PriceTo=99999999
}
};
}
public IEnumerable getType()
{
return new SparesType[]
{
new SparesType()
{
title="Легкая"
},
new SparesType()
{
title="Грузовая"
},
};
}
public IEnumerable getSpares()
{
return new Spares[]{
new Spares{
type="Легкая",
name="Двигатель МБ140(ОК2281.4)",
price = 72000,
detail="Двигатель"},
new Spares{
type="Легкая",
name="КПП 5-СТ РС-ПДР",
price = 20000,
detail="КПП"},
new Spares{
type="Грузовая",
name="Подвеска ПДРС1337",
price = 15000,
detail="Подвеска"},
new Spares{
type="Легкая",
name="Шина 15-Р",
price = 3000,
detail="Колеса"},
new Spares{
type="Грузовая",
name="Лобовое стекло СТ150",
price = 40000,
detail="Стекла"},
};
}
}
}