using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

namespace WpfApp1
{
    /// <summary>
    /// Логика взаимодействия для pgTours.xaml
    /// </summary>
    public partial class pgTours : Page
    {
        List<Tour> tours;
        List<Type> tourTypes;
        public pgTours()
        {
            InitializeComponent();
            BaseConnect.BaseModel = new ToursEntities();
            tours = BaseConnect.BaseModel.Tour.ToList();
            lbTours.ItemsSource = tours;
            tourTypes = BaseConnect.BaseModel.Type.ToList();
            Type type = new Type();
            type.Name = "Все типы";
            //type.Id = 10;
            tourTypes.Insert(0, type);
            cbType.ItemsSource = tourTypes;
            cbType.SelectedValuePath = "Id";
            cbType.DisplayMemberPath = "Name";
            cbType.SelectedIndex = 0;
            decimal fullP =0;
            foreach(Tour tour in tours)
            {
                fullP += tour.fullPrice;
            }
            rSummPrice.Text = fullP.ToString();
        }

        public void filter()
        {
            List<Tour> filt = BaseConnect.BaseModel.Tour.ToList();
            if (cbType.SelectedIndex != 0)
            {
                Type tepe = tourTypes.FirstOrDefault(x => x.Id == (int)cbType.SelectedValue);
                filt = filt.Where(x => x.TourType.Contains(tepe.Name)).ToList();

            }
            else
            {
                filt = BaseConnect.BaseModel.Tour.ToList();
            }
            if (chbIsActual.IsChecked == true)
            {
                filt = filt.Where(x => x.IsActual == true).ToList();
            }
            if(cbSort.SelectedIndex!=-1)
            {
                if(cbSort.SelectedIndex == 1)
                    filt = filt.OrderBy(x=>x.Price).ToList();
                if(cbSort.SelectedIndex == 2)
                {
                    filt = filt.OrderBy(x => x.Price).ToList();
                    filt.Reverse();
                }
            }
            decimal fullP = 0;
            
            filt = filt.Where(x => x.Name.Contains(tbName.Text)).ToList();
            lbTours.ItemsSource = filt;
            foreach (Tour tour in filt)
            {
                fullP += tour.fullPrice;
            }
            rSummPrice.Text = fullP.ToString();
        }

        private void cbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            filter();
        }

        private void tbName_TextChanged(object sender, TextChangedEventArgs e)
        {
            filter();
        }

        private void chbIsActual_Checked(object sender, RoutedEventArgs e)
        {
            filter();
        }

        private void chbIsActual_Unchecked(object sender, RoutedEventArgs e)
        {
            filter();
        }

        private void cbSort_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            filter();
        }

        private void btnHotel_Click(object sender, RoutedEventArgs e)
        {
            LoadPages.frmMain.Navigate(new pgHotels());
        }
    }
}
