using AvaloniaApplicationTest.Models;
using Microsoft.EntityFrameworkCore;
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Linq;

namespace AvaloniaApplicationTest.ViewModels
{
    public class ShowTourPageViewModel : ReactiveObject
    {
        public List<Tour> LBTours
        {
            get => lbTours;
            set=> this.RaiseAndSetIfChanged(ref lbTours, value);
        }
        public List<string> CBTypes
        {
            get
            {
                types.AddRange(MainWindowViewModel.myConnection.Types.Select(x => x.Name));
                return types;
            }
        }

        public List<string> CBTypesSort { get => types_sort; }

        public string SearchText
        {
            get => text;
            set
            {
                this.RaiseAndSetIfChanged(ref text, value);
                Filter();
            }
        }
        public bool SelectedActual
        {
            get => selectedActual;
            set
            {
                selectedActual = value;
                Filter();
            }
        }
        public string ValueSelected
        {
            get => data;
            set
            {
                this.RaiseAndSetIfChanged(ref data, value);
                Filter();
            }
        }

        public string ValueSelectedSort
        {
            get => selected_sort;
            set
            {
                this.RaiseAndSetIfChanged(ref selected_sort, value);
                Filter();
            }
        }

        public int AllPrice
        {
            get => all_price;
            set
            {
                this.RaiseAndSetIfChanged(ref all_price, value);
            }
        }

        bool selectedActual = false;
        List<string> types = new List<string>() { "Все типы" };
        List<string> types_sort = new List<string>() { "Без сортировки", "По возрастанию", "По убыванию" };
        string data = "Все типы";
        string selected_sort = "Без сортировки";
        string text = "";
        List<Tour> lbTours = MainWindowViewModel.myConnection.Tours.ToList();
        int all_price = 0;

        private void Filter()
        {
            List<Tour> filtered_tours = MainWindowViewModel.myConnection.Tours.ToList(); ;
            if (SearchText.Length > 0)
            {
                filtered_tours = MainWindowViewModel.myConnection.Tours.Where(x=> x.Name.ToLower().Contains(SearchText.ToLower())|| x.Description.ToLower().Contains(SearchText.ToLower())).ToList();
            }
            
            if (ValueSelected!="Все типы" && ValueSelected!=null)
            {
                Models.Type type = MainWindowViewModel.myConnection.Types.FirstOrDefault(x => x.Name == ValueSelected);
                List<Tour> tours = MainWindowViewModel.myConnection.Tours.Include(x => x.Types).Where(x => x.Types.Where(x => x.Name == type.Name).Count() != 0).ToList();
                filtered_tours = filtered_tours.Intersect(tours).ToList();
            }
            if (ValueSelectedSort == "По возрастанию")
            {
                filtered_tours = filtered_tours.OrderBy(x => x.Price).ToList();
            }
            if (ValueSelected == "По убыванию")
            {
                filtered_tours = filtered_tours.OrderBy(x=> x.Price).Reverse().ToList();
            }
            if (SelectedActual)
            {
                filtered_tours = filtered_tours.Where(x => x.Status == "Актуален").ToList();
            }
            int sum = 0;
            foreach (Tour tour in filtered_tours)
            {
                sum += Convert.ToInt32(tour.Price*tour.TicketCount);
            }
            AllPrice = sum;
            LBTours = filtered_tours;
        }
    }
}
