using System;
using System.Collections.Generic;
using System.Linq;
using ReactiveUI;
using UPtur.Models;

namespace UPtur.ViewModels
{
	public class ToursViewModel : ReactiveObject
	{
        UpPrytovContext DB = new UpPrytovContext();
       private List<Tour>? _tours;
        public List<Tour>? Tours { get => _tours; set => this.RaiseAndSetIfChanged(ref _tours, value); }

        private static List<string>? _types;
        public static List<string>? Types { get => _types; set => _types = value; }

        private static List<string>? _sort;
        public static List<string>? Sort { get => _sort; set => _sort = value; }

        private decimal _priceOfAll;
        public decimal PriceOfAll
        {
            get => _priceOfAll; 
            set => this.RaiseAndSetIfChanged(ref _priceOfAll, value);
        }

        private bool _checkOrNot = false;
        public bool CheckOrNot
        {
            get => _checkOrNot;
            set
            {
                _checkOrNot = value;
                Filter();
            }
        }

        private string? _findTour;
        public string? FindTour
        {
            get => _findTour;
            set
            {
                _findTour = value;
                Filter();
            }
        }

        private string? _selectedItem;
        public string? SelectedItem
        {
            get
            {
                return _selectedItem;
            }
            set
            {
                _selectedItem = value;
                Filter();
            }
        }

        private string? _selectedSort;
        public string? SelectedSort
        {
            get
            {
                return _selectedSort;
            }
            set
            {
                _selectedSort = value;
                Filter();
            }
        }

        public ToursViewModel()
        {
            Tours = DB.Tours.ToList();
            Types = DB.Types.Select(x=>x.Name).ToList();
            Types.Insert(0, "Все");
            SelectedItem = Types[0];
            Sort = new List<string> { "Без сортировки", "По возрастанию цены", "По убыванию цены" };
            SelectedSort = Sort[0];
        }

        public void Filter()
        {
            Tours = DB.Tours.ToList();
            PriceOfAll = 0;

            if (SelectedItem != "Все")
            {
                List <TypeOfTour> TypesForFilter = DB.TypeOfTours.ToList();
                List <int> IdForFilter = new List<int>();
                switch (SelectedItem)
                {
                    case "Международный туризм":
                        TypesForFilter = TypesForFilter.Where(x=>x.TypeId==1).ToList();
                        IdForFilter = TypesForFilter.Select(x=>x.TourId).ToList();
                        Tours = Tours.Where(x=> IdForFilter.Contains(x.Id)).ToList();
                        break;
                    case "Внутренний туризм":
                        TypesForFilter = TypesForFilter.Where(x => x.TypeId == 2).ToList();
                        IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
                        Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
                        break;
                    case "Лечебно-оздоровительные туры":
                        TypesForFilter = TypesForFilter.Where(x => x.TypeId == 3).ToList();
                        IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
                        Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
                        break;
                    case "Пляжные туры":
                        TypesForFilter = TypesForFilter.Where(x => x.TypeId == 4).ToList();
                        IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
                        Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
                        break;
                    case "Экскурсионные туры":
                        TypesForFilter = TypesForFilter.Where(x => x.TypeId == 5).ToList();
                        IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
                        Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
                        break;
                    case "Специализированные детские туры":
                        TypesForFilter = TypesForFilter.Where(x => x.TypeId == 6).ToList();
                        IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
                        Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
                        break;
                    case "Обслуживание корпоративных клиентов по заказу":
                        TypesForFilter = TypesForFilter.Where(x => x.TypeId == 7).ToList();
                        IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
                        Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
                        break;
                    case "Горнолыжные курорты":
                        TypesForFilter = TypesForFilter.Where(x => x.TypeId == 8).ToList();
                        IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
                        Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
                        break;
                    case "Культурно-исторические туры":
                        TypesForFilter = TypesForFilter.Where(x => x.TypeId == 9).ToList();
                        IdForFilter = TypesForFilter.Select(x => x.TourId).ToList();
                        Tours = Tours.Where(x => IdForFilter.Contains(x.Id)).ToList();
                        break;
                    default:
                        break;
                }
            }

            if(CheckOrNot)
            {
                Tours = Tours.Where(x => x.IsActual[0]==true).ToList();
            }

            if(FindTour!=null && FindTour != "")
            {
                Tours = Tours.Where(x=>x.Name.ToUpper().Contains(FindTour.ToString().ToUpper()) || x.DescriptionWithoutNull.Contains(FindTour.ToString().ToUpper())).ToList();
            }

            if(SelectedSort != "Без сортировки")
            {
                switch(SelectedSort)
                {
                    case "По возрастанию цены":
                        Tours = Tours.OrderBy(x=>x.Price).ToList();
                        break;
                    case "По убыванию цены":
                        Tours = Tours.OrderByDescending(x => x.Price).ToList();
                        break;
                }
            }

            foreach(Tour t in Tours)
            {
                PriceOfAll += (t.Price * t.TicketCount);
            }
        }
    }

}
